Skip to content

Commit 9e8dff2

Browse files
committed
refactor(web): simplify sidebar header sizing
1 parent 28fca27 commit 9e8dff2

3 files changed

Lines changed: 53 additions & 51 deletions

File tree

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

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ import {
2424
import LegacyThreadSidebar from "./LegacySidebar";
2525
import ThreadSidebar from "./Sidebar";
2626
import { SettingsSidebarNav } from "./settings/SettingsSidebarNav";
27-
import {
28-
SidebarChromeHeader,
29-
SidebarChromeIntrinsicWidthProbe,
30-
SidebarWindowControlsContext,
31-
} from "./sidebar/SidebarChrome";
27+
import { SidebarChromeHeader, SidebarChromeIntrinsicWidthProbe } from "./sidebar/SidebarChrome";
3228
import {
3329
resolveSidebarStageFocusRingOffsetClass,
3430
useSidebarStageBackdropVariant,
@@ -167,7 +163,10 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
167163
// and a clamped drag ends with an unchanged width, which skips the re-render
168164
// that would otherwise refresh a render-time snapshot.
169165
const viewportWidth = useSyncExternalStore(subscribeToViewportWidth, readViewportWidth);
170-
const sidebarMaximumWidth = resolveThreadSidebarMaximumWidth(viewportWidth, sidebarMinimumWidth);
166+
const sidebarMaximumWidth = Math.max(
167+
sidebarMinimumWidth,
168+
resolveThreadSidebarMaximumWidth(viewportWidth),
169+
);
171170
const renderedSidebarWidth = Math.min(
172171
sidebarMaximumWidth,
173172
Math.max(sidebarMinimumWidth, sidebarWidth),
@@ -178,7 +177,9 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
178177
} catch (error) {
179178
console.error("Could not clear persisted thread sidebar width.", error);
180179
}
181-
setSidebarWidth(resolveInitialThreadSidebarWidth(null, viewportWidth, sidebarMinimumWidth));
180+
setSidebarWidth(
181+
Math.max(sidebarMinimumWidth, resolveInitialThreadSidebarWidth(null, viewportWidth)),
182+
);
182183
};
183184
const [isWindowFullscreen, setIsWindowFullscreen] = useState(() => {
184185
const getWindowFullscreenState = window.desktopBridge?.getWindowFullscreenState;
@@ -243,35 +244,39 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
243244
<SidebarChromeIntrinsicWidthProbe onWidthChange={updateSidebarMinimumWidth} />
244245
) : null}
245246
<ProjectProjectionRetention />
246-
<SidebarWindowControlsContext value={isMacosDesktop && !isWindowFullscreen}>
247-
<Sidebar
248-
side="left"
249-
collapsible="offcanvas"
250-
data-app-sidebar=""
251-
className="border-r border-sidebar-border bg-sidebar text-sidebar-foreground"
252-
resizable={{
253-
maxWidth: sidebarMaximumWidth,
254-
minWidth: sidebarMinimumWidth,
255-
shouldAcceptWidth: ({ currentWidth, nextWidth, wrapper }) =>
256-
nextWidth <= currentWidth ||
257-
wrapper.clientWidth - nextWidth >= THREAD_MAIN_CONTENT_MIN_WIDTH,
258-
storageKey: THREAD_SIDEBAR_WIDTH_STORAGE_KEY,
259-
onResize: setSidebarWidth,
260-
}}
261-
>
262-
{isOnSettings ? (
263-
<>
264-
<SidebarChromeHeader isElectron={isElectron} />
265-
<SettingsSidebarNav pathname={pathname} />
266-
</>
267-
) : legacySidebarEnabled ? (
268-
<LegacyThreadSidebar />
269-
) : (
270-
<ThreadSidebar />
271-
)}
272-
<SidebarRail onDoubleClick={resetSidebarWidth} />
273-
</Sidebar>
274-
</SidebarWindowControlsContext>
247+
{/* The drawer is portaled, so its header inset must be set on the popup. */}
248+
<Sidebar
249+
side="left"
250+
collapsible="offcanvas"
251+
data-app-sidebar=""
252+
className={cn(
253+
"border-r border-sidebar-border bg-sidebar text-sidebar-foreground",
254+
isMacosDesktop &&
255+
!isWindowFullscreen &&
256+
"[--sidebar-drawer-controls-inset:var(--desktop-window-controls-inset,90px)]",
257+
)}
258+
resizable={{
259+
maxWidth: sidebarMaximumWidth,
260+
minWidth: sidebarMinimumWidth,
261+
shouldAcceptWidth: ({ currentWidth, nextWidth, wrapper }) =>
262+
nextWidth <= currentWidth ||
263+
wrapper.clientWidth - nextWidth >= THREAD_MAIN_CONTENT_MIN_WIDTH,
264+
storageKey: THREAD_SIDEBAR_WIDTH_STORAGE_KEY,
265+
onResize: setSidebarWidth,
266+
}}
267+
>
268+
{isOnSettings ? (
269+
<>
270+
<SidebarChromeHeader isElectron={isElectron} />
271+
<SettingsSidebarNav pathname={pathname} />
272+
</>
273+
) : legacySidebarEnabled ? (
274+
<LegacyThreadSidebar />
275+
) : (
276+
<ThreadSidebar />
277+
)}
278+
<SidebarRail onDoubleClick={resetSidebarWidth} />
279+
</Sidebar>
275280
{children}
276281
<SidebarControl />
277282
</SidebarProvider>

‎apps/web/src/components/sidebar/SidebarChrome.tsx‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ArrowLeftIcon, ChartNoAxesColumnIcon, SettingsIcon } from "lucide-react";
22
import type { ReactNode } from "react";
3-
import { createContext, memo, use, useCallback, useLayoutEffect, useRef } from "react";
3+
import { memo, useCallback, useLayoutEffect, useRef } from "react";
44
import { Link, useCanGoBack, useLocation, useNavigate } from "@tanstack/react-router";
55

66
import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
@@ -30,15 +30,11 @@ import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
3030
import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
3131
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
3232

33-
// Sheet headers render through a portal, so they cannot inherit SidebarProvider's layout inset.
34-
export const SidebarWindowControlsContext = createContext(false);
35-
3633
export const SidebarChromeHeader = memo(function SidebarChromeHeader({
3734
isElectron,
3835
}: {
3936
isElectron: boolean;
4037
}) {
41-
const reserveWindowControls = use(SidebarWindowControlsContext);
4238
const stageLabel = useEnvironmentStageLabel();
4339
const environmentIdentificationMode = useEnvironmentIdentificationMode();
4440
const backdropVariant = resolveSidebarStageBackdropVariant(
@@ -53,9 +49,8 @@ export const SidebarChromeHeader = memo(function SidebarChromeHeader({
5349
return (
5450
<SidebarHeader
5551
className={cn(
56-
"@container/sidebar-header relative h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center px-3 py-0 md:pl-0",
52+
"@container/sidebar-header relative h-[var(--workspace-topbar-height)] shrink-0 flex-row items-center py-0 pr-3 pl-[var(--sidebar-drawer-controls-inset,0.75rem)] md:pl-0",
5753
isElectron && "drag-region",
58-
reserveWindowControls && "max-md:pl-[var(--desktop-window-controls-inset,90px)]",
5954
)}
6055
>
6156
{backdropVariant ? <SidebarStageBackdrop variant={backdropVariant} /> : null}

‎apps/web/src/components/threadSidebarWidth.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ const THREAD_SIDEBAR_DEFAULT_WIDTH = 16 * 16;
33
export const THREAD_SIDEBAR_MIN_WIDTH = 13 * 16;
44
export const THREAD_MAIN_CONTENT_MIN_WIDTH = 40 * 16;
55

6-
export function resolveThreadSidebarMaximumWidth(
7-
viewportWidth: number,
8-
minimumWidth = THREAD_SIDEBAR_MIN_WIDTH,
9-
): number {
10-
return Math.max(minimumWidth, Math.floor(viewportWidth) - THREAD_MAIN_CONTENT_MIN_WIDTH);
6+
export function resolveThreadSidebarMaximumWidth(viewportWidth: number): number {
7+
return Math.max(
8+
THREAD_SIDEBAR_MIN_WIDTH,
9+
Math.floor(viewportWidth) - THREAD_MAIN_CONTENT_MIN_WIDTH,
10+
);
1111
}
1212

1313
export function resolveInitialThreadSidebarWidth(
1414
storedWidth: number | null,
1515
viewportWidth: number,
16-
minimumWidth = THREAD_SIDEBAR_MIN_WIDTH,
1716
): number {
18-
const preferredWidth = Math.max(minimumWidth, storedWidth ?? THREAD_SIDEBAR_DEFAULT_WIDTH);
19-
return Math.min(preferredWidth, resolveThreadSidebarMaximumWidth(viewportWidth, minimumWidth));
17+
const preferredWidth =
18+
storedWidth === null
19+
? THREAD_SIDEBAR_DEFAULT_WIDTH
20+
: Math.max(THREAD_SIDEBAR_MIN_WIDTH, storedWidth);
21+
return Math.min(preferredWidth, resolveThreadSidebarMaximumWidth(viewportWidth));
2022
}

0 commit comments

Comments
 (0)