-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfeedback.js
656 lines (565 loc) · 16.2 KB
/
feedback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
export const IS_BROWSER = typeof window !== 'undefined'
export default class Feedback {
/**
* Feedback item
* @param {object} options - object containing feedback options
*/
constructor(options) {
if (!IS_BROWSER) {
console.warn('Detected environment without a `window` object')
return
}
const defaultOptions = {
id: 'feedback',
endpoint: '',
events: false,
emailField: false,
forceShowButton: false,
btnTitle: 'Feedback',
title: 'Feedback',
contactText: 'Want to chat?',
contactLink: '',
typeMessage: 'What feedback do you have?',
success: 'Thanks! 👊',
inputPlaceholder: 'Your feedback goes here!',
emailPlaceholder: 'Email address (optional)',
submitText: 'Submit',
backText: 'Back',
failedTitle: 'Oops, an error ocurred!',
failedMessage: 'Please try again. If this keeps happening, try to send an email instead.',
position: 'right',
primary: 'rgb(53, 222, 118)',
background: '#fff',
color: '#000',
types: {
general: {
text: 'General Feedback',
icon: '😁'
},
idea: {
text: 'I have an idea',
icon: '💡'
},
bug: {
text: 'I found an issue',
icon: '🐞'
}
}
}
// Parse the provided options and merge with defaults
this.options = Object.assign({}, defaultOptions, options)
// Create a new root element for feedback-js to place it's elements in
const div = document.createElement('div')
div.id = 'feedback-root'
document.body.insertBefore(div, document.body.firstChild)
this.root = div
// Add a comment to the dom
const comment = document.createComment('feedback-js modal code')
document.body.insertBefore(comment, document.body.firstChild)
// Add the required styles to the page
this._addStyle()
}
/**
* Render the default button
*/
renderButton() {
if (!this.root) return
this.showDefaultBtn = true
const html = `
<div class="feedback-btn-wrapper">
<button id="feedback-btn" title="Give feedback">
<svg class="inline w-5 h-5" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M18 13V5a2 2 0 00-2-2H4a2 2 0 00-2 2v8a2 2 0 002 2h3l3 3 3-3h3a2 2 0 002-2zM5 7a1 1 0 011-1h8a1 1 0 110 2H6a1 1 0 01-1-1zm1 3a1 1 0 100 2h3a1 1 0 100-2H6z" clip-rule="evenodd"></path></svg>
<span>${ this.options.btnTitle }</span>
</button>
</div>
`
this.root.innerHTML = html
const button = document.getElementById('feedback-btn')
button.addEventListener('click', () => {
this.renderModal()
})
}
/**
* Render the feedback modal
*/
renderModal() {
if (!this.root) return
const html = `
<div class="feedback-wrapper">
<div class="feedback-main">
<div class="feedback-header">
<p>${ this.options.title }</p>
${ this.options.contactLink.length > 0 ? '<a href=' + this.options.contactLink + '>' + this.options.contactText + '</a>' : '' }
</div>
<div class="feedback-content">
<p>${ this.options.typeMessage }</p>
<div class="feedback-content-list">
${ Object.entries(this.options.types).reduce((prev, [ id, item ]) => prev += `<button id="feedback-item-${ id }" class="feedback-item"><span>${ item.icon }</span>${ item.text }</button>`, '') }
</div>
</div>
</div>
<div class="feedback-close">
<button id="feedback-close">
<svg class="w-5 h-5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" stroke="currentColor"><path d="M6 18L18 6M6 6l12 12"></path></svg>
</button>
</div>
</div>
`
this.root.innerHTML = html
const button = document.getElementById('feedback-close')
button.addEventListener('click', () => {
this.closeModal()
})
Object.keys(this.options.types).forEach((id) => {
const elem = document.getElementById(`feedback-item-${ id }`)
elem.onclick = () => {
this.renderForm(id)
}
})
}
/**
* Render the form for the given feedback type.
*
* The type needs to be specified in the `types` option.
* @param {String} type Feedback Type
*/
renderForm(type) {
if (!this.root) return
const feedbackType = this.options.types[type]
const html = `
<div class="feedback-wrapper">
<div class="feedback-main">
<div class="feedback-header">
<p>${ feedbackType.icon } ${ feedbackType.text }</p>
</div>
<div class="feedback-content">
${ this.options.emailField ? `<input id="feedback-email" type="email" name="email" placeholder="${ this.options.emailPlaceholder }">` : '' }
<textarea id="feedback-message" name="feedback" autofocus type="text" maxlength="500" rows="5" placeholder="${ this.options.inputPlaceholder }"></textarea>
<div id="feedback-actions" class="feedback-actions">
<button type="button" id="feedback-back">${ this.options.backText }</button>
<button type="submit" id="feedback-submit">${ this.options.submitText }</button>
</div>
</div>
</div>
<div class="feedback-close">
<button id="feedback-close">
<svg class="w-5 h-5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" stroke="currentColor"><path d="M6 18L18 6M6 6l12 12"></path></svg>
</button>
</div>
</div>
`
this.current = type
this.root.innerHTML = html
document.getElementById('feedback-message').focus()
const button = document.getElementById('feedback-close')
button.addEventListener('click', () => {
this.closeModal()
})
const back = document.getElementById('feedback-back')
back.addEventListener('click', () => {
this.renderModal()
})
const submit = document.getElementById('feedback-submit')
submit.addEventListener('click', () => {
this.submitForm()
})
}
/**
* Close/hide the feedback modal.
*
* If the default button was rendered before, it will be shown again.
*/
closeModal() {
this.root.innerHTML = ''
if (this.showDefaultBtn) {
this.renderButton()
}
}
/**
* Submit the form.
*
* Will be called when the user clicks submit.
*/
submitForm() {
const message = document.getElementById('feedback-message').value
const email = this.options.emailField ? document.getElementById('feedback-email').value : undefined
const data = {
id: this.options.id,
email: email,
feedbackType: this.current,
url: window.location.href,
message: message
}
if (this.options.events) {
const event = new CustomEvent('feedback-submit', { detail: data })
window.dispatchEvent(event)
this.renderSuccess()
return
}
this.sendToEndpoint(data)
}
/**
* Send the given feedback to the specified endpoint.
* @param {Object} data - the feedback data
*/
sendToEndpoint(data) {
this.renderLoading()
const request = new XMLHttpRequest()
request.open('POST', this.options.endpoint)
request.setRequestHeader('Content-type', 'application/json')
request.send(JSON.stringify(data))
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status === 200) {
return this.renderSuccess()
}
this.renderFailed()
}
}
}
/**
* Render the loading state.
*/
renderLoading() {
if (!this.root) return
const html = `
<button id="feedback-loading"><div class="feedback-loader"><div></div><div></div><div></div><div></div></div>Loading</button>
`
document.getElementById('feedback-actions').innerHTML = html
const button = document.getElementById('feedback-close')
button.addEventListener('click', () => {
this.closeModal()
})
}
/**
* Render the success state.
*/
renderSuccess() {
if (!this.root) return
const html = `
<div class="feedback-btn-wrapper">
<button id="feedback-btn" title="Give feedback">
<span>${ this.options.success }</span>
</button>
</div>
`
this.root.innerHTML = html
setTimeout(() => {
this.renderButton()
}, 3000)
}
/**
* Render the failed state.
*/
renderFailed() {
if (!this.root) return
const html = `
<div class="feedback-wrapper">
<div class="feedback-main">
<div class="feedback-header">
<p>${ this.options.failedTitle }</p>
</div>
<div class="feedback-content">
<p>${ this.options.failedMessage }</p>
</div>
</div>
<div class="feedback-close">
<button id="feedback-close">
<svg class="w-5 h-5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" stroke="currentColor"><path d="M6 18L18 6M6 6l12 12"></path></svg>
</button>
</div>
</div>
`
this.root.innerHTML = html
const button = document.getElementById('feedback-close')
button.addEventListener('click', () => {
this.closeModal()
})
}
_addStyle() {
const css = `
#feedback-root{
font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;
}
.feedback-wrapper{
position: fixed;
z-index: 1000;
bottom: 0;
${ this.options.position === 'left' ? 'left: 0' : 'right: 0' };
margin: 2rem;
width: 100%;
max-width: 20rem;
}
.feedback-main{
background-color: ${ this.options.background };
color: ${ this.options.color };
border-radius: 0.6rem;
text-align: center;
overflow: hidden;
box-shadow: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
}
.feedback-header{
color: ${ this.options.background };
background-color: ${ this.options.primary };
padding: 0.8rem 1.25rem;
}
.feedback-header p{
margin: 0;
font-weight: 600;
font-size: 1.2rem;
}
.feedback-header a{
margin: 0;
outline: 0;
font-size: 1rem;
margin-top: 0.5rem;
display: block;
color: ${ this.options.background };
border: 2px solid ${ this.options.primary };
text-decoration: none;
padding: 5px 10px
}
.feedback-header a:hover{
text-decoration: underline;
}
.feedback-header a:focus{
border: 2px solid ${ this.options.background };
border-radius: 5px;
}
.feedback-content{
padding: 0.7rem 1.5rem;
display: flex;
align-items: center;
flex-direction: column;
}
.feedback-content p{
margin: 0;
font-size: 1rem;
font-weight: 600;
margin-top: 0.5rem;
margin-bottom: 0.6rem;
}
.feedback-content input{
border: 3px solid ${ this.options.background };
filter: brightness(95%);
border-radius: 10px;
outline: 0;
padding: 10px;
margin-bottom: 0.5rem;
width: 100%;
box-sizing: border-box;
font-size: 1rem;
font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;
}
.feedback-content textarea{
overflow: auto;
border: 3px solid ${ this.options.background };
filter: brightness(95%);
border-radius: 10px;
outline: 0;
padding: 10px;
width: 100%;
box-sizing: border-box;
resize: none;
font-size: 1rem;
font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;
}
.feedback-content textarea:focus,
.feedback-content input:focus{
border: 3px solid ${ this.options.primary };
}
.feedback-actions{
display: flex;
margin-top: 1rem;
width: 100%;
}
.feedback-actions button{
padding: 0.5rem;
border-radius: 10px;
cursor: pointer;
margin: 0;
outline: 0;
border: 3px solid rgba(255, 255, 255, 0);
appearance: none;
font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;
user-select: none;
box-shadow: 0 1px 3px 0 rgba(0,0,0,.1);
}
.feedback-actions button:focus {
border: 3px solid ${ this.options.primary };
filter: brightness(105%);
}
.feedback-actions button:active {
transform: scale(0.95);
}
#feedback-back{
background: ${ this.options.background };
color: rgba(58,71,65);
}
#feedback-submit{
margin-left: auto;
background: ${ this.options.primary };
color: ${ this.options.background };
font-weight: 700;
}
#feedback-loading{
margin-left: auto;
background: ${ this.options.primary };
color: ${ this.options.background };
font-weight: 700;
}
.feedback-loader{
display: inline-block;
position: relative;
width: 15px;
height: 15px;
margin-right: 1rem;
}
.feedback-loader div {
box-sizing: border-box;
display: block;
position: absolute;
width: 20px;
height: 20px;
margin: 0;
border: 3px solid ${ this.options.background };
border-radius: 50%;
animation: feedback-loader 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
border-color: ${ this.options.background } transparent transparent transparent;
}
.feedback-loader div:nth-child(1) {
animation-delay: -0.45s;
}
.feedback-loader div:nth-child(2) {
animation-delay: -0.3s;
}
.feedback-loader div:nth-child(3) {
animation-delay: -0.15s;
}
@keyframes feedback-loader {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.feedback-content-list{
display: flex;
flex-direction: column;
align-items: flex-start;
margin-left: auto;
margin-right: auto;
margin-bottom: 1rem;
}
.feedback-item {
width: 100%;
cursor: pointer;
user-select:none;
border:0;
outline: 0;
background: 0;
font-size: 0.9rem;
text-align: left;
display: flex;
align-items: center;
flex-direction: row;
color: ${ this.options.color };
background-color: ${ this.options.background };
border: 2px solid ${ this.options.background };
border-radius: 10px;
padding: 10px;
}
.feedback-item:hover{
filter: brightness(95%);
}
.feedback-item:focus{
border: 2px solid ${ this.options.primary };
border-radius: 5px;
}
.feedback-item span{
width: 1.5rem;
margin-right: 0.2rem;
display: block;
text-align: center;
}
.feedback-close{
margin-top: .75rem;
display: flex;
align-items: center;
justify-content: ${ this.options.position === 'left' ? 'flex-start' : 'flex-end' };
flex-direction: row;
}
.feedback-close button{
box-shadow: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
cursor: pointer;
user-select:none;
border:0;
outline: 0;
border-radius: 9999px;
padding: .5rem;
background: ${ this.options.background };
color: ${ this.options.color };
border: 2px solid ${ this.options.background };
}
.feedback-close button:focus{
border: 2px solid ${ this.options.primary };
}
.feedback-close button:hover{
filter: brightness(95%);
}
.feedback-close button:active {
transform: scale(0.95);
}
.feedback-close svg{
display: block;
width: 1.25rem;
height: 1.25rem;
}
.feedback-btn-wrapper{
position: fixed;
z-index: 1000;
bottom: 0;
${ this.options.position === 'left' ? 'left: 0' : 'right: 0' };
margin: 2rem;
}
#feedback-btn{
height: 30px;
display: flex;
align-items: center;
padding: 1.2rem 0.7rem;
border-radius: 0.4rem;
cursor: pointer;
box-shadow: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
user-select:none;
border:0;
outline: 0;
color: ${ this.options.background };
background-color: ${ this.options.primary };
transition: filter .4s ease;
}
#feedback-btn:hover, #feedback-btn:focus{
filter: brightness(105%);
}
#feedback-btn:active {
transform: scale(0.95);
}
#feedback-btn svg{
width: 1.25rem;
height: 1.25rem;
}
#feedback-btn span{
font-weight: 700;
font-size: 1rem;
margin-left: .5rem;
}
`
const comment = document.createComment('feedback-js stylesheet')
document.head.appendChild(comment)
const linkElement = document.createElement('link')
linkElement.setAttribute('rel', 'stylesheet')
linkElement.setAttribute('type', 'text/css')
linkElement.setAttribute('href', 'data:text/css;charset=UTF-8,' + encodeURIComponent(css))
document.head.appendChild(linkElement)
}
}