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
+29
View File
@@ -0,0 +1,29 @@
"use client";
import { createContext, useContext, useState } from "react";
type SidebarCtx = {
collapsed: boolean;
toggle: () => void;
setCollapsed: (v: boolean) => void;
};
const Ctx = createContext<SidebarCtx | null>(null);
/** 折叠状态提供者:包裹侧边栏 + 主区,供二者共享 collapsed */
export function SidebarProvider({ children }: { children: React.ReactNode }) {
const [collapsed, setCollapsed] = useState(false);
return (
<Ctx.Provider
value={{ collapsed, toggle: () => setCollapsed((v) => !v), setCollapsed }}
>
{children}
</Ctx.Provider>
);
}
export function useSidebar() {
const ctx = useContext(Ctx);
if (!ctx) throw new Error("useSidebar 必须在 SidebarProvider 内使用");
return ctx;
}
+11 -1
View File
@@ -6,13 +6,22 @@ import { cn } from "@/lib/utils";
import { copyright } from "@/lib/brand";
import { BrandLogo } from "./brand-logo";
import { navSections, activeHref } from "./nav";
import { useSidebar } from "./sidebar-context";
export function Sidebar() {
const pathname = usePathname();
const active = activeHref(pathname);
const { collapsed } = useSidebar();
return (
<aside className="flex w-60 shrink-0 flex-col bg-[var(--color-sidebar)] text-[var(--color-sidebar-foreground)]">
<aside
className={cn(
"shrink-0 overflow-hidden transition-[width] duration-200 ease-in-out",
collapsed ? "w-0" : "w-60"
)}
>
{/* 固定宽度内容容器:折叠时被外层裁剪,形成平滑滑动而非挤压 */}
<div className="flex h-full w-60 flex-col bg-[var(--color-sidebar)] text-[var(--color-sidebar-foreground)]">
{/* 品牌 */}
<div className="flex h-16 items-center px-5">
<Link href="/">
@@ -54,6 +63,7 @@ export function Sidebar() {
<div className="px-5 py-4 text-[11px] text-[var(--color-sidebar-muted)]">
{copyright()}
</div>
</div>
</aside>
);
}
+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