-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.js
164 lines (153 loc) · 4.7 KB
/
examples.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
import yamodal from '../src/yamodal.js';
import animation from './templates/animation.js';
import basic from './templates/basic.js';
import fade from './templates/fade.js';
import scrollLock from './templates/scroll-lock.js';
import clickOutside from './templates/click-outside.js';
import closeOnEscape from './templates/close-on-esc.js';
import autoOpenOnHashParam from './templates/auto-open-on-hash-param.js';
import dynamicContext from './templates/dynamic-context.js';
import interstitial from './templates/interstitial.js';
import dynamicContextReadingEvent from './templates/dynamic-context-reading-event.js';
// Basic example (click modal to close)
yamodal({
template: basic,
trigger_selector: '[data-modal-trigger="basic"]',
});
// Fade in / Fade out
// @see https://gist.github.com/paulirish/5d52fb081b3570c81e3a for an explanation of the `void modal_node.clientHeight` expressions.
yamodal({
template: fade,
trigger_selector: '[data-modal-trigger="fade"]',
remove_modal_after_event_type: 'transitionend',
afterInsertIntoDom(modal_node) {
// Force layout calc
void modal_node.clientHeight;
modal_node.style.opacity = '1';
},
beforeRemoveFromDom(modal_node) {
modal_node.style.opacity = '0';
},
});
// Animate in
yamodal({
template: animation,
trigger_selector: '[data-modal-trigger="animation"]',
// Technically the inner modal has the animation but this event bubbles up so this works
remove_modal_after_event_type: 'animationend',
beforeInsertIntoDom(modal_node) {
let inner_modal = modal_node.querySelector('.modal');
inner_modal.classList.add('tilt-in-fwd-tr');
inner_modal.classList.remove('slide-out-elliptic-top-bck');
},
beforeRemoveFromDom(modal_node) {
let inner_modal = modal_node.querySelector('.modal');
inner_modal.classList.remove('tilt-in-fwd-tr');
inner_modal.classList.add('slide-out-elliptic-top-bck');
},
});
// Body scroll lock
yamodal({
template: scrollLock,
trigger_selector: '[data-modal-trigger="scroll-lock"]',
afterInsertIntoDom() {
document.body.style.overflow = 'hidden';
},
afterRemoveFromDom() {
document.body.style.overflow = '';
},
});
// Click outside to close modal
let click_outside_inner_node, closeModalOnClickOutside;
yamodal({
template: clickOutside,
trigger_selector: '[data-modal-trigger="click-outside"]',
afterInsertIntoDom(modal_node, trigger_node, e) {
// When opening the modal, stop it from immediately closing
e.stopImmediatePropagation && e.stopImmediatePropagation();
},
onAfterSetup(modal_node, { isOpen, close }) {
click_outside_inner_node = modal_node.querySelector('.modal');
closeModalOnClickOutside = function (e) {
if (!click_outside_inner_node.contains(e.target) && isOpen()) {
close();
}
};
document.addEventListener('click', closeModalOnClickOutside);
},
onDestroy() {
document.removeEventListener('click', closeModalOnClickOutside);
},
});
// Close modal on ESC press
let closeModalOnEscPress;
yamodal({
template: closeOnEscape,
trigger_selector: '[data-modal-trigger="close-on-esc"]',
onAfterSetup(modal_node, { isOpen, close }) {
closeModalOnEscPress = function (e) {
// ESC_KEY === 27
if (e.keyCode === 27 && isOpen()) {
e.stopPropagation();
close();
}
};
document.addEventListener('keydown', closeModalOnEscPress);
},
onDestroy() {
document.removeEventListener('keydown', closeModalOnEscPress);
},
});
// Automatically open modal on some condition (checking a hash param)
yamodal({
template: autoOpenOnHashParam,
trigger_selector: '[data-modal-trigger="auto-open-on-hash-param"]',
onAfterSetup(modal_node, { open }) {
if (window.location.hash === '#open') {
open();
}
},
afterRemoveFromDom() {
if (window.location.hash === '#open') {
// Remove hash on close
history.pushState(
'',
document.title,
window.location.pathname + window.location.search
);
}
},
});
// Modal with dynamic context
yamodal({
template: dynamicContext,
trigger_selector: '[data-modal-trigger="dynamic-context"]',
context: () => Math.random(),
});
// Link with an interstitial
yamodal({
template: interstitial,
trigger_selector: '[data-modal-trigger="interstitial"]',
beforeInsertIntoDom(modal_node, trigger_node, event) {
event.preventDefault();
},
context(trigger_node) {
return trigger_node.href;
},
});
// Reading the custom `event` that is passed when `open()` is called
let modal = yamodal({
template: dynamicContextReadingEvent,
trigger_selector: '[data-modal-trigger="dynamic-context-reading-event"]',
context(trigger_node, event) {
return event;
},
});
let number_input = document.getElementById('number-input');
number_input.addEventListener('input', e => {
let num = parseInt(e.target.value, 10);
if (window.isNaN(num)) return;
if (num % 5 === 0) {
modal.open();
}
});