SanYeCao-blog/src/components/Navigation.astro

194 lines
4.5 KiB
Text
Raw Normal View History

2026-03-24 17:33:01 +00:00
---
2026-03-24 22:28:23 +00:00
import { getLangFromUrl, getTranslations } from "@/i18n";
2026-03-24 17:33:01 +00:00
2026-03-24 22:28:23 +00:00
const lang = getLangFromUrl(Astro.url);
const t = getTranslations(lang);
2026-09-17 04:42:39 +00:00
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);
2026-03-24 17:33:01 +00:00
---
2026-03-25 18:51:38 +00:00
<nav class="site-nav" aria-label="Site navigation">
<div class="site-nav-desktop">
2026-09-17 04:42:39 +00:00
{
items.map(({ href, label, exact }) => (
<a
href={href}
aria-current={isCurrent(href, exact) ? "page" : undefined}
>
{label}
</a>
))
}
2026-03-25 18:51:38 +00:00
</div>
<details class="site-nav-mobile">
<summary>☰ {lang === "zh" ? "导航栏" : "Menu"}</summary>
<div class="dropdown-menu">
2026-09-17 04:42:39 +00:00
{
items.map(({ href, label, exact }) => (
<a
href={href}
aria-current={isCurrent(href, exact) ? "page" : undefined}
>
{label}
</a>
))
}
2026-03-25 18:51:38 +00:00
</div>
</details>
2026-03-24 19:32:44 +00:00
</nav>
2026-03-24 22:28:23 +00:00
2026-04-24 21:01:08 +00:00
<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>
2026-03-24 22:28:23 +00:00
<style>
.site-nav {
margin: 1rem 0;
padding-bottom: 1rem;
2026-03-24 22:28:23 +00:00
position: relative;
}
2026-03-25 18:51:38 +00:00
.site-nav-desktop {
display: flex;
gap: 1.5rem;
align-items: center;
}
2026-03-24 22:28:23 +00:00
.site-nav a {
2026-09-17 04:42:39 +00:00
position: relative;
color: var(--text-color);
text-decoration: none;
font-weight: 500;
font-size: 1.05rem;
line-height: 1.4;
2026-03-24 22:28:23 +00:00
}
2026-09-17 04:42:39 +00:00
.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;
2026-03-24 22:28:23 +00:00
}
:global(.dark) .site-nav a {
2026-09-17 04:42:39 +00:00
color: var(--text-color-dark);
2026-03-24 22:28:23 +00:00
}
:global(.dark) .site-nav a:hover {
2026-09-17 04:42:39 +00:00
color: #fff;
2026-03-24 22:28:23 +00:00
}
2026-03-25 18:51:38 +00:00
.site-nav-mobile {
display: none;
position: relative;
}
.site-nav-mobile summary {
list-style: none;
cursor: pointer;
display: inline-block;
2026-09-17 04:42:39 +00:00
font-weight: 500;
font-size: 1rem;
color: var(--text-color);
text-decoration: none;
2026-03-25 18:51:38 +00:00
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;
}
2026-09-17 04:42:39 +00:00
.dropdown-menu a[aria-current="page"] {
color: var(--deep-red);
font-weight: 600;
}
2026-03-25 18:51:38 +00:00
:global(.dark) .dropdown-menu {
background: #1f1b18;
border-color: #c8a98a;
}
:global(.dark) .dropdown-menu a {
color: white;
}
2026-04-12 22:42:40 +00:00
@media (max-width: 700px) {
2026-03-25 18:51:38 +00:00
.site-nav-desktop {
display: none;
}
.site-nav-mobile {
display: block;
}
}
2026-03-24 22:28:23 +00:00
</style>