gabeefran.co-blog/src/components/Header.astro

113 lines
3.9 KiB
Text

---
import ThemeToggle from './ThemeToggle.astro';
import { getLocalizedPath, LANG_LABELS, useTranslations, type Lang } from '../lib/i18n';
interface Props {
lang: Lang;
/** See BaseLayout. */
alternates: Partial<Record<Lang, string>>;
}
const { lang, alternates } = Astro.props;
const t = useTranslations(lang);
const otherLang: Lang = lang === 'en' ? 'pt' : 'en';
const pathname = Astro.url.pathname;
const homeHref = getLocalizedPath('/', lang);
const postsHref = getLocalizedPath('/posts', lang);
const aboutHref = getLocalizedPath('/about', lang);
// Not everything is translated: fall back to the other language's home page.
const langSwitchHref = alternates[otherLang] ?? getLocalizedPath('/', otherLang);
const navLinks = [
{ href: homeHref, label: t('nav.home'), id: 'home' },
{ href: postsHref, label: t('nav.posts'), id: 'posts' },
{ href: aboutHref, label: t('nav.about'), id: 'about' },
];
function isActive(href: string): boolean {
if (href === homeHref) return pathname.replace(/\/$/, '') === href;
return pathname === href || pathname.startsWith(`${href}/`);
}
---
<header class="sticky top-0 z-40 border-b border-border/80 bg-bg/85 backdrop-blur">
<div class="flex flex-col justify-center gap-4 px-4 py-3 sm:px-6">
<div class="flex flex-row justify-between items-center">
<a href={homeHref} class="flex hover:underline shrink-0 items-center gap-2.5" aria-label={t('site.name')} data-umami-event="logo-header-click">
<img src="/logo.png" class="h-4"/>
<span class="font-mono text-[15px] font-semibold tracking-tight text-text">{t('site.name')}</span>
</a>
<div class="ml-10 flex sm:hidden justify-end gap-3">
<a
href={langSwitchHref}
hreflang={otherLang}
title={t('lang.switch')}
aria-label={`${t('lang.switch')}: ${LANG_LABELS[otherLang]}`}
data-umami-event="switch-language-header-click"
class="text-accent max-w-fit font-mono text-xs sm:text-sm underline hover:cursor-pointer hover:text-accent-deep"
>
{`${t('lang.switch')}: ${LANG_LABELS[otherLang]}`}
</a>
<ThemeToggle lang={lang} />
</div>
<div class="hidden sm:flex items-center gap-2">
<nav class="flex items-center gap-1 justify-right" aria-label={t('nav.menu')}>
{navLinks.map((link) => (
<a
href={link.href}
aria-current={isActive(link.href) ? 'page' : undefined}
class:list={[
'px-2 py-1 text-sm font-medium font-mono underline',
isActive(link.href)
? 'text-primary hover:text-primary-hover'
: 'text-text-muted hover:text-text',
]}
data-umami-event={`${link.id}-header-click`}
>
{link.label}
</a>
))}
</nav>
<a
href={langSwitchHref}
hreflang={otherLang}
title={t('lang.switch')}
aria-label={`${t('lang.switch')}: ${LANG_LABELS[otherLang]}`}
data-umami-event="switch-language-header-click"
class="text-accent max-w-fit py-1 font-mono text-xs sm:text-sm underline hover:cursor-pointer hover:text-accent-deep "
>
{`${t('lang.switch')}: ${LANG_LABELS[otherLang]}`}
</a>
<ThemeToggle lang={lang} />
</div>
</div>
<div class="flex justify-end sm:hidden items-center gap-2">
<nav class="flex items-center gap-1 justify-right" aria-label={t('nav.menu')}>
{navLinks.map((link) => (
<a
href={link.href}
aria-current={isActive(link.href) ? 'page' : undefined}
class:list={[
'px-2 py-1 text-sm font-medium font-mono underline',
isActive(link.href)
? 'text-primary hover:text-primary-hover'
: 'text-text-muted hover:text-text',
]}
data-umami-event={`${link.id}-header-click`}
>
{link.label}
</a>
))}
</nav>
</div>
</div>
</header>