File size: 5,516 Bytes
b190b45 |
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 |
/**
* Modal Dialog Component
*/
export class Modal {
constructor(options = {}) {
this.id = options.id || `modal-${Date.now()}`;
this.title = options.title || '';
this.content = options.content || '';
this.size = options.size || 'medium'; // small, medium, large
this.closeOnBackdrop = options.closeOnBackdrop !== false;
this.closeOnEscape = options.closeOnEscape !== false;
this.onClose = options.onClose || null;
this.element = null;
this.backdrop = null;
}
/**
* Show the modal
*/
show() {
if (this.element) {
console.warn('[Modal] Modal already open');
return;
}
// Create backdrop
this.backdrop = document.createElement('div');
this.backdrop.className = 'modal-backdrop';
if (this.closeOnBackdrop) {
this.backdrop.addEventListener('click', () => this.hide());
}
// Create modal
this.element = document.createElement('div');
this.element.className = `modal modal-${this.size}`;
this.element.setAttribute('role', 'dialog');
this.element.setAttribute('aria-modal', 'true');
this.element.setAttribute('aria-labelledby', `${this.id}-title`);
this.element.innerHTML = `
<div class="modal-dialog">
<div class="modal-header">
<h2 class="modal-title" id="${this.id}-title">${this.escapeHtml(this.title)}</h2>
<button class="modal-close" aria-label="Close modal">×</button>
</div>
<div class="modal-body">
${this.content}
</div>
</div>
`;
// Close button handler
const closeBtn = this.element.querySelector('.modal-close');
closeBtn.addEventListener('click', () => this.hide());
// Escape key handler
if (this.closeOnEscape) {
this.escapeHandler = (e) => {
if (e.key === 'Escape') this.hide();
};
document.addEventListener('keydown', this.escapeHandler);
}
// Append to body
document.body.appendChild(this.backdrop);
document.body.appendChild(this.element);
// Trigger animation
setTimeout(() => {
this.backdrop.classList.add('show');
this.element.classList.add('show');
}, 10);
// Prevent body scroll
document.body.style.overflow = 'hidden';
// Focus first focusable element
this.trapFocus();
}
/**
* Hide the modal
*/
hide() {
if (!this.element) return;
// Remove animations
this.backdrop.classList.remove('show');
this.element.classList.remove('show');
// Remove after animation
setTimeout(() => {
if (this.backdrop && this.backdrop.parentNode) {
this.backdrop.parentNode.removeChild(this.backdrop);
}
if (this.element && this.element.parentNode) {
this.element.parentNode.removeChild(this.element);
}
this.backdrop = null;
this.element = null;
// Restore body scroll
document.body.style.overflow = '';
// Remove escape handler
if (this.escapeHandler) {
document.removeEventListener('keydown', this.escapeHandler);
}
// Call onClose callback
if (this.onClose) {
this.onClose();
}
}, 300);
}
/**
* Update modal content
*/
setContent(html) {
if (!this.element) return;
const body = this.element.querySelector('.modal-body');
if (body) {
body.innerHTML = html;
}
}
/**
* Trap focus inside modal
*/
trapFocus() {
const focusable = this.element.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
if (focusable.length === 0) return;
const firstFocusable = focusable[0];
const lastFocusable = focusable[focusable.length - 1];
firstFocusable.focus();
this.element.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
if (e.shiftKey && document.activeElement === firstFocusable) {
lastFocusable.focus();
e.preventDefault();
} else if (!e.shiftKey && document.activeElement === lastFocusable) {
firstFocusable.focus();
e.preventDefault();
}
}
});
}
/**
* Escape HTML
*/
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* Create confirmation dialog
*/
static confirm(message, onConfirm, onCancel) {
const modal = new Modal({
title: 'Confirm',
content: `
<p>${message}</p>
<div class="modal-actions">
<button class="btn btn-secondary" id="modal-cancel">Cancel</button>
<button class="btn btn-primary" id="modal-confirm">Confirm</button>
</div>
`,
size: 'small',
});
modal.show();
// Bind buttons
setTimeout(() => {
const confirmBtn = document.getElementById('modal-confirm');
const cancelBtn = document.getElementById('modal-cancel');
if (confirmBtn) {
confirmBtn.addEventListener('click', () => {
modal.hide();
if (onConfirm) onConfirm();
});
}
if (cancelBtn) {
cancelBtn.addEventListener('click', () => {
modal.hide();
if (onCancel) onCancel();
});
}
}, 50);
return modal;
}
}
export default Modal;
|