Files
basecoat/src/js/sidebar.js
T

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2025-06-12 10:37:19 +08:00
(() => {
2025-06-13 14:14:38 +08:00
// Monkey patching the history API to detect client-side navigation
if (!window.history.__basecoatPatched) {
const originalPushState = window.history.pushState;
window.history.pushState = function(...args) {
originalPushState.apply(this, args);
window.dispatchEvent(new Event('basecoat:locationchange'));
};
const originalReplaceState = window.history.replaceState;
window.history.replaceState = function(...args) {
originalReplaceState.apply(this, args);
window.dispatchEvent(new Event('basecoat:locationchange'));
};
window.history.__basecoatPatched = true;
}
2025-06-12 10:37:19 +08:00
const initSidebar = (sidebarComponent) => {
const initialOpen = sidebarComponent.dataset.initialOpen !== 'false';
const initialMobileOpen = sidebarComponent.dataset.initialMobileOpen === 'true';
const breakpoint = parseInt(sidebarComponent.dataset.breakpoint) || 768;
let open = breakpoint > 0
? (window.innerWidth >= breakpoint ? initialOpen : initialMobileOpen)
: initialOpen;
2025-06-13 14:14:38 +08:00
const updateCurrentPageLinks = () => {
const currentPath = window.location.pathname.replace(/\/$/, '');
sidebarComponent.querySelectorAll('a').forEach(link => {
if (link.hasAttribute('data-ignore-current')) return;
const linkPath = new URL(link.href).pathname.replace(/\/$/, '');
if (linkPath === currentPath) {
link.setAttribute('aria-current', 'page');
} else {
link.removeAttribute('aria-current');
}
});
};
2025-06-12 10:37:19 +08:00
const updateState = () => {
sidebarComponent.setAttribute('aria-hidden', !open);
if (open) {
sidebarComponent.removeAttribute('inert');
} else {
sidebarComponent.setAttribute('inert', '');
}
};
2025-05-02 18:27:45 +08:00
2025-06-12 10:37:19 +08:00
const setState = (state) => {
open = state;
updateState();
};
2025-05-02 18:27:45 +08:00
2025-06-12 10:37:19 +08:00
const sidebarId = sidebarComponent.id;
2025-06-18 19:42:26 +08:00
document.addEventListener('basecoat:sidebar', (event) => {
if (event.detail?.id && event.detail.id !== sidebarId) return;
switch (event.detail?.action) {
case 'open':
setState(true);
break;
case 'close':
setState(false);
break;
default:
setState(!open);
break;
}
2025-06-12 10:37:19 +08:00
});
2025-06-18 19:42:26 +08:00
sidebarComponent.addEventListener('click', (event) => {
const target = event.target;
2025-06-12 10:37:19 +08:00
const nav = sidebarComponent.querySelector('nav');
2025-06-13 14:14:38 +08:00
const isMobile = window.innerWidth < breakpoint;
2025-06-14 16:48:01 +08:00
if (isMobile && (target.closest('a, button') && !target.closest('[data-keep-mobile-sidebar-open]'))) {
2025-06-13 14:14:38 +08:00
if (document.activeElement) document.activeElement.blur();
2025-06-12 10:37:19 +08:00
setState(false);
2025-06-13 14:14:38 +08:00
return;
}
if (target === sidebarComponent || (nav && !nav.contains(target))) {
if (document.activeElement) document.activeElement.blur();
2025-06-12 10:37:19 +08:00
setState(false);
}
});
2025-06-13 14:14:38 +08:00
window.addEventListener('popstate', updateCurrentPageLinks);
window.addEventListener('basecoat:locationchange', updateCurrentPageLinks);
2025-06-12 10:37:19 +08:00
updateState();
2025-06-13 14:14:38 +08:00
updateCurrentPageLinks();
2025-06-12 10:37:19 +08:00
sidebarComponent.dataset.sidebarInitialized = true;
2025-06-19 15:21:12 +08:00
sidebarComponent.dispatchEvent(new CustomEvent('basecoat:initialized'));
2025-06-12 10:37:19 +08:00
};
if (window.basecoat) {
window.basecoat.register('sidebar', '.sidebar:not([data-sidebar-initialized])', initSidebar);
}
2025-06-12 10:37:19 +08:00
})();