mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
feat: 添加 SidebarAutoCloseOnRouteChange 组件以在路由变化时自动关闭侧边栏
This commit is contained in:
@ -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
|
||||
}
|
||||
>
|
||||
<SidebarAutoCloseOnRouteChange />
|
||||
<AppSidebar variant="inset" />
|
||||
<SidebarInset>
|
||||
<SiteHeader title={title} />
|
||||
|
@ -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,
|
||||
}
|
||||
|
Reference in New Issue
Block a user