Skip to content

Commit 6464dbc

Browse files
committed
fix(web): restore diff sidebar width after resize
1 parent dcd2e5c commit 6464dbc

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

apps/web/src/components/ui/sidebar.tsx

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
import { Skeleton } from "~/components/ui/skeleton";
1919
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
2020
import { useMediaQuery } from "~/hooks/useMediaQuery";
21-
import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage";
21+
import {
22+
getLocalStorageItem,
23+
removeLocalStorageItem,
24+
setLocalStorageItem,
25+
} from "~/hooks/useLocalStorage";
2226
import { Schema } from "effect";
2327

2428
const SIDEBAR_COOKIE_NAME = "sidebar_state";
@@ -542,13 +546,41 @@ function SidebarRail({
542546
if (!rail) return;
543547
const wrapper = rail.closest<HTMLElement>("[data-slot='sidebar-wrapper']");
544548
if (!wrapper) return;
549+
const defaultSidebarWidth = wrapper.style.getPropertyValue("--sidebar-width");
550+
551+
const applyStoredWidth = () => {
552+
const storedWidth = getLocalStorageItem(resolvedResizable.storageKey!, Schema.Finite);
553+
if (storedWidth === null) return;
554+
const clampedWidth = clampSidebarWidth(storedWidth, resolvedResizable);
555+
const sidebarContainer = rail
556+
.closest<HTMLElement>("[data-slot='sidebar']")
557+
?.querySelector<HTMLElement>("[data-slot='sidebar-container']");
558+
const accepted =
559+
!sidebarContainer ||
560+
!resolvedResizable.shouldAcceptWidth ||
561+
resolvedResizable.shouldAcceptWidth({
562+
currentWidth: sidebarContainer.getBoundingClientRect().width,
563+
nextWidth: clampedWidth,
564+
rail,
565+
side: sidebarInstance?.side ?? "left",
566+
sidebarRoot: rail.closest<HTMLElement>("[data-slot='sidebar']")!,
567+
wrapper,
568+
});
569+
if (accepted) {
570+
wrapper.style.setProperty("--sidebar-width", `${clampedWidth}px`);
571+
resolvedResizable.onResize?.(clampedWidth);
572+
} else {
573+
wrapper.style.setProperty("--sidebar-width", defaultSidebarWidth);
574+
removeLocalStorageItem(resolvedResizable.storageKey!);
575+
}
576+
};
545577

546-
const storedWidth = getLocalStorageItem(resolvedResizable.storageKey, Schema.Finite);
547-
if (storedWidth === null) return;
548-
const clampedWidth = clampSidebarWidth(storedWidth, resolvedResizable);
549-
wrapper.style.setProperty("--sidebar-width", `${clampedWidth}px`);
550-
resolvedResizable.onResize?.(clampedWidth);
551-
}, [resolvedResizable]);
578+
applyStoredWidth();
579+
window.addEventListener("resize", applyStoredWidth);
580+
return () => {
581+
window.removeEventListener("resize", applyStoredWidth);
582+
};
583+
}, [resolvedResizable, sidebarInstance?.side]);
552584

553585
React.useEffect(() => {
554586
return () => {

0 commit comments

Comments
 (0)