mirror of
https://github.com/hunvreus/basecoat.git
synced 2026-11-14 14:08:18 +00:00
Removing current page code from sidebar.js
This commit is contained in:
+1
-36
@@ -1,21 +1,4 @@
|
||||
(() => {
|
||||
// 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;
|
||||
}
|
||||
|
||||
const initSidebar = (sidebarComponent) => {
|
||||
const initialOpen = sidebarComponent.dataset.initialOpen !== 'false';
|
||||
const initialMobileOpen = sidebarComponent.dataset.initialMobileOpen === 'true';
|
||||
@@ -25,20 +8,6 @@
|
||||
? (window.innerWidth >= breakpoint ? initialOpen : initialMobileOpen)
|
||||
: initialOpen;
|
||||
|
||||
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');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const updateState = () => {
|
||||
sidebarComponent.setAttribute('aria-hidden', !open);
|
||||
if (open) {
|
||||
@@ -89,11 +58,7 @@
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('popstate', updateCurrentPageLinks);
|
||||
window.addEventListener('basecoat:locationchange', updateCurrentPageLinks);
|
||||
|
||||
updateState();
|
||||
updateCurrentPageLinks();
|
||||
sidebarComponent.dataset.sidebarInitialized = true;
|
||||
sidebarComponent.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
||||
};
|
||||
@@ -101,4 +66,4 @@
|
||||
if (window.basecoat) {
|
||||
window.basecoat.register('sidebar', '.sidebar:not([data-sidebar-initialized])', initSidebar);
|
||||
}
|
||||
})();
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user