class CustomModal extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); const modalId = this.getAttribute('data-modal') || 'modal'; const title = this.getAttribute('data-title') || 'Modal Title'; const description = this.getAttribute('data-description') || ''; this.shadowRoot.innerHTML = ` `; } } customElements.define('custom-modal', CustomModal); // Global modal functions - explicitly attached to window for Shadow DOM access window.openModal = function(modalId) { const modal = document.querySelector(`custom-modal[data-modal="${modalId}"]`); if (modal) { const overlay = modal.shadowRoot.getElementById(modalId); if (overlay) { overlay.classList.add('open'); document.body.style.overflow = 'hidden'; } } }; window.closeModal = function(modalId) { const modal = document.querySelector(`custom-modal[data-modal="${modalId}"]`); if (modal) { const overlay = modal.shadowRoot.getElementById(modalId); if (overlay) { overlay.classList.remove('open'); document.body.style.overflow = ''; } } }; window.handleFormSubmit = function(event, modalId) { event.preventDefault(); const modal = document.querySelector(`custom-modal[data-modal="${modalId}"]`); if (modal) { const formContent = modal.shadowRoot.getElementById(`form-${modalId}`); const successMessage = modal.shadowRoot.getElementById(`success-${modalId}`); formContent.style.display = 'none'; successMessage.style.display = 'block'; setTimeout(() => { closeModal(modalId); setTimeout(() => { formContent.style.display = 'block'; successMessage.style.display = 'none'; }, 300); }, 2000); } } // Close modals on Escape key (global handler for shadow DOM elements) document.addEventListener('keydown', (e) => { if (e.key === 'Escape') { const modals = document.querySelectorAll('custom-modal'); modals.forEach(modal => { const overlay = modal.shadowRoot.querySelector('.modal-overlay.open'); if (overlay) { const modalId = modal.getAttribute('data-modal'); window.closeModal(modalId); } }); } });