From 6afe30790206530d32328a43e65e913cade1c015 Mon Sep 17 00:00:00 2001 From: Donald Silveia Date: Fri, 13 Mar 2026 10:58:17 -0300 Subject: [PATCH] fix(web): restore diff sidebar width after resize --- apps/web/src/components/ui/sidebar.tsx | 46 ++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/apps/web/src/components/ui/sidebar.tsx b/apps/web/src/components/ui/sidebar.tsx index 15b8430c8..bae444f44 100644 --- a/apps/web/src/components/ui/sidebar.tsx +++ b/apps/web/src/components/ui/sidebar.tsx @@ -18,7 +18,11 @@ import { import { Skeleton } from "~/components/ui/skeleton"; import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; import { useIsMobile } from "~/hooks/useMediaQuery"; -import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage"; +import { + getLocalStorageItem, + removeLocalStorageItem, + setLocalStorageItem, +} from "~/hooks/useLocalStorage"; import { Schema } from "effect"; const SIDEBAR_COOKIE_NAME = "sidebar_state"; @@ -542,13 +546,41 @@ function SidebarRail({ if (!rail) return; const wrapper = rail.closest("[data-slot='sidebar-wrapper']"); if (!wrapper) return; + const defaultSidebarWidth = wrapper.style.getPropertyValue("--sidebar-width"); + + const applyStoredWidth = () => { + const storedWidth = getLocalStorageItem(resolvedResizable.storageKey!, Schema.Finite); + if (storedWidth === null) return; + const clampedWidth = clampSidebarWidth(storedWidth, resolvedResizable); + const sidebarContainer = rail + .closest("[data-slot='sidebar']") + ?.querySelector("[data-slot='sidebar-container']"); + const accepted = + !sidebarContainer || + !resolvedResizable.shouldAcceptWidth || + resolvedResizable.shouldAcceptWidth({ + currentWidth: sidebarContainer.getBoundingClientRect().width, + nextWidth: clampedWidth, + rail, + side: sidebarInstance?.side ?? "left", + sidebarRoot: rail.closest("[data-slot='sidebar']")!, + wrapper, + }); + if (accepted) { + wrapper.style.setProperty("--sidebar-width", `${clampedWidth}px`); + resolvedResizable.onResize?.(clampedWidth); + } else { + wrapper.style.setProperty("--sidebar-width", defaultSidebarWidth); + removeLocalStorageItem(resolvedResizable.storageKey!); + } + }; - const storedWidth = getLocalStorageItem(resolvedResizable.storageKey, Schema.Finite); - if (storedWidth === null) return; - const clampedWidth = clampSidebarWidth(storedWidth, resolvedResizable); - wrapper.style.setProperty("--sidebar-width", `${clampedWidth}px`); - resolvedResizable.onResize?.(clampedWidth); - }, [resolvedResizable]); + applyStoredWidth(); + window.addEventListener("resize", applyStoredWidth); + return () => { + window.removeEventListener("resize", applyStoredWidth); + }; + }, [resolvedResizable, sidebarInstance?.side]); React.useEffect(() => { return () => {