-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
554 lines (480 loc) · 18.8 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Modal | Vanilla JS TailwindCSS Component</title>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="antialiased p-8">
<div class="mb-4">
<button
data-modal-trigger
aria-controls="modal-name"
aria-expanded="false"
type="button">
Open modal
</button>
</div>
<div
id="modal-name"
data-modal-target
class="hidden">
<div class="flex items-center justify-center fixed inset-0 z-50">
<div
data-modal-close
data-modal-overlay
tabindex="-1"
data-class-in="opacity-50"
data-class-out="opacity-0"
class="opacity-0 fixed inset-0 w-full z-40 transition-opacity duration-300 bg-black select-none"></div>
<div
data-modal-wrapper
data-class-in="opacity-100 translate-y-0"
data-class-out="opacity-0 translate-y-5"
class="opacity-0 translate-y-5 w-full z-50 overflow-auto max-w-md max-h-screen scrolling-touch transition-all duration-300 bg-white flex flex-col transform shadow-xl rounded-md m-5">
<div class="flex items-center justify-between border-b p-6">
<h3>
Headline
</h3>
<button
data-modal-close
aria-label="Close"
type="button"
class="text-gray-700 hover:text-black focus:text-black transition ease-in-out duration-150 ml-auto">
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /></svg>
<span class="sr-only">Close</span>
</button>
</div>
<div class="relative overflow-x-hidden overflow-y-auto h-full flex-grow p-5">
<p class="mb-4">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem in aliquid nulla, sed veritatis, officiis ea aut natus quas voluptates perferendis ratione modi ab qui omnis cum labore alias eos.
</p>
<div class="mb-4">
<label for="example" class="block mb-2">Autofocus when opening modal</label>
<input type="text" name="example" id="example" placeholder="Example" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:shadow-outline" data-modal-input-focus autofocus>
</div>
<div class="text-right">
<button
data-modal-close
type="button"
class="underline">
Close Modal
</button>
or [esc] key
</div>
</div>
</div>
</div>
</div>
<div class="mb-4">
<a
href="#modal-full-screen"
aria-expanded="false">
Open modal full screen
</a>
</div>
<div
id="modal-full-screen"
data-modal-target
class="hidden">
<div class="flex items-center justify-center fixed inset-0 z-50">
<div
data-modal-close
data-modal-overlay
tabindex="-1"
data-class-in="opacity-50"
data-class-out="opacity-0"
class="opacity-0 fixed inset-0 w-full z-40 transition-opacity duration-300 bg-black select-none"></div>
<div
data-modal-wrapper
data-class-in="opacity-100 translate-y-0"
data-class-out="opacity-0 translate-y-5"
class="opacity-0 translate-y-5 w-full h-full z-50 overflow-auto scrolling-touch transition-all duration-300 bg-white flex flex-col transform">
<div class="flex items-center justify-between p-6">
<button
data-modal-close
aria-label="Close"
type="button"
class="text-gray-700 hover:text-black focus:text-black transition ease-in-out duration-150 ml-auto">
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
<span class="sr-only">Close</span>
</button>
</div>
<div class="relative overflow-x-hidden overflow-y-auto h-full flex flex-col justify-center flex-grow p-5 w-full max-w-xl mx-auto">
<p class="mb-4">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem in aliquid nulla, sed veritatis, officiis ea aut natus quas voluptates perferendis ratione modi ab qui omnis cum labore alias eos.
</p>
<div class="text-right">
<button
data-modal-close
type="button"
class="underline">
Close Modal
</button>
or [esc] key
</div>
</div>
</div>
</div>
</div>
<div class="mb-4">
<button
data-modal-trigger
aria-controls="drawer-name"
aria-expanded="false"
type="button">
Open modal drawer-style right side
</button>
</div>
<div
id="drawer-name"
data-modal-target
class="hidden absolute inset-0 overflow-hidden">
<div
data-modal-close
data-modal-overlay
tabindex="-1"
data-class-in="opacity-50"
data-class-out="opacity-0"
class="fixed inset-0 w-full z-40 opacity-0 transition-opacity duration-500 bg-black select-none"></div>
<div
data-modal-wrapper
data-class-in="translate-x-0"
data-class-out="translate-x-full"
class="fixed top-0 right-0 bottom-0 w-10/12 h-full max-w-md z-50 overflow-auto scrolling-touch transition-transform duration-500 bg-white flex flex-col shadow-xl transform translate-x-full">
<div class="flex items-center justify-between border-b p-6">
<h3>
Headline
</h3>
<button
data-modal-close
class="text-gray-700 hover:text-black focus:text-black transition ease-in-out duration-150 ml-auto" aria-label="Close">
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
<span class="sr-only">Close</span>
</button>
</div>
<div class="relative h-full flex-grow overflow-x-hidden overflow-y-auto p-6">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem in aliquid nulla, sed veritatis, officiis ea aut
natus quas voluptates perferendis ratione modi ab qui omnis cum labore alias eos.
</p>
<div class="text-right">
<button
data-modal-close
type="button"
class="underline">
Close Drawer
</button>
or [esc] key
</div>
</div>
</div>
</div>
<div class="mb-4">
<button
data-modal-trigger
aria-controls="drawer-name-left"
aria-expanded="false"
type="button">
Open modal drawer-style left side
</button>
</div>
<div
id="drawer-name-left"
data-modal-target
class="hidden absolute inset-0 overflow-hidden">
<div
data-modal-close
data-modal-overlay
tabindex="-1"
data-class-in="opacity-50"
data-class-out="opacity-0"
class="fixed inset-0 w-full z-40 opacity-0 transition-opacity duration-500 bg-black select-none"></div>
<div
data-modal-wrapper
data-class-in="translate-x-0"
data-class-out="-translate-x-full"
class="fixed top-0 left-0 bottom-0 w-10/12 h-full max-w-md z-50 overflow-auto scrolling-touch transition-transform duration-500 bg-white flex flex-col shadow-xl transform -translate-x-full">
<div class="flex items-center justify-between border-b p-6">
<h3>
Headline
</h3>
<button
data-modal-close
class="text-gray-700 hover:text-black focus:text-black transition ease-in-out duration-150 ml-auto" aria-label="Close">
<svg class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
<span class="sr-only">Close</span>
</button>
</div>
<div class="relative h-full flex-grow overflow-x-hidden overflow-y-auto p-6">
<p class="mb-4">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem in aliquid nulla, sed veritatis, officiis ea aut
natus quas voluptates perferendis ratione modi ab qui omnis cum labore alias eos.
</p>
<div class="mb-4">
<label for="example" class="block mb-2">Autofocus when opening modal</label>
<input type="text" name="example" id="example" placeholder="Example" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:shadow-outline" data-modal-input-focus autofocus>
</div>
<div class="text-right">
<button
data-modal-close
type="button"
class="underline">
Close Drawer
</button>
or [esc] key
</div>
</div>
</div>
</div>
<script>
/**
* Modal
* modal.openModal('modal-name');
*/
const modal = (() => {
var publicAPIs = {};
//
// Settings
//
var settings = {
speedOpen: 50,
speedClose: 250,
toggleClass: 'hidden',
selectorTarget: '[data-modal-target]',
selectorTrigger: '[data-modal-trigger]',
selectorClose: '[data-modal-close]',
selectorOverlay: '[data-modal-overlay]',
selectorWrapper: '[data-modal-wrapper]',
selectorInputFocus: '[data-modal-input-focus]'
};
/**
* Element.closest() polyfill
* https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill
*/
if (!Element.prototype.closest) {
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}
Element.prototype.closest = function (s) {
var el = this;
var ancestor = this;
if (!document.documentElement.contains(el)) return null;
do {
if (ancestor.matches(s)) return ancestor;
ancestor = ancestor.parentElement;
} while (ancestor !== null);
return null;
};
}
// Trap Focus
// https://hiddedevries.nl/en/blog/2017-01-29-using-javascript-to-trap-focus-in-an-element
//
function trapFocus(element) {
var focusableEls = element.querySelectorAll('a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])');
var firstFocusableEl = focusableEls[0];
var lastFocusableEl = focusableEls[focusableEls.length - 1];
var KEYCODE_TAB = 9;
element.addEventListener('keydown', function(e) {
var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB);
if (!isTabPressed) {
return;
}
if ( e.shiftKey ) /* shift + tab */ {
if (document.activeElement === firstFocusableEl) {
lastFocusableEl.focus();
e.preventDefault();
}
} else /* tab */ {
if (document.activeElement === lastFocusableEl) {
firstFocusableEl.focus();
e.preventDefault();
}
}
});
}
//
// Methods
//
// Toggle accessibility
var toggleccessibility = function (event) {
if (event.getAttribute('aria-expanded') === 'true') {
event.setAttribute('aria-expanded', false);
} else {
event.setAttribute('aria-expanded', true);
}
};
// Open Modal
var openModal = function (event, destination) {
var target = destination;
// Check whether the modal is triggered automatically via modal.openModal
if (typeof event === 'string') {
target = document.getElementById(event);
// If modal is triggered via modal.openModal we add a data attribute
// to know whether toggleccessibility() should be used when closeModal
// as there is no button used
if (target) {
target.setAttribute('data-auto-trigger', '');
}
}
// If target doesn't exist, bail
if (!target) return;
// Find target
var overlay = target.querySelector(settings.selectorOverlay),
wrapper = target.querySelector(settings.selectorWrapper),
input = target.querySelector(settings.selectorInputFocus);
// Make it active and remoe hidden class
target.classList.remove(settings.toggleClass);
// Make body overflow hidden so it's not scrollable
document.documentElement.style.overflow = 'hidden';
// Toggle accessibility
// Check whether the modal is triggered automatically via modal.openModal
if (typeof event !== 'string') {
toggleccessibility(event);
}
// Show wrapper
setTimeout(function () {
// Show overlay
if (overlay) {
var overlayIn = overlay.getAttribute('data-class-in').split(' '),
overlayOut = overlay.getAttribute('data-class-out').split(' ');
overlay.classList.remove(...overlayOut);
overlay.classList.add(...overlayIn);
}
// Show drawer
if (wrapper) {
var wrapperIn = wrapper.getAttribute('data-class-in').split(' '),
wrapperOut = wrapper.getAttribute('data-class-out').split(' ');
wrapper.classList.remove(...wrapperOut);
wrapper.classList.add(...wrapperIn);
}
// Focus on input
if (input) {
input.focus();
}
// Trap focus
trapFocus(target);
}, settings.speedOpen);
};
// Close Modal
var closeModal = function (event) {
// Find target
var closestParent = event.closest(settings.selectorTarget),
trigger = document.querySelector('[aria-controls="' + closestParent.id + '"'),
overlay = closestParent.querySelector(settings.selectorOverlay),
wrapper = closestParent.querySelector(settings.selectorWrapper);
if (trigger === null) {
trigger = document.querySelector('a[href="#' + closestParent.id + '"');
}
// Hide overlay
if (overlay) {
var overlayIn = overlay.getAttribute('data-class-in').split(' '),
overlayOut = overlay.getAttribute('data-class-out').split(' ');
overlay.classList.remove(...overlayIn);
overlay.classList.add(...overlayOut);
}
// Hide wrapper
if (wrapper) {
var wrapperIn = wrapper.getAttribute('data-class-in').split(' '),
wrapperOut = wrapper.getAttribute('data-class-out').split(' ');
wrapper.classList.remove(...wrapperIn);
wrapper.classList.add(...wrapperOut);
}
// Remove body overflow hidden
document.documentElement.style.overflow = '';
// Toggle accessibility
// Check whether the modal was triggered automatically via modal.openModal
if (closestParent.hasAttribute('data-auto-trigger')) {
closestParent.removeAttribute('data-auto-trigger');
} else {
toggleccessibility(trigger);
}
// Make it not active
setTimeout(function () {
closestParent.classList.add(settings.toggleClass);
}, settings.speedClose);
};
// Click Handler
var clickHandler = function (event) {
// Find toggle element
var toggle = event.target,
trigger,
target,
closestButton = toggle.closest('button'),
closest = toggle.closest('a'),
open = null;
// Check whether toggle is:
// 1. <button data-modal-trigger aria-controls="modal-name" ...
// 2. <button data-modal-trigger aria-controls="modal-name"><span>...</span> ...
// 3. <a href="#modal-name" ...
// 4. <a href="#modal-name"><span>...</span> ...
// 5. null
if ( toggle.hasAttribute('data-modal-trigger') && (toggle.hasAttribute('aria-controls')) ) {
trigger = toggle.closest(settings.selectorTrigger);
target = document.getElementById(trigger.getAttribute('aria-controls'));
open = true;
} else if ( closestButton && closestButton.hasAttribute('data-modal-trigger') && (closestButton.hasAttribute('aria-controls')) ) {
trigger = toggle.closest(settings.selectorTrigger);
target = document.getElementById(trigger.getAttribute('aria-controls'));
open = true;
} else if ( (toggle.hash) && (toggle.hash.substr(1).indexOf('modal') > -1) ) {
trigger = toggle;
target = document.getElementById(toggle.hash.substr(1));
open = true;
} else if ( closest && (closest.hash) && (closest.hash.substr(1).indexOf('modal') > -1) ) {
trigger = closest;
target = document.getElementById(closest.hash.substr(1));
open = true;
}
var close = toggle.closest(settings.selectorClose);
// Open modal when the open button is clicked
if (open && target) {
openModal(trigger, target);
}
// Close modal when the close button (or overlay area) is clicked
if (close) {
closeModal(close);
}
// Prevent default link behavior
if (open || close) {
event.preventDefault();
}
};
// Keydown Handler, handle Escape button
var keydownHandler = function (event) {
if (event.key === 'Escape' || event.keyCode === 27) {
// Find all possible modals
var modals = document.querySelectorAll(settings.selectorTarget),
i;
// Find active modals and close them when escape is clicked
for (i = 0; i < modals.length; ++i) {
if (!modals[i].classList.contains(settings.toggleClass)) {
closeModal(modals[i]);
}
}
}
};
publicAPIs.init = function () {
//
// Inits & Event Listeners
//
document.addEventListener('click', clickHandler, false);
document.addEventListener('keydown', keydownHandler, false);
};
publicAPIs.openModal = openModal;
publicAPIs.closeModal = closeModal;
return publicAPIs;
})();
document.addEventListener("DOMContentLoaded", function() {
modal.init()
});
</script>
</body>
</html>