Skip to content

Commit 08d4e97

Browse files
committed
fix: normalize pathname and href for active nav check
1 parent 5611c43 commit 08d4e97

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/components/Header.astro

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getLangFromUrl, useTranslations } from '@/i18n/utils';
66
77
const lang = getLangFromUrl(Astro.url);
88
const t = useTranslations(lang);
9-
const pathname = Astro.url.pathname;
9+
const pathname = Astro.url.pathname.replace(/\/$/, '') || '/';
1010
1111
const navigationLinks = [
1212
{ href: getRelativeLocaleUrl(lang, '/'), label: t('nav.home') },
@@ -50,19 +50,23 @@ const ctaLinks = {
5050
<Logo size="nav" showTitle />
5151
<ul class="menu menu-horizontal flex flex-1 flex-wrap gap-0 px-0 lg:gap-1">
5252
{
53-
navigationLinks.map((link) => (
54-
<li>
55-
<a
56-
class:list={[
57-
'btn btn-xs btn-ghost',
58-
link.href === pathname && 'btn-outline btn-primary',
59-
]}
60-
href={link.href}
61-
>
62-
{link.label}
63-
</a>
64-
</li>
65-
))
53+
navigationLinks.map((link) => {
54+
const href = typeof link.href === 'string' ? link.href : link.href?.pathname ?? '/';
55+
const isActive = href.replace(/\/$/, '') === pathname;
56+
return (
57+
<li>
58+
<a
59+
class:list={[
60+
'btn btn-xs btn-ghost',
61+
isActive && 'btn-outline btn-primary',
62+
]}
63+
href={link.href}
64+
>
65+
{link.label}
66+
</a>
67+
</li>
68+
);
69+
})
6670
}
6771
</ul>
6872
</div>

0 commit comments

Comments
 (0)