Skip to content

Commit efe6301

Browse files
Track viewport width for live sidebar resize limits
1 parent 96398e3 commit efe6301

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

‎apps/web/src/components/AppSidebarLayout.tsx‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import * as Schema from "effect/Schema";
3-
import { useEffect, useState, type CSSProperties, type ReactNode } from "react";
3+
import {
4+
useEffect,
5+
useState,
6+
useSyncExternalStore,
7+
type CSSProperties,
8+
type ReactNode,
9+
} from "react";
410
import { useLocation, useNavigate } from "@tanstack/react-router";
511

612
import { isElectron } from "../env";
@@ -31,6 +37,15 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
3137

3238
const MACOS_TRAFFIC_LIGHTS_LEFT_INSET = "90px";
3339

40+
function subscribeToViewportWidth(onChange: () => void): () => void {
41+
window.addEventListener("resize", onChange);
42+
return () => window.removeEventListener("resize", onChange);
43+
}
44+
45+
function readViewportWidth(): number {
46+
return window.innerWidth;
47+
}
48+
3449
function readInitialThreadSidebarWidth(): number {
3550
try {
3651
return resolveInitialThreadSidebarWidth(
@@ -109,7 +124,11 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
109124
const useSidebarV2Theme = useSidebarV2 || isOnSettings;
110125
const isMacosDesktop = isElectron && isMacPlatform(navigator.platform);
111126
const [sidebarWidth, setSidebarWidth] = useState(readInitialThreadSidebarWidth);
112-
const sidebarMaximumWidth = resolveThreadSidebarMaximumWidth(window.innerWidth);
127+
// Subscribed rather than read once: the clamp must track live window size,
128+
// and a clamped drag ends with an unchanged width, which skips the re-render
129+
// that would otherwise refresh a render-time snapshot.
130+
const viewportWidth = useSyncExternalStore(subscribeToViewportWidth, readViewportWidth);
131+
const sidebarMaximumWidth = resolveThreadSidebarMaximumWidth(viewportWidth);
113132
const [isWindowFullscreen, setIsWindowFullscreen] = useState(() => {
114133
const getWindowFullscreenState = window.desktopBridge?.getWindowFullscreenState;
115134
return isMacosDesktop && typeof getWindowFullscreenState === "function"

0 commit comments

Comments
 (0)