SanYeCao-blog/src/components/Navigation.astro

193 lines
4.5 KiB
Text

---
import { getLangFromUrl, getTranslations } from "@/i18n";
const lang = getLangFromUrl(Astro.url);
const t = getTranslations(lang);
const pathname = Astro.url.pathname;
const items = [
{ href: `/${lang}/`, label: t.nav.home, exact: true },
{ href: `/${lang}/about/`, label: t.nav.about },
{ href: `/${lang}/tags/`, label: t.nav.tags },
{ href: `/${lang}/timeline/`, label: t.nav.timeline },
{ href: `/${lang}/friends/`, label: t.nav.friends },
];
const isCurrent = (href: string, exact = false) =>
exact
? pathname === href || pathname === href.slice(0, -1)
: pathname.startsWith(href);
---
<nav class="site-nav" aria-label="Site navigation">
<div class="site-nav-desktop">
{
items.map(({ href, label, exact }) => (
<a
href={href}
aria-current={isCurrent(href, exact) ? "page" : undefined}
>
{label}
</a>
))
}
</div>
<details class="site-nav-mobile">
<summary>☰ {lang === "zh" ? "导航栏" : "Menu"}</summary>
<div class="dropdown-menu">
{
items.map(({ href, label, exact }) => (
<a
href={href}
aria-current={isCurrent(href, exact) ? "page" : undefined}
>
{label}
</a>
))
}
</div>
</details>
</nav>
<script>
document.addEventListener("click", (event) => {
const nav = document.querySelector(".site-nav-mobile");
if (!nav) return;
const target = event.target as Node;
if (!nav.contains(target)) {
nav.removeAttribute("open");
}
});
</script>
<style>
.site-nav {
margin: 1rem 0;
padding-bottom: 1rem;
position: relative;
}
.site-nav-desktop {
display: flex;
gap: 1.5rem;
align-items: center;
}
.site-nav a {
position: relative;
color: var(--text-color);
text-decoration: none;
font-weight: 500;
font-size: 1.05rem;
line-height: 1.4;
}
.site-nav-desktop a::after {
content: "";
position: absolute;
left: 50%;
bottom: -0.28rem;
width: 100%;
height: 2px;
background: var(--deep-red);
transform: translateX(-50%) scaleX(0);
transform-origin: center;
transition: transform 0.18s ease;
}
.site-nav-desktop a:hover::after,
.site-nav-desktop a:focus-visible::after,
.site-nav-desktop a[aria-current="page"]::after {
transform: translateX(-50%) scaleX(1);
}
.site-nav a:focus-visible {
outline: 2px solid var(--url-color);
outline-offset: 0.35rem;
text-decoration: none;
}
:global(.dark) .site-nav a {
color: var(--text-color-dark);
}
:global(.dark) .site-nav a:hover {
color: #fff;
}
.site-nav-mobile {
display: none;
position: relative;
}
.site-nav-mobile summary {
list-style: none;
cursor: pointer;
display: inline-block;
font-weight: 500;
font-size: 1rem;
color: var(--text-color);
text-decoration: none;
user-select: none;
}
.site-nav-mobile summary::-webkit-details-marker {
display: none;
}
:global(.dark) .site-nav-mobile summary {
color: white;
}
.dropdown-menu {
position: absolute;
top: calc(100% + 0.75rem);
left: 0;
min-width: 180px;
display: flex;
flex-direction: column;
gap: 0.75rem;
padding: 0.9rem 1rem;
background: #fffaf5;
border: 2px dashed #8b5e3c;
border-radius: 0;
z-index: 20;
}
.dropdown-menu a {
font-size: 1rem;
text-decoration: none;
color: black;
}
.dropdown-menu a:hover {
text-decoration: underline;
}
.dropdown-menu a[aria-current="page"] {
color: var(--deep-red);
font-weight: 600;
}
:global(.dark) .dropdown-menu {
background: #1f1b18;
border-color: #c8a98a;
}
:global(.dark) .dropdown-menu a {
color: white;
}
@media (max-width: 700px) {
.site-nav-desktop {
display: none;
}
.site-nav-mobile {
display: block;
}
}
</style>