Removing current page code from sidebar.js

This commit is contained in:
Ronan Berder
2026-02-05 17:57:07 +08:00
parent 713c163fb8
commit 8445f6d150
3 changed files with 50 additions and 39 deletions
+38 -1
View File
@@ -1,6 +1,43 @@
{% from "sidebar.njk" import sidebar %}
{% from "popover.njk" import popover %}
<script>
(() => {
document.addEventListener('DOMContentLoaded', () => {
const sidebar = document.getElementById('sidebar');
if (!sidebar) return;
const updateCurrentLinks = () => {
const currentPath = window.location.pathname.replace(/\/$/, '');
const currentSearch = window.location.search;
const currentKey = `${currentPath}${currentSearch}`;
sidebar.querySelectorAll('a, button').forEach((item) => {
const href = item.getAttribute('href');
if (!href) {
item.removeAttribute('aria-current');
return;
}
const url = new URL(href, window.location.origin);
const itemPath = url.pathname.replace(/\/$/, '');
const itemKey = `${itemPath}${url.search}`;
if (itemKey === currentKey) {
item.setAttribute('aria-current', 'page');
} else {
item.removeAttribute('aria-current');
}
});
};
document.addEventListener('htmx:afterSettle', updateCurrentLinks);
window.addEventListener('popstate', updateCurrentLinks);
updateCurrentLinks();
});
})();
</script>
{% set footer %}
{% set trigger %}
<img src="https://github.com/hunvreus.png" class="rounded-lg shrink-0 size-8">
@@ -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
) }}
) }}