Skip to content

Commit

Permalink
feat: add persistent cookie sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
mfts committed Jan 18, 2025
1 parent 4c77782 commit dad16bb
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion components/ui/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,22 @@ const SidebarProvider = React.forwardRef<
const isMobile = useIsMobile();
const [openMobile, setOpenMobile] = React.useState(false);

// Read initial state from cookie
const initialState = React.useMemo(() => {
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(initialState);
const open = openProp ?? _open;
const setOpen = React.useCallback(
(value: boolean | ((value: boolean) => boolean)) => {
Expand Down

0 comments on commit dad16bb

Please sign in to comment.