Compare commits

..
2 Commits
Author SHA1 Message Date
Ronan Berder a54d5fb4eb Bumping version to 0.3.11 2026-02-05 18:01:54 +08:00
Ronan Berder 8445f6d150 Removing current page code from sidebar.js 2026-02-05 17:57:07 +08:00
6 changed files with 53 additions and 42 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
) }}
) }}
+11 -2
View File
@@ -123,9 +123,9 @@ toc:
<dd>Individual item.
<dl>
<dt><code class="highlight language-html">&lt;a&gt;</code></dt>
<dd>A link. By default, clicking on a link will close the sidebar on mobile unless the <code>data-keep-mobile-sidebar-open</code> attribute is present.</dd>
<dd>A link. By default, clicking on a link will close the sidebar on mobile unless the <code>data-keep-mobile-sidebar-open</code> attribute is present. Set <code>aria-current="page"</code> on the active link.</dd>
<dt><code class="highlight language-html">&lt;button&gt;</code></dt>
<dd>A button. By default, clicking on a button will close the sidebar on mobile unless the <code>data-keep-mobile-sidebar-open</code> attribute is present.</dd>
<dd>A button. By default, clicking on a button will close the sidebar on mobile unless the <code>data-keep-mobile-sidebar-open</code> attribute is present. If your app uses buttons for navigation, set <code>aria-current="page"</code> on the active item.</dd>
<dt><code class="highlight language-html">&lt;details&gt;</code></dt>
<dd>Collapsible section.
<dl>
@@ -154,6 +154,15 @@ toc:
</dl>
</section>
{% set code_current %}<!-- Server-side or router-driven current link -->
<nav>
<ul>
<li><a href="/my-path?page=overview">Overview</a></li>
<li><a href="/my-path?page=settings" aria-current="page">Settings</a></li>
</ul>
</nav>{% endset %}
{{ code_block(code_current | prettyHtml, "html") }}
<h4 id="usage-html-js-4"><a href="#usage-html-js-4">JavaScript events</a></h4>
<section class="prose">
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "basecoat",
"version": "0.3.10",
"version": "0.3.11",
"description": "shadcn/ui for the rest of us",
"author": "hunvreus",
"type": "module",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "basecoat-cli",
"version": "0.3.10",
"version": "0.3.11",
"description": "Add Basecoat components to your project",
"author": "hunvreus",
"license": "MIT",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "basecoat-css",
"version": "0.3.10",
"version": "0.3.11",
"description": "Tailwind CSS for Basecoat components",
"author": {
"name": "hunvreus",
+1 -36
View File
@@ -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);
}
})();
})();