Download ave-dropdown-menu.js from Avenislo/ave: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/spaces/Avenislo/ave/resolve/main/ave-dropdown-menu.js
- Command line
-
hf download hf://spaces/Avenislo/ave/ave-dropdown-menu.js
-
curl -L -o ave-dropdown-menu.js https://huggingface.co/spaces/Avenislo/ave/resolve/main/ave-dropdown-menu.js
10.1 kB
| 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 = ` | |
| <style> | |
| :host { | |
| display: inline-block; | |
| } | |
| .active { | |
| background-color: var(--accent); | |
| color: var(--accent-foreground); | |
| } | |
| </style> | |
| <div class="dropdown-menu" id="${this._dropdownId}"> | |
| <button type="button" class="btn-outline" aria-haspopup="true" aria-expanded="false" ${this.disabled ? 'disabled' : ''}> | |
| ${this.triggerText} | |
| <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-down-icon lucide-chevron-down"> | |
| <path d="m6 9 6 6 6-6" /> | |
| </svg> | |
| </button> | |
| <div data-popover class="popover" aria-hidden="true"> | |
| <div role="menu" aria-orientation="vertical"> | |
| ${hasCustomContent ? slotContent : this._getDefaultContent()} | |
| </div> | |
| </div> | |
| </div> | |
| `; | |
| 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: `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2" /><circle cx="12" cy="7" r="4" /></svg>`, | |
| shortcut: "⇧⌘P" | |
| }, | |
| { | |
| value: "billing", | |
| label: "Billing", | |
| icon: `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="14" x="2" y="5" rx="2" /><line x1="2" x2="22" y1="10" y2="10" /></svg>`, | |
| shortcut: "⌘B" | |
| }, | |
| { | |
| value: "settings", | |
| label: "Settings", | |
| icon: `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12.22 2h-.44a2 2 0 0 0-2 2.18l.2 1.83a2 2 0 0 1-.26 1.31l-.24.43a2 2 0 0 1-1.1.8l-1.83.51a2 2 0 0 0-1.32 2.59l.22.44a2 2 0 0 0 2.18 1.32l1.83-.2a2 2 0 0 1 1.31.26l.43.24a2 2 0 0 1 .8 1.1l.51 1.83a2 2 0 0 0 2.59 1.32l.44-.22a2 2 0 0 0 1.32-2.18l-.2-1.83a2 2 0 0 1 .26-1.31l.24-.43a2 2 0 0 1 1.1-.8l1.83-.51a2 2 0 0 0 1.32-2.59l-.22-.44a2 2 0 0 0-2.18-1.32l-1.83.2a2 2 0 0 1-1.31-.26l-.43-.24a2 2 0 0 1-.8-1.1l-.51-1.83a2 2 0 0 0-2.59-1.32Z" /><circle cx="12" cy="12" r="3" /></svg>`, | |
| shortcut: "⌘S" | |
| }, | |
| { separator: true }, | |
| { | |
| value: "logout", | |
| label: "Log out", | |
| icon: `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" /><polyline points="16,17 21,12 16,7" /><line x1="21" x2="9" y1="12" y2="12" /></svg>`, | |
| shortcut: "⇧⌘Q" | |
| } | |
| ]; | |
| return items.map(item => { | |
| if (item.separator) { | |
| return '<hr role="separator" />'; | |
| } | |
| return ` | |
| <div role="menuitem" tabindex="-1" data-value="${item.value}"> | |
| ${item.icon || ''} | |
| ${item.label} | |
| ${item.shortcut ? `<span class="ml-auto text-xs tracking-widest opacity-60">${item.shortcut}</span>` : ''} | |
| </div> | |
| `; | |
| }).join(''); | |
| } | |
| } | |
| customElements.define('ave-dropdown-menu', AveDropdownMenu); | |