-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal.js
More file actions
372 lines (316 loc) · 10.7 KB
/
modal.js
File metadata and controls
372 lines (316 loc) · 10.7 KB
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
/**
* Modal Component
* A customizable modal component matching the CodeSignal Design System
*/
class Modal {
constructor(options = {}) {
// Configuration
this.config = {
size: options.size || 'medium', // 'small', 'medium', 'large', 'xlarge'
title: options.title || null,
content: options.content || null, // Can be HTML string, DOM element, or selector
showCloseButton: options.showCloseButton !== undefined ? options.showCloseButton : true,
footerButtons: options.footerButtons || null, // Array of button configs or null to hide footer
closeOnOverlayClick: options.closeOnOverlayClick !== undefined ? options.closeOnOverlayClick : true,
closeOnEscape: options.closeOnEscape !== undefined ? options.closeOnEscape : true,
onOpen: options.onOpen || null,
onClose: options.onClose || null,
...options
};
// State
this.isOpen = false;
this.previousActiveElement = null;
this.bodyScrollLocked = false;
// Create modal structure
this.init();
}
init() {
// Create overlay
this.overlay = document.createElement('div');
this.overlay.className = 'modal-overlay';
this.overlay.setAttribute('role', 'dialog');
this.overlay.setAttribute('aria-modal', 'true');
this.overlay.setAttribute('aria-labelledby', 'modal-title');
// Create dialog
this.dialog = document.createElement('div');
this.dialog.className = `modal-dialog size-${this.config.size}`;
// Create header
this.header = document.createElement('div');
this.header.className = 'modal-header';
if (this.config.title) {
this.title = document.createElement('h2');
this.title.className = 'modal-title';
this.title.id = 'modal-title';
this.title.textContent = this.config.title;
this.header.appendChild(this.title);
}
if (this.config.showCloseButton) {
this.closeButton = document.createElement('button');
this.closeButton.className = 'modal-close-button';
this.closeButton.setAttribute('type', 'button');
this.closeButton.setAttribute('aria-label', 'Close modal');
this.closeButton.innerHTML = this.getCloseIcon();
this.header.appendChild(this.closeButton);
}
// Only append header if it has content
if (this.config.title || this.config.showCloseButton) {
// Header will be appended to dialog
} else {
// Hide header if no title and no close button
this.header.style.display = 'none';
}
// Create content area
this.content = document.createElement('div');
this.content.className = 'modal-content';
this.setContent(this.config.content);
// Create footer
this.footer = null;
if (this.config.footerButtons) {
this.footer = document.createElement('div');
this.footer.className = 'modal-footer';
const buttonsRow = document.createElement('div');
buttonsRow.className = 'modal-footer-buttons';
this.createFooterButtons(buttonsRow);
this.footer.appendChild(buttonsRow);
}
// Assemble structure
if (this.config.title || this.config.showCloseButton) {
this.dialog.appendChild(this.header);
}
this.dialog.appendChild(this.content);
if (this.footer) {
this.dialog.appendChild(this.footer);
}
this.overlay.appendChild(this.dialog);
// Bind events
this.bindEvents();
// Append to body (but keep hidden until opened)
document.body.appendChild(this.overlay);
}
setContent(content) {
// Clear existing content
this.content.innerHTML = '';
if (!content) {
return;
}
// Handle different content types
if (content instanceof HTMLElement) {
// DOM element
this.content.appendChild(content);
} else if (typeof content === 'object' && content.nodeType) {
// Node object
this.content.appendChild(content);
} else if (typeof content === 'string') {
// Check if it's a CSS selector first
if (content.startsWith('#') || content.startsWith('.')) {
// CSS selector (ID or class)
const element = document.querySelector(content);
if (element) {
// Clone the element to avoid moving it from its original location
const cloned = element.cloneNode(true);
// Remove any hidden/display:none classes that might prevent content from showing
cloned.classList.remove('hidden-content');
cloned.style.display = '';
this.content.appendChild(cloned);
}
} else {
// HTML string
this.content.innerHTML = content;
}
}
}
createFooterButtons(container) {
if (!this.config.footerButtons || !Array.isArray(this.config.footerButtons)) {
return;
}
this.config.footerButtons.forEach((buttonConfig, index) => {
const button = document.createElement('button');
button.className = 'button';
// Determine button type
if (buttonConfig.type === 'secondary') {
button.classList.add('button-secondary');
} else if (buttonConfig.type === 'tertiary') {
button.classList.add('button-tertiary');
} else {
button.classList.add('button-primary');
}
button.textContent = buttonConfig.label || 'Button';
if (buttonConfig.onClick) {
button.addEventListener('click', (e) => {
buttonConfig.onClick(e, this);
});
} else {
// Default: close modal on click
button.addEventListener('click', () => {
this.close();
});
}
container.appendChild(button);
});
}
bindEvents() {
// Close button
if (this.closeButton) {
this.closeButton.addEventListener('click', () => {
this.close();
});
}
// Overlay click
if (this.config.closeOnOverlayClick) {
this.overlay.addEventListener('click', (e) => {
if (e.target === this.overlay) {
this.close();
}
});
}
// Escape key
if (this.config.closeOnEscape) {
this.escapeHandler = (e) => {
if (e.key === 'Escape' && this.isOpen) {
this.close();
}
};
document.addEventListener('keydown', this.escapeHandler);
}
// Prevent dialog clicks from closing modal
this.dialog.addEventListener('click', (e) => {
e.stopPropagation();
});
// Handle anchor link clicks within modal content for smooth scrolling
this.content.addEventListener('click', (e) => {
const link = e.target.closest('a[href^="#"]');
if (link && link.hash) {
e.preventDefault();
const targetId = link.hash.substring(1);
const targetElement = this.content.querySelector(`#${targetId}`);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
});
}
open() {
if (this.isOpen) return;
this.isOpen = true;
this.previousActiveElement = document.activeElement;
// Show overlay
this.overlay.classList.add('open');
this.overlay.setAttribute('aria-hidden', 'false');
// Lock body scroll
this.lockBodyScroll();
// Focus management
if (this.closeButton) {
this.closeButton.focus();
} else if (this.title) {
this.title.focus();
}
// Call onOpen callback
if (this.config.onOpen) {
this.config.onOpen(this);
}
}
close() {
if (!this.isOpen) return;
this.isOpen = false;
// Hide overlay
this.overlay.classList.remove('open');
this.overlay.setAttribute('aria-hidden', 'true');
// Unlock body scroll
this.unlockBodyScroll();
// Restore focus
if (this.previousActiveElement) {
this.previousActiveElement.focus();
}
// Call onClose callback
if (this.config.onClose) {
this.config.onClose(this);
}
}
lockBodyScroll() {
if (this.bodyScrollLocked) return;
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
document.body.style.overflow = 'hidden';
if (scrollbarWidth > 0) {
document.body.style.paddingRight = `${scrollbarWidth}px`;
}
this.bodyScrollLocked = true;
}
unlockBodyScroll() {
if (!this.bodyScrollLocked) return;
document.body.style.overflow = '';
document.body.style.paddingRight = '';
this.bodyScrollLocked = false;
}
updateContent(content) {
this.setContent(content);
}
updateTitle(title) {
if (this.title) {
this.title.textContent = title;
} else if (title) {
// Create title if it doesn't exist
this.title = document.createElement('h2');
this.title.className = 'modal-title';
this.title.id = 'modal-title';
this.title.textContent = title;
this.header.insertBefore(this.title, this.closeButton || null);
}
}
getCloseIcon() {
return `<span class="modal-close-icon">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M18 6L6 18M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</span>`;
}
destroy() {
// Remove event listeners
if (this.escapeHandler) {
document.removeEventListener('keydown', this.escapeHandler);
}
// Unlock body scroll if still locked
this.unlockBodyScroll();
// Remove from DOM
if (this.overlay && this.overlay.parentNode) {
this.overlay.parentNode.removeChild(this.overlay);
}
}
/**
* Static method to create a Help Modal
* Convenience method that creates a modal optimized for help/documentation content
* with sensible defaults (xlarge size, footer with close button)
*
* @param {Object} options - Configuration options (same as Modal constructor)
* @param {string} options.title - Modal title (default: 'Help')
* @param {string|Element} options.content - Required: HTML content for the help modal
* @param {Array} options.footerButtons - Optional: Footer buttons (default: single 'Close' button)
* @param {Object} options.modalOptions - Optional: Additional modal options to override defaults
* @returns {Modal} The created Modal instance
*/
static createHelpModal(options = {}) {
const {
title = 'Help',
content,
footerButtons,
...modalOptions
} = options;
// Default to a single Close button if no footer buttons provided
const defaultFooterButtons = footerButtons !== undefined
? footerButtons
: [{ label: 'Close', type: 'primary' }];
return new Modal({
size: 'xlarge',
title,
content,
footerButtons: defaultFooterButtons,
showCloseButton: true,
...modalOptions
});
}
}
// Export for ES6 modules
export default Modal;
// Also make available globally
if (typeof window !== 'undefined') {
window.Modal = Modal;
}