Spaces:
Running
Running
| class CustomModal extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| const modalId = this.getAttribute('id') || 'modal'; | |
| const title = this.getAttribute('title') || 'Modal Title'; | |
| const size = this.getAttribute('size') || 'md'; // sm, md, lg, xl | |
| const sizeClasses = { | |
| sm: 'max-w-md', | |
| md: 'max-w-lg', | |
| lg: 'max-w-2xl', | |
| xl: 'max-w-4xl' | |
| }; | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: none; | |
| } | |
| :host([open]) { | |
| display: flex; | |
| } | |
| .overlay { | |
| position: fixed; | |
| inset: 0; | |
| background: rgba(0, 0, 0, 0.8); | |
| backdrop-filter: blur(8px); | |
| z-index: 9999; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| padding: 1rem; | |
| animation: fadeIn 0.2s ease; | |
| } | |
| .modal { | |
| background: #0a0a0a; | |
| border: 1px solid #222; | |
| border-radius: 8px; | |
| width: 100%; | |
| max-height: 90vh; | |
| overflow: hidden; | |
| animation: slideUp 0.3s ease; | |
| display: flex; | |
| flex-direction: column; | |
| } | |
| .modal-header { | |
| padding: 1.5rem; | |
| border-bottom: 1px solid #222; | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: center; | |
| } | |
| .modal-title { | |
| font-size: 1.25rem; | |
| font-weight: 700; | |
| color: #fff; | |
| } | |
| .close-btn { | |
| background: none; | |
| border: none; | |
| color: #666; | |
| cursor: pointer; | |
| padding: 0.5rem; | |
| transition: color 0.2s; | |
| } | |
| .close-btn:hover { | |
| color: #fff; | |
| } | |
| .modal-body { | |
| padding: 1.5rem; | |
| overflow-y: auto; | |
| flex: 1; | |
| } | |
| .modal-footer { | |
| padding: 1.5rem; | |
| border-top: 1px solid #222; | |
| display: flex; | |
| justify-content: flex-end; | |
| gap: 1rem; | |
| } | |
| .btn { | |
| padding: 0.75rem 1.5rem; | |
| border-radius: 4px; | |
| font-weight: 600; | |
| cursor: pointer; | |
| transition: all 0.2s; | |
| } | |
| .btn-primary { | |
| background: #b026ff; | |
| border: none; | |
| color: white; | |
| } | |
| .btn-primary:hover { | |
| background: #9920e0; | |
| } | |
| .btn-secondary { | |
| background: transparent; | |
| border: 1px solid #333; | |
| color: white; | |
| } | |
| .btn-secondary:hover { | |
| border-color: #555; | |
| } | |
| @keyframes fadeIn { | |
| from { opacity: 0; } | |
| to { opacity: 1; } | |
| } | |
| @keyframes slideUp { | |
| from { | |
| opacity: 0; | |
| transform: translateY(20px) scale(0.95); | |
| } | |
| to { | |
| opacity: 1; | |
| transform: translateY(0) scale(1); | |
| } | |
| } | |
| </style> | |
| <div class="overlay" id="overlay"> | |
| <div class="modal ${sizeClasses[size]}"> | |
| <div class="modal-header"> | |
| <h3 class="modal-title">${title}</h3> | |
| <button class="close-btn" id="closeBtn"> | |
| <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg> | |
| </button> | |
| </div> | |
| <div class="modal-body"> | |
| <slot></slot> | |
| </div> | |
| <div class="modal-footer" id="footer"> | |
| <slot name="footer"> | |
| <button class="btn btn-secondary" id="cancelBtn">Cancel</button> | |
| <button class="btn btn-primary" id="confirmBtn">Confirm</button> | |
| </slot> | |
| </div> | |
| </div> | |
| </div> | |
| `; | |
| // Event Listeners | |
| this.shadowRoot.getElementById('closeBtn').addEventListener('click', () => this.close()); | |
| this.shadowRoot.getElementById('cancelBtn')?.addEventListener('click', () => this.close()); | |
| this.shadowRoot.getElementById('overlay').addEventListener('click', (e) => { | |
| if (e.target.id === 'overlay') this.close(); | |
| }); | |
| // Confirm button event | |
| this.shadowRoot.getElementById('confirmBtn')?.addEventListener('click', () => { | |
| this.dispatchEvent(new CustomEvent('confirm')); | |
| this.close(); | |
| }); | |
| // Keyboard escape | |
| document.addEventListener('keydown', (e) => { | |
| if (e.key === 'Escape' && this.hasAttribute('open')) { | |
| this.close(); | |
| } | |
| }); | |
| } | |
| open() { | |
| this.setAttribute('open', ''); | |
| document.body.style.overflow = 'hidden'; | |
| } | |
| close() { | |
| this.removeAttribute('open'); | |
| document.body.style.overflow = ''; | |
| } | |
| } | |
| customElements.define('custom-modal', CustomModal); |