Files
basecoat/src/js/dropdown-menu.js
T

121 lines
3.6 KiB
JavaScript
Raw Normal View History

2025-06-12 18:44:17 +08:00
(() => {
const initDropdownMenu = (dropdownMenuComponent) => {
const trigger = dropdownMenuComponent.querySelector('[popovertarget]');
const popover = dropdownMenuComponent.querySelector('[popover]');
const menu = popover?.querySelector('[role="menu"]');
if (!trigger || !popover || !menu) return;
2025-05-01 20:53:03 +08:00
2025-06-12 18:44:17 +08:00
let menuItems = [];
let activeIndex = -1;
2025-05-01 20:53:03 +08:00
2025-06-12 18:44:17 +08:00
const setActiveItem = (index) => {
if (activeIndex > -1 && menuItems[activeIndex]) {
menuItems[activeIndex].classList.remove('active');
2025-05-01 20:53:03 +08:00
}
2025-06-12 18:44:17 +08:00
activeIndex = index;
if (activeIndex > -1 && menuItems[activeIndex]) {
const activeItem = menuItems[activeIndex];
activeItem.classList.add('active');
trigger.setAttribute('aria-activedescendant', activeItem.id);
activeItem.scrollIntoView({ block: 'nearest' });
2025-05-01 20:53:03 +08:00
} else {
2025-06-12 18:44:17 +08:00
trigger.removeAttribute('aria-activedescendant');
}
};
const handleKeyDown = (e) => {
if (!popover.matches(':popover-open')) {
if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(e.key)) {
e.preventDefault();
trigger.click();
}
return;
2025-05-01 20:53:03 +08:00
}
2025-06-12 18:44:17 +08:00
let nextIndex = activeIndex;
if (menuItems.length === 0) return;
switch (e.key) {
case 'ArrowDown':
e.preventDefault();
if (activeIndex < menuItems.length - 1) {
nextIndex = activeIndex + 1;
}
break;
case 'ArrowUp':
e.preventDefault();
if (activeIndex > 0) {
nextIndex = activeIndex - 1;
}
break;
case 'Home':
e.preventDefault();
nextIndex = 0;
break;
case 'End':
e.preventDefault();
nextIndex = menuItems.length - 1;
break;
case 'Enter':
case ' ':
e.preventDefault();
menuItems[activeIndex]?.click();
break;
2025-05-01 20:53:03 +08:00
}
2025-06-12 18:44:17 +08:00
if (nextIndex !== activeIndex) {
setActiveItem(nextIndex);
}
};
trigger.addEventListener('keydown', handleKeyDown);
popover.addEventListener('toggle', (e) => {
trigger.setAttribute('aria-expanded', e.newState === 'open');
if (e.newState === 'open') {
menuItems = Array.from(menu.querySelectorAll('[role^="menuitem"]:not([disabled])'));
menuItems.forEach((item, index) => {
if (!item.id) item.id = `${menu.id}-item-${index}`;
2025-05-01 20:53:03 +08:00
});
2025-06-12 18:44:17 +08:00
setActiveItem(0);
} else {
setActiveItem(-1);
2025-05-01 20:53:03 +08:00
}
2025-06-12 18:44:17 +08:00
});
2025-05-01 20:53:03 +08:00
2025-06-12 18:44:17 +08:00
menu.addEventListener('click', (e) => {
if (e.target.closest('[role^="menuitem"]')) {
popover.hidePopover();
}
});
menu.addEventListener('mouseover', (e) => {
const item = e.target.closest('[role^="menuitem"]:not([disabled])');
if (item) {
const index = menuItems.indexOf(item);
if (index > -1 && index !== activeIndex) {
setActiveItem(index);
}
}
});
dropdownMenuComponent.dataset.dropdownMenuInitialized = true;
};
document.querySelectorAll('.dropdown-menu:not([data-dropdown-menu-initialized])').forEach(initDropdownMenu);
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.matches('.dropdown-menu:not([data-dropdown-menu-initialized])')) {
initDropdownMenu(node);
}
node.querySelectorAll('.dropdown-menu:not([data-dropdown-menu-initialized])').forEach(initDropdownMenu);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
})();