From b43d29e86200861f038b1627d9302f191e8688d2 Mon Sep 17 00:00:00 2001 From: JSC Date: Thu, 7 Aug 2025 17:02:34 +0200 Subject: [PATCH] feat: add initial state management for Sidebar from cookie --- src/components/ui/sidebar.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/components/ui/sidebar.tsx b/src/components/ui/sidebar.tsx index 30638ac..30b51cd 100644 --- a/src/components/ui/sidebar.tsx +++ b/src/components/ui/sidebar.tsx @@ -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)) => {