From 2c337c7d62910da53279844cc3978a6bb85157dd Mon Sep 17 00:00:00 2001 From: luishou Date: Sat, 11 Jul 2026 22:56:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BE=A7=E8=BE=B9=E6=A0=8F=E5=8F=AF?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E6=8A=98=E5=8F=A0/=E5=B1=95=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 顶栏左侧新增切换按钮,点击后左侧菜单在 w-60↔w-0 间平滑伸缩隐藏。 折叠状态通过 SidebarProvider(context) 在侧边栏与顶栏间共享。 Co-Authored-By: Claude Opus 4.8 --- src/app/(dashboard)/layout.tsx | 41 ++++++++++++++------------ src/components/app/sidebar-context.tsx | 29 ++++++++++++++++++ src/components/app/sidebar.tsx | 12 +++++++- src/components/app/topbar.tsx | 16 ++++++++-- 4 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 src/components/app/sidebar-context.tsx diff --git a/src/app/(dashboard)/layout.tsx b/src/app/(dashboard)/layout.tsx index 20d35ba..7f839b4 100644 --- a/src/app/(dashboard)/layout.tsx +++ b/src/app/(dashboard)/layout.tsx @@ -2,6 +2,7 @@ import { redirect } from "next/navigation"; import { auth } from "@/lib/auth"; import { Sidebar } from "@/components/app/sidebar"; import { Topbar } from "@/components/app/topbar"; +import { SidebarProvider } from "@/components/app/sidebar-context"; import { brand, copyright } from "@/lib/brand"; export default async function DashboardLayout({ @@ -14,25 +15,27 @@ export default async function DashboardLayout({ const u = session.user; return ( -
- -
- -
-
{children}
-
- {copyright()} - {brand.name} · {brand.tagline} -
-
+ +
+ +
+ +
+
{children}
+
+ {copyright()} + {brand.name} · {brand.tagline} +
+
+
-
+ ); } diff --git a/src/components/app/sidebar-context.tsx b/src/components/app/sidebar-context.tsx new file mode 100644 index 0000000..7661cd7 --- /dev/null +++ b/src/components/app/sidebar-context.tsx @@ -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(null); + +/** 折叠状态提供者:包裹侧边栏 + 主区,供二者共享 collapsed */ +export function SidebarProvider({ children }: { children: React.ReactNode }) { + const [collapsed, setCollapsed] = useState(false); + return ( + setCollapsed((v) => !v), setCollapsed }} + > + {children} + + ); +} + +export function useSidebar() { + const ctx = useContext(Ctx); + if (!ctx) throw new Error("useSidebar 必须在 SidebarProvider 内使用"); + return ctx; +} diff --git a/src/components/app/sidebar.tsx b/src/components/app/sidebar.tsx index d83e260..c716a55 100644 --- a/src/components/app/sidebar.tsx +++ b/src/components/app/sidebar.tsx @@ -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 ( -