fix: correct mobile navigation layout

This commit is contained in:
2026-10-11 10:59:37 +08:00
parent 607dcdd660
commit 6b12546d06
2 changed files with 70 additions and 3 deletions
+5 -3
View File
@@ -1,5 +1,6 @@
import Link from "next/link";
import { getNavigationMenu } from "@/services/navigation.service";
import MobileNavigation from "./MobileNavigation";
const fallbackNavItems = [
{ title: "首页", href: "/" },
@@ -24,15 +25,15 @@ export default async function Header() {
return (
<header className="sticky top-0 z-50 border-b border-slate-100 bg-white/80 backdrop-blur">
<div className="mx-auto flex h-16 max-w-7xl items-center justify-between px-6">
<Link href="/" className="text-xl font-bold text-primary">
<Link href="/" className="shrink-0 whitespace-nowrap text-xl font-bold text-primary">
企业官网
</Link>
<nav className="flex gap-8">
<nav aria-label="主导航" className="hidden items-center gap-6 md:flex lg:gap-8">
{navItems.map((item) => (
<Link
key={item.href}
href={item.href}
className="text-slate-700"
className="whitespace-nowrap text-slate-700"
target={item.newTab ? "_blank" : undefined}
rel={item.newTab ? "noreferrer" : undefined}
>
@@ -40,6 +41,7 @@ export default async function Header() {
</Link>
))}
</nav>
<MobileNavigation items={navItems} />
</div>
</header>
);
+65
View File
@@ -0,0 +1,65 @@
"use client";
import Link from "next/link";
import { Menu, X } from "lucide-react";
import { useId, useRef, useState } from "react";
interface NavigationItem {
title: string;
href: string;
newTab: boolean;
}
export default function MobileNavigation({ items }: { items: NavigationItem[] }) {
const [isOpen, setIsOpen] = useState(false);
const menuId = useId();
const toggleRef = useRef<HTMLButtonElement>(null);
const label = isOpen ? "关闭导航菜单" : "打开导航菜单";
return (
<div
className="md:hidden"
onKeyDown={(event) => {
if (event.key === "Escape" && isOpen) {
setIsOpen(false);
toggleRef.current?.focus();
}
}}
>
<button
ref={toggleRef}
type="button"
aria-label={label}
aria-expanded={isOpen}
aria-controls={menuId}
title={label}
onClick={() => setIsOpen(!isOpen)}
className="flex h-11 w-11 items-center justify-center rounded-md text-slate-700 hover:bg-slate-100 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
>
{isOpen ? <X size={24} aria-hidden="true" /> : <Menu size={24} aria-hidden="true" />}
</button>
<nav
id={menuId}
aria-label="移动端主导航"
hidden={!isOpen}
className="absolute inset-x-0 top-full max-h-[calc(100dvh-4rem)] overflow-y-auto border-b border-slate-100 bg-white px-6 py-3 shadow-sm"
>
{items.map((item) => (
<Link
key={item.href}
href={item.href}
target={item.newTab ? "_blank" : undefined}
rel={item.newTab ? "noreferrer" : undefined}
onClick={() => {
setIsOpen(false);
toggleRef.current?.focus();
}}
className="block break-words rounded-md px-3 py-3 text-slate-700 hover:bg-slate-50 focus-visible:outline-2 focus-visible:outline-primary"
>
{item.title}
</Link>
))}
</nav>
</div>
);
}