feat: add initial state management for Sidebar from cookie
This commit is contained in:
@@ -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)) => {
|
||||
|
||||
Reference in New Issue
Block a user