mirror of
https://github.com/hunvreus/basecoat.git
synced 2026-11-17 23:48:19 +00:00
Revamped the select docs
This commit is contained in:
+45
-3
@@ -1,4 +1,21 @@
|
||||
(() => {
|
||||
// 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';
|
||||
@@ -8,6 +25,20 @@
|
||||
? (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) {
|
||||
@@ -35,17 +66,28 @@
|
||||
});
|
||||
|
||||
sidebarComponent.addEventListener('click', (e) => {
|
||||
const target = e.target;
|
||||
const nav = sidebarComponent.querySelector('nav');
|
||||
const closeTarget = e.target.closest('[data-sidebar-close-on-mobile]');
|
||||
|
||||
if (closeTarget && window.innerWidth < breakpoint) {
|
||||
const isMobile = window.innerWidth < breakpoint;
|
||||
|
||||
if (isMobile && (target.closest('[data-close-mobile-sidebar]') || (target.closest('a, button') && !target.closest('[data-keep-mobile-sidebar-open]')))) {
|
||||
if (document.activeElement) document.activeElement.blur();
|
||||
setState(false);
|
||||
} else if (e.target === sidebarComponent || (nav && !nav.contains(e.target))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (target === sidebarComponent || (nav && !nav.contains(target))) {
|
||||
if (document.activeElement) document.activeElement.blur();
|
||||
setState(false);
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('popstate', updateCurrentPageLinks);
|
||||
window.addEventListener('basecoat:locationchange', updateCurrentPageLinks);
|
||||
|
||||
updateState();
|
||||
updateCurrentPageLinks();
|
||||
sidebarComponent.dataset.sidebarInitialized = true;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user