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

144 lines
4.3 KiB
JavaScript
Raw Normal View History

2025-06-12 18:44:17 +08:00
(() => {
const initDropdownMenu = (dropdownMenuComponent) => {
const trigger = dropdownMenuComponent.querySelector(':scope > button');
const popover = dropdownMenuComponent.querySelector(':scope > [data-popover]');
const menu = popover.querySelector('[role="menu"]');
if (!trigger || !menu || !popover) {
2025-06-18 19:42:26 +08:00
const missing = [];
if (!trigger) missing.push('trigger');
if (!menu) missing.push('menu');
if (!popover) missing.push('popover');
console.error(`Dropdown menu initialisation failed. Missing element(s): ${missing.join(', ')}`, dropdownMenuComponent);
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
const closeMenu = () => {
if (trigger.getAttribute('aria-expanded') === 'false') return;
trigger.setAttribute('aria-expanded', 'false');
trigger.removeAttribute('aria-activedescendant');
popover.setAttribute('aria-hidden', 'true');
trigger.focus();
2025-06-18 19:42:26 +08:00
setActiveItem(-1);
};
const openMenu = () => {
trigger.setAttribute('aria-expanded', 'true');
popover.setAttribute('aria-hidden', 'false');
menuItems = Array.from(menu.querySelectorAll('[role^="menuitem"]:not([disabled])'));
if (menuItems.length > 0) {
setActiveItem(0);
}
};
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);
2025-05-01 20:53:03 +08:00
} else {
2025-06-12 18:44:17 +08:00
trigger.removeAttribute('aria-activedescendant');
}
};
trigger.addEventListener('click', () => {
const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
if (isExpanded) {
closeMenu();
} else {
openMenu();
}
});
2025-06-18 19:42:26 +08:00
dropdownMenuComponent.addEventListener('keydown', (event) => {
const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
2025-06-18 19:42:26 +08:00
if (event.key === 'Escape') {
if (isExpanded) closeMenu();
return;
}
if (!isExpanded) {
2025-06-18 19:42:26 +08:00
if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(event.key)) {
event.preventDefault();
openMenu();
2025-06-12 18:44:17 +08:00
}
return;
2025-05-01 20:53:03 +08:00
}
2025-06-12 18:44:17 +08:00
if (menuItems.length === 0) return;
let nextIndex = activeIndex;
2025-06-18 19:42:26 +08:00
switch (event.key) {
2025-06-12 18:44:17 +08:00
case 'ArrowDown':
2025-06-18 19:42:26 +08:00
event.preventDefault();
if (activeIndex < menuItems.length - 1) {
nextIndex = activeIndex + 1;
}
break;
2025-06-12 18:44:17 +08:00
case 'ArrowUp':
2025-06-18 19:42:26 +08:00
event.preventDefault();
if (activeIndex > 0) {
nextIndex = activeIndex - 1;
}
2025-06-12 18:44:17 +08:00
break;
case 'Home':
2025-06-18 19:42:26 +08:00
event.preventDefault();
2025-06-12 18:44:17 +08:00
nextIndex = 0;
break;
case 'End':
2025-06-18 19:42:26 +08:00
event.preventDefault();
2025-06-12 18:44:17 +08:00
nextIndex = menuItems.length - 1;
break;
case 'Enter':
case ' ':
2025-06-18 19:42:26 +08:00
event.preventDefault();
2025-06-12 18:44:17 +08:00
menuItems[activeIndex]?.click();
closeMenu();
return;
2025-05-01 20:53:03 +08:00
}
2025-06-12 18:44:17 +08:00
if (nextIndex !== activeIndex) {
setActiveItem(nextIndex);
}
});
2025-05-01 20:53:03 +08:00
2025-06-18 19:42:26 +08:00
menu.addEventListener('click', (event) => {
if (event.target.closest('[role^="menuitem"]')) {
closeMenu();
2025-06-12 18:44:17 +08:00
}
});
2025-06-18 19:42:26 +08:00
document.addEventListener('click', (event) => {
if (!dropdownMenuComponent.contains(event.target)) {
closeMenu();
2025-06-12 18:44:17 +08:00
}
});
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) return;
if (node.matches('.dropdown-menu:not([data-dropdown-menu-initialized])')) {
initDropdownMenu(node);
2025-06-12 18:44:17 +08:00
}
node.querySelectorAll('.dropdown-menu:not([data-dropdown-menu-initialized])').forEach(initDropdownMenu);
2025-06-12 18:44:17 +08:00
});
});
});
2025-06-12 18:44:17 +08:00
observer.observe(document.body, { childList: true, subtree: true });
})();