mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-07 11:16:37 +00:00
remember sidebar expand
This commit is contained in:
@@ -71,20 +71,42 @@ function CollapsibleNavItem({
|
|||||||
build,
|
build,
|
||||||
isUnlocked
|
isUnlocked
|
||||||
}: CollapsibleNavItemProps) {
|
}: CollapsibleNavItemProps) {
|
||||||
const [isOpen, setIsOpen] = React.useState(isChildActive);
|
const storageKey = `pangolin-sidebar-expanded-${item.title}`;
|
||||||
|
|
||||||
|
// Get initial state from localStorage or use isChildActive
|
||||||
|
const getInitialState = (): boolean => {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return isChildActive;
|
||||||
|
}
|
||||||
|
const saved = localStorage.getItem(storageKey);
|
||||||
|
if (saved !== null) {
|
||||||
|
return saved === "true";
|
||||||
|
}
|
||||||
|
return isChildActive;
|
||||||
|
};
|
||||||
|
|
||||||
// Update open state when child active state changes
|
const [isOpen, setIsOpen] = React.useState(getInitialState);
|
||||||
|
|
||||||
|
// Update open state when child active state changes (but don't override user preference)
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (isChildActive) {
|
if (isChildActive) {
|
||||||
setIsOpen(true);
|
setIsOpen(true);
|
||||||
}
|
}
|
||||||
}, [isChildActive]);
|
}, [isChildActive]);
|
||||||
|
|
||||||
|
// Save state to localStorage when it changes
|
||||||
|
const handleOpenChange = (open: boolean) => {
|
||||||
|
setIsOpen(open);
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
localStorage.setItem(storageKey, String(open));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Collapsible
|
<Collapsible
|
||||||
key={item.title}
|
key={item.title}
|
||||||
open={isOpen}
|
open={isOpen}
|
||||||
onOpenChange={setIsOpen}
|
onOpenChange={handleOpenChange}
|
||||||
className="group/collapsible"
|
className="group/collapsible"
|
||||||
>
|
>
|
||||||
<CollapsibleTrigger asChild>
|
<CollapsibleTrigger asChild>
|
||||||
|
|||||||
Reference in New Issue
Block a user