feat: 侧边栏可点击折叠/展开
顶栏左侧新增切换按钮,点击后左侧菜单在 w-60↔w-0 间平滑伸缩隐藏。 折叠状态通过 SidebarProvider(context) 在侧边栏与顶栏间共享。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -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,6 +15,7 @@ export default async function DashboardLayout({
|
||||
const u = session.user;
|
||||
|
||||
return (
|
||||
<SidebarProvider>
|
||||
<div className="flex h-screen overflow-hidden">
|
||||
<Sidebar />
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
@@ -34,5 +36,6 @@ export default async function DashboardLayout({
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</SidebarProvider>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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">
|
||||
<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
|
||||
|
||||
Reference in New Issue
Block a user