feat: add initial state management for Sidebar from cookie

This commit is contained in:
JSC
2025-08-07 17:02:34 +02:00
parent 671d034b9f
commit b43d29e862

View File

@@ -69,9 +69,25 @@ function SidebarProvider({
const isMobile = useIsMobile()
const [openMobile, setOpenMobile] = React.useState(false)
// Read initial state from cookie
const getInitialState = React.useCallback(() => {
if (typeof window === "undefined") return defaultOpen
const cookie = document.cookie
.split("; ")
.find((row) => row.startsWith(`${SIDEBAR_COOKIE_NAME}=`))
if (cookie) {
const value = cookie.split("=")[1]
return value === "true"
}
return defaultOpen
}, [defaultOpen])
// This is the internal state of the sidebar.
// We use openProp and setOpenProp for control from outside the component.
const [_open, _setOpen] = React.useState(defaultOpen)
const [_open, _setOpen] = React.useState(getInitialState)
const open = openProp ?? _open
const setOpen = React.useCallback(
(value: boolean | ((value: boolean) => boolean)) => {