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 ( -