import { BaseComponent } from "./ave-base-component.js"; class AveDropdownMenu extends BaseComponent { static get observedAttributes() { return ['trigger-text', 'disabled', 'menu-items']; } constructor() { super(); this._dropdownId = `dropdown-${Math.random().toString(36).substr(2, 9)}`; this._trigger = null; this._popover = null; this._menu = null; this._menuItems = []; this._activeIndex = -1; this._handleTriggerClick = this._handleTriggerClick.bind(this); this._handleKeydown = this._handleKeydown.bind(this); this._handleMenuClick = this._handleMenuClick.bind(this); this._handleDocumentClick = this._handleDocumentClick.bind(this); } // Property getters/setters get triggerText() { return this.getAttribute('trigger-text') || 'Open Menu'; } set triggerText(value) { this.setAttribute('trigger-text', value); } get disabled() { return this._getBooleanAttribute('disabled'); } set disabled(value) { this._setBooleanAttribute('disabled', value); } get menuItems() { const itemsAttr = this.getAttribute('menu-items'); if (itemsAttr && typeof itemsAttr === 'string') { try { return JSON.parse(itemsAttr); } catch (e) { console.warn('Invalid JSON in menu-items attribute:', itemsAttr); return null; } } return null; } set menuItems(value) { if (Array.isArray(value)) { this.setAttribute('menu-items', JSON.stringify(value)); } else { this.setAttribute('menu-items', value); } } setupEventListeners() { this._trigger = this.shadowRoot.querySelector('button'); this._popover = this.shadowRoot.querySelector('[data-popover]'); this._menu = this.shadowRoot.querySelector('[role="menu"]'); if (this._trigger && this._popover && this._menu) { this._trigger.addEventListener('click', this._handleTriggerClick); this.addEventListener('keydown', this._handleKeydown); this._menu.addEventListener('click', this._handleMenuClick); document.addEventListener('click', this._handleDocumentClick); } } _handleTriggerClick() { if (this.disabled) return; const isExpanded = this._trigger.getAttribute('aria-expanded') === 'true'; if (isExpanded) { this.closeMenu(); } else { this.openMenu(); } } _handleKeydown(e) { const isExpanded = this._trigger.getAttribute('aria-expanded') === 'true'; if (e.key === 'Escape') { if (isExpanded) this.closeMenu(); return; } if (!isExpanded) { if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(e.key)) { e.preventDefault(); this.openMenu(); } return; } if (this._menuItems.length === 0) return; let nextIndex = this._activeIndex; switch (e.key) { case 'ArrowDown': e.preventDefault(); nextIndex = this._activeIndex < this._menuItems.length - 1 ? this._activeIndex + 1 : 0; break; case 'ArrowUp': e.preventDefault(); nextIndex = this._activeIndex > 0 ? this._activeIndex - 1 : this._menuItems.length - 1; break; case 'Home': e.preventDefault(); nextIndex = 0; break; case 'End': e.preventDefault(); nextIndex = this._menuItems.length - 1; break; case 'Enter': case ' ': e.preventDefault(); if (this._activeIndex > -1) { const activeItem = this._menuItems[this._activeIndex]; this._handleMenuItemClick(activeItem); } this.closeMenu(); return; } if (nextIndex !== this._activeIndex) { this._setActiveItem(nextIndex); } } _handleMenuClick(e) { const clickedItem = e.target.closest('[role="menuitem"]'); if (clickedItem) { this._handleMenuItemClick(clickedItem); this.closeMenu(); } } _handleMenuItemClick(item) { // Dispatch menu item click event this._dispatch('ave-menuitem-click', { item: item, value: item.getAttribute('data-value'), text: item.textContent.trim() }); } _handleDocumentClick(e) { if (!this.contains(e.target)) { this.closeMenu(); } } // Public API methods openMenu() { if (!this._trigger || !this._popover || !this._menu) return; this._trigger.setAttribute('aria-expanded', 'true'); this._popover.setAttribute('aria-hidden', 'false'); this._menuItems = Array.from(this._menu.querySelectorAll('[role="menuitem"]:not([disabled])')); if (this._menuItems.length > 0) { this._setActiveItem(0); } // Dispatch open event this._dispatch('ave-dropdown-open', { trigger: this._trigger }); } closeMenu() { if (!this._trigger || !this._popover) return; if (this._trigger.getAttribute('aria-expanded') === 'false') return; this._trigger.setAttribute('aria-expanded', 'false'); this._trigger.removeAttribute('aria-activedescendant'); this._popover.setAttribute('aria-hidden', 'true'); this._trigger.focus(); this._activeIndex = -1; // Dispatch close event this._dispatch('ave-dropdown-close', { trigger: this._trigger }); } isOpen() { return this._trigger?.getAttribute('aria-expanded') === 'true'; } _setActiveItem(index) { if (this._activeIndex > -1 && this._menuItems[this._activeIndex]) { this._menuItems[this._activeIndex].classList.remove('active'); } this._activeIndex = index; if (this._activeIndex > -1 && this._menuItems[this._activeIndex]) { const activeItem = this._menuItems[this._activeIndex]; activeItem.classList.add('active'); if (activeItem.id) { this._trigger.setAttribute('aria-activedescendant', activeItem.id); } // Scroll into view if needed activeItem.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); } else { this._trigger.removeAttribute('aria-activedescendant'); } } render() { const slotContent = this.innerHTML; const hasCustomContent = slotContent.trim().length > 0; this.shadowRoot.innerHTML = `
`; this.setupEventListeners(); } disconnectedCallback() { // Remove event listeners if (this._trigger) { this._trigger.removeEventListener('click', this._handleTriggerClick); } this.removeEventListener('keydown', this._handleKeydown); if (this._menu) { this._menu.removeEventListener('click', this._handleMenuClick); } document.removeEventListener('click', this._handleDocumentClick); } _getDefaultContent() { const items = this.menuItems || [ { value: "profile", label: "Profile", icon: ``, shortcut: "⇧⌘P" }, { value: "billing", label: "Billing", icon: ``, shortcut: "⌘B" }, { value: "settings", label: "Settings", icon: ``, shortcut: "⌘S" }, { separator: true }, { value: "logout", label: "Log out", icon: ``, shortcut: "⇧⌘Q" } ]; return items.map(item => { if (item.separator) { return '