From 12991985aee4e04c4761e64969e5da0639550174 Mon Sep 17 00:00:00 2001 From: Snowykami Date: Thu, 25 Sep 2025 12:34:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20SidebarAutoCloseOn?= =?UTF-8?q?RouteChange=20=E7=BB=84=E4=BB=B6=E4=BB=A5=E5=9C=A8=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=8F=98=E5=8C=96=E6=97=B6=E8=87=AA=E5=8A=A8=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E4=BE=A7=E8=BE=B9=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/app/console/layout.tsx | 2 ++ web/src/components/ui/sidebar.tsx | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/web/src/app/console/layout.tsx b/web/src/app/console/layout.tsx index 925167c..d8e042f 100644 --- a/web/src/app/console/layout.tsx +++ b/web/src/app/console/layout.tsx @@ -2,6 +2,7 @@ import { AppSidebar } from "@/components/console/app-sidebar" import { SiteHeader } from "@/components/console/site-header" import { + SidebarAutoCloseOnRouteChange, SidebarInset, SidebarProvider, } from "@/components/ui/sidebar" @@ -51,6 +52,7 @@ export default function ConsoleLayout({ } as React.CSSProperties } > + diff --git a/web/src/components/ui/sidebar.tsx b/web/src/components/ui/sidebar.tsx index 1ee5a45..d6cd97c 100644 --- a/web/src/components/ui/sidebar.tsx +++ b/web/src/components/ui/sidebar.tsx @@ -24,6 +24,7 @@ import { TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip" +import { usePathname } from "next/navigation" const SIDEBAR_COOKIE_NAME = "sidebar_state" const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7 @@ -698,6 +699,18 @@ function SidebarMenuSubButton({ ) } +function SidebarAutoCloseOnRouteChange() { + const { isMobile, setOpenMobile } = useSidebar() + const pathname = usePathname() ?? "/" + + React.useEffect(() => { + if (isMobile) setOpenMobile(false) // 只在 pathname 变化时执行 + }, [pathname, isMobile, setOpenMobile]) + + return null +} + + export { Sidebar, SidebarContent, @@ -723,4 +736,5 @@ export { SidebarSeparator, SidebarTrigger, useSidebar, + SidebarAutoCloseOnRouteChange, }