From 8445f6d150a693982870174ad31d3668431be6f9 Mon Sep 17 00:00:00 2001 From: Ronan Berder Date: Thu, 5 Feb 2026 17:57:07 +0800 Subject: [PATCH] Removing current page code from sidebar.js --- docs/src/_includes/partials/sidebar.njk | 39 ++++++++++++++++++++++++- docs/src/components/sidebar.njk | 13 +++++++-- src/js/sidebar.js | 37 +---------------------- 3 files changed, 50 insertions(+), 39 deletions(-) diff --git a/docs/src/_includes/partials/sidebar.njk b/docs/src/_includes/partials/sidebar.njk index 470dc65..979bc14 100644 --- a/docs/src/_includes/partials/sidebar.njk +++ b/docs/src/_includes/partials/sidebar.njk @@ -1,6 +1,43 @@ {% from "sidebar.njk" import sidebar %} {% from "popover.njk" import popover %} + + {% set footer %} {% set trigger %} @@ -136,4 +173,4 @@ footer=footer, content_attrs={ "class": "scrollbar [&_[data-new-link]::after]:content-['New'] [&_[data-new-link]::after]:ml-auto [&_[data-new-link]::after]:text-xs [&_[data-new-link]::after]:font-medium [&_[data-new-link]::after]:bg-sidebar-primary [&_[data-new-link]::after]:text-sidebar-primary-foreground [&_[data-new-link]::after]:px-2 [&_[data-new-link]::after]:py-0.5 [&_[data-new-link]::after]:rounded-md" }, menu=menu -) }} +) }} \ No newline at end of file diff --git a/docs/src/components/sidebar.njk b/docs/src/components/sidebar.njk index 151e909..8eb5fb6 100644 --- a/docs/src/components/sidebar.njk +++ b/docs/src/components/sidebar.njk @@ -123,9 +123,9 @@ toc:
Individual item.
<a>
-
A link. By default, clicking on a link will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present.
+
A link. By default, clicking on a link will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present. Set aria-current="page" on the active link.
<button>
-
A button. By default, clicking on a button will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present.
+
A button. By default, clicking on a button will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present. If your app uses buttons for navigation, set aria-current="page" on the active item.
<details>
Collapsible section.
@@ -154,6 +154,15 @@ toc:
+{% set code_current %} +{% endset %} +{{ code_block(code_current | prettyHtml, "html") }} +

JavaScript events

diff --git a/src/js/sidebar.js b/src/js/sidebar.js index b327524..fc4a007 100644 --- a/src/js/sidebar.js +++ b/src/js/sidebar.js @@ -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); } -})(); \ No newline at end of file +})();