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 { auth } from "@/lib/auth";
|
||||||
import { Sidebar } from "@/components/app/sidebar";
|
import { Sidebar } from "@/components/app/sidebar";
|
||||||
import { Topbar } from "@/components/app/topbar";
|
import { Topbar } from "@/components/app/topbar";
|
||||||
|
import { SidebarProvider } from "@/components/app/sidebar-context";
|
||||||
import { brand, copyright } from "@/lib/brand";
|
import { brand, copyright } from "@/lib/brand";
|
||||||
|
|
||||||
export default async function DashboardLayout({
|
export default async function DashboardLayout({
|
||||||
@@ -14,25 +15,27 @@ export default async function DashboardLayout({
|
|||||||
const u = session.user;
|
const u = session.user;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen overflow-hidden">
|
<SidebarProvider>
|
||||||
<Sidebar />
|
<div className="flex h-screen overflow-hidden">
|
||||||
<div className="flex min-w-0 flex-1 flex-col">
|
<Sidebar />
|
||||||
<Topbar
|
<div className="flex min-w-0 flex-1 flex-col">
|
||||||
user={{
|
<Topbar
|
||||||
name: u.name,
|
user={{
|
||||||
username: u.username,
|
name: u.name,
|
||||||
roles: u.roles,
|
username: u.username,
|
||||||
image: null, // 旧 face 为老站点相对路径,统一用文字头像
|
roles: u.roles,
|
||||||
}}
|
image: null, // 旧 face 为老站点相对路径,统一用文字头像
|
||||||
/>
|
}}
|
||||||
<main className="flex-1 overflow-y-auto">
|
/>
|
||||||
<div className="p-6">{children}</div>
|
<main className="flex-1 overflow-y-auto">
|
||||||
<footer className="flex items-center justify-between border-t px-6 py-4 text-xs text-[var(--color-muted-foreground)]">
|
<div className="p-6">{children}</div>
|
||||||
<span>{copyright()}</span>
|
<footer className="flex items-center justify-between border-t px-6 py-4 text-xs text-[var(--color-muted-foreground)]">
|
||||||
<span>{brand.name} · {brand.tagline}</span>
|
<span>{copyright()}</span>
|
||||||
</footer>
|
<span>{brand.name} · {brand.tagline}</span>
|
||||||
</main>
|
</footer>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
</div>
|
</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 { copyright } from "@/lib/brand";
|
||||||
import { BrandLogo } from "./brand-logo";
|
import { BrandLogo } from "./brand-logo";
|
||||||
import { navSections, activeHref } from "./nav";
|
import { navSections, activeHref } from "./nav";
|
||||||
|
import { useSidebar } from "./sidebar-context";
|
||||||
|
|
||||||
export function Sidebar() {
|
export function Sidebar() {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const active = activeHref(pathname);
|
const active = activeHref(pathname);
|
||||||
|
const { collapsed } = useSidebar();
|
||||||
|
|
||||||
return (
|
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">
|
<div className="flex h-16 items-center px-5">
|
||||||
<Link href="/">
|
<Link href="/">
|
||||||
@@ -54,6 +63,7 @@ export function Sidebar() {
|
|||||||
<div className="px-5 py-4 text-[11px] text-[var(--color-sidebar-muted)]">
|
<div className="px-5 py-4 text-[11px] text-[var(--color-sidebar-muted)]">
|
||||||
{copyright()}
|
{copyright()}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</aside>
|
</aside>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import { useEffect, useRef, useState } from "react";
|
|||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { usePathname } from "next/navigation";
|
import { usePathname } from "next/navigation";
|
||||||
import { signOut } from "next-auth/react";
|
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 { Avatar } from "@/components/ui/avatar";
|
||||||
import { navSections } from "./nav";
|
import { navSections } from "./nav";
|
||||||
|
import { useSidebar } from "./sidebar-context";
|
||||||
|
|
||||||
function pageTitle(pathname: string): string {
|
function pageTitle(pathname: string): string {
|
||||||
if (pathname === "/") return "控制台";
|
if (pathname === "/") return "控制台";
|
||||||
@@ -28,6 +29,7 @@ export function Topbar({
|
|||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
const { toggle } = useSidebar();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function onClick(e: MouseEvent) {
|
function onClick(e: MouseEvent) {
|
||||||
@@ -41,7 +43,17 @@ export function Topbar({
|
|||||||
|
|
||||||
return (
|
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">
|
<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}>
|
<div className="relative" ref={ref}>
|
||||||
<button
|
<button
|
||||||
|
|||||||
Reference in New Issue
Block a user