feat: 侧边栏可点击折叠/展开

顶栏左侧新增切换按钮,点击后左侧菜单在 w-60↔w-0 间平滑伸缩隐藏。
折叠状态通过 SidebarProvider(context) 在侧边栏与顶栏间共享。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 22:56:04 +08:00
co-authored by Claude Opus 4.8
parent d123d1e46d
commit 2c337c7d62
4 changed files with 76 additions and 22 deletions
+14 -2
View File
@@ -4,9 +4,10 @@ import { useEffect, useRef, useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { signOut } from "next-auth/react";
import { ChevronDown, User, LogOut } from "lucide-react";
import { ChevronDown, User, LogOut, PanelLeft } from "lucide-react";
import { Avatar } from "@/components/ui/avatar";
import { navSections } from "./nav";
import { useSidebar } from "./sidebar-context";
function pageTitle(pathname: string): string {
if (pathname === "/") return "控制台";
@@ -28,6 +29,7 @@ export function Topbar({
const pathname = usePathname();
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const { toggle } = useSidebar();
useEffect(() => {
function onClick(e: MouseEvent) {
@@ -41,7 +43,17 @@ export function Topbar({
return (
<header className="sticky top-0 z-20 flex h-16 items-center justify-between border-b bg-[var(--color-surface)]/90 px-6 backdrop-blur">
<h1 className="text-base font-semibold">{pageTitle(pathname)}</h1>
<div className="flex items-center gap-3">
<button
type="button"
onClick={toggle}
aria-label="收起/展开菜单"
className="-ml-1.5 rounded-md p-1.5 text-[var(--color-muted-foreground)] transition-colors hover:bg-[var(--color-muted)] hover:text-[var(--color-foreground)]"
>
<PanelLeft className="h-[18px] w-[18px]" />
</button>
<h1 className="text-base font-semibold">{pageTitle(pathname)}</h1>
</div>
<div className="relative" ref={ref}>
<button