Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(sidebar): move the viewport clamp into the provider

M3gA-Mind4657c9f8ab16
diff --git a/app/src/components/ui/Sidebar.tsx b/app/src/components/ui/Sidebar.tsx
index 85ec9ede1a..92619a1fa0 100644
--- a/app/src/components/ui/Sidebar.tsx
+++ b/app/src/components/ui/Sidebar.tsx
@@ -87,6 +87,54 @@ function clampWidth(width: number, min: number, max: number): number {
return Math.min(Math.max(width, min), max);
}
+/**
+ * The widest the sidebar may render at, for a given viewport (#5907).
+ *
+ * Half the window, floored — so the column can never own more than half a
+ * narrow one. Inert on any real desktop: it only bites below `2 * maxWidth`,
+ * i.e. an 840px window at the default 420px cap.
+ *
+ * `min` wins over this on purpose. A window narrower than `2 * min` is past the
+ * point where a fraction is meaningful, and returning something below `min`
+ * would fight `clampWidth`'s floor and make the two disagree.
+ */
+function viewportCap(viewportWidth: number, min: number): number {
+ return Math.max(min, Math.floor(viewportWidth / 2));
+}
+
+/**
+ * The current window width, tracked across resizes.
+ *
+ * A listener rather than a CSS `max-width`, and the distinction is the whole
+ * point (#5941, Codex). A CSS-only clamp renders the right number while every
+ * consumer of the stored one — the rail's drag arithmetic, the arrow-key step,
+ * `aria-valuenow` — keeps reading the larger value. With a persisted 420 in a
+ * 414px viewport that renders a 207px column the rail cannot narrow at all:
+ * dragging fully left proposes ~213, `max-width` still pins 207, and the
+ * separator announces 420 for a 207px column. Clamping the value instead means
+ * there is one width and everything agrees with it.
+ *
+ * `Infinity` before mount so the cap is inert until a real measurement exists —
+ * a 0 here would collapse the sidebar to `min` for one frame.
+ */
+function useViewportWidth(): number {
+ const [viewportWidth, setViewportWidth] = useState(() =>
+ typeof window === 'undefined' ? Number.POSITIVE_INFINITY : window.innerWidth
+ );
+
+ useEffect(() => {
+ if (typeof window === 'undefined') return;
+ const onResize = () => setViewportWidth(window.innerWidth);
+ // Sync once on mount: the lazy initial state above runs before any resize
+ // that happened between module evaluation and this effect.
+ onResize();
+ window.addEventListener('resize', onResize);
+ return () => window.removeEventListener('resize', onResize);
+ }, []);
+
+ return viewportWidth;
+}
+
export interface SidebarProviderProps extends HTMLAttributes<HTMLDivElement> {
/** Controlled open state. Omit for uncontrolled. */
open?: boolean;
@@ -133,7 +181,14 @@ export const SidebarProvider = forwardRef<HTMLDivElement, SidebarProviderProps>(
const [uncontrolledWidth, setUncontrolledWidth] = useState(() =>
clampWidth(defaultWidth, minWidth, maxWidth)
);
- const width = clampWidth(widthProp ?? uncontrolledWidth, minWidth, maxWidth);
+ // One effective width, viewport included, handed to everything through
+ // context: the rendered column, the rail's drag origin, its arrow-key step
+ // and its `aria-valuenow`. The STORED value keeps the user's preference
+ // untouched — widening the window restores it — but nothing reads the
+ // stored value directly, so no consumer can disagree with what is on screen.
+ const viewportWidth = useViewportWidth();
+ const effectiveMax = Math.min(maxWidth, viewportCap(viewportWidth, minWidth));
+ const width = clampWidth(widthProp ?? uncontrolledWidth, minWidth, effectiveMax);
const setOpen = useCallback(
(next: boolean) => {
@@ -226,24 +281,6 @@ export const Sidebar = forwardRef<HTMLDivElement, SidebarProps>(
data-collapsible={collapsible}
className={cn(
'flex h-full min-h-0 min-w-0 flex-none flex-col overflow-hidden text-content',
- // Viewport clamp (#5907). The stored width is clamped against
- // SIDEBAR_MIN_WIDTH/SIDEBAR_MAX_WIDTH only, never against the window,
- // so a narrow window left the sidebar owning most of it — 224px of a
- // 414px viewport, 54%, with a hard 188px floor below that.
- //
- // `max-w-[50vw]` rather than a resize listener, deliberately. The
- // width arrives as an inline style, and `max-width` always constrains
- // `width`, so the browser applies this continuously with no listener,
- // no re-render and no extra state. A JS clamp would need both halves:
- // the arithmetic AND something that re-renders on resize, because
- // `clamp()` in RootShellLayout only runs at render and nothing in the
- // shell listens for `resize` — measured, by injecting a viewport clamp
- // there and observing no change at all.
- //
- // Inert on desktop: 50vw exceeds SIDEBAR_MAX_WIDTH (420) above an
- // 840px window, so this can only engage on a genuinely narrow one.
- // The collapsed icon column is far below it and unaffected.
- 'max-w-[50vw]',
className
)}
style={{ width: collapsed ? SIDEBAR_ICON_WIDTH : width, ...(style ?? {}) }}
diff --git a/app/src/components/ui/Sidebar.viewport-clamp.test.tsx b/app/src/components/ui/Sidebar.viewport-clamp.test.tsx
new file mode 100644
index 0000000000..9375dbf409
--- /dev/null
+++ b/app/src/components/ui/Sidebar.viewport-clamp.test.tsx
@@ -0,0 +1,99 @@
+import { act, render, screen } from '@testing-library/react';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { Sidebar, SidebarProvider, SidebarRail } from './Sidebar';
+
+/**
+ * The viewport clamp (#5907) and the single-source-of-truth property that
+ * makes it correct (#5941).
+ *
+ * The sidebar width was clamped against SIDEBAR_MIN_WIDTH/SIDEBAR_MAX_WIDTH and
+ * never against the window, so a narrow window left the column owning most of
+ * it. The first fix was a CSS `max-w-[50vw]`, which review correctly rejected:
+ * CSS constrains the *rendered* box while every consumer of the stored value —
+ * the rail's drag origin, its arrow-key step, its `aria-valuenow` — keeps
+ * reading the larger number. With a persisted 420 in a 414px viewport the
+ * column renders 207px, dragging fully left proposes ~213 so `max-width` pins
+ * it at 207, and the sidebar cannot be narrowed at all.
+ *
+ * The clamp now lives in `SidebarProvider`, so context hands one width to
+ * everything. These tests assert exactly that agreement — the rendered box AND
+ * the separator's reported value, together, because the defect was the two
+ * disagreeing.
+ */
+
+const ORIGINAL_INNER_WIDTH = window.innerWidth;
+
+function setViewport(width: number) {
+ Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: width });
+ act(() => {
+ window.dispatchEvent(new Event('resize'));
+ });
+}
+
+afterEach(() => {
+ Object.defineProperty(window, 'innerWidth', {
+ configurable: true,
+ writable: true,
+ value: ORIGINAL_INNER_WIDTH,
+ });
+});
+
+const renderSidebar = (width: number) =>
+ render(
+ <SidebarProvider defaultWidth={width}>
+ <Sidebar data-testid="sidebar" collapsible="none" />
+ <SidebarRail data-testid="rail" aria-label="Resize sidebar" />
+ </SidebarProvider>
+ );
+
+describe('Sidebar viewport clamp', () => {
+ it('is inert at a desktop width — the stored width decides', () => {
+ setViewport(1280);
+ renderSidebar(420);
+
+ // 50% of 1280 is 640, well above the 420 cap, so the clamp cannot bind.
+ expect(screen.getByTestId('sidebar')).toHaveStyle({ width: '420px' });
+ expect(screen.getByTestId('rail')).toHaveAttribute('aria-valuenow', '420');
+ });
+
+ it('caps the column at half a narrow window', () => {
+ setViewport(414);
+ renderSidebar(420);
+
+ // floor(414 / 2) = 207.
+ expect(screen.getByTestId('sidebar')).toHaveStyle({ width: '207px' });
+ });
+
+ it('reports the clamped width to assistive tech, not the stored one', () => {
+ // The half of #5941 that a CSS-only clamp could never have fixed: the
+ // separator announced 420 for a 207px column.
+ setViewport(414);
+ renderSidebar(420);
+
+ expect(screen.getByTestId('rail')).toHaveAttribute('aria-valuenow', '207');
+ });
+
+ it('never goes below the minimum, however narrow the window', () => {
+ // Below 2 * minWidth a fraction stops being meaningful; the floor wins so
+ // the two clamps cannot disagree.
+ setViewport(200);
+ renderSidebar(420);
+
+ expect(screen.getByTestId('sidebar')).toHaveStyle({ width: '188px' });
+ });
+
+ it('re-clamps when the window is resized, and restores on widening', () => {
+ // The listener is the reason this is not a CSS rule. It also shows the
+ // stored preference surviving: 420 comes back when there is room for it.
+ setViewport(1280);
+ renderSidebar(420);
+ expect(screen.getByTestId('sidebar')).toHaveStyle({ width: '420px' });
+
+ setViewport(414);
+ expect(screen.getByTestId('sidebar')).toHaveStyle({ width: '207px' });
+
+ setViewport(1280);
+ expect(screen.getByTestId('sidebar')).toHaveStyle({ width: '420px' });
+ });
+});
diff --git a/app/test/playwright/specs/app-shell-responsive.spec.ts b/app/test/playwright/specs/app-shell-responsive.spec.ts
index 14ab2374c6..9989fb3a62 100644
--- a/app/test/playwright/specs/app-shell-responsive.spec.ts
+++ b/app/test/playwright/specs/app-shell-responsive.spec.ts
@@ -108,13 +108,17 @@ test.describe('App shell — narrow viewports', () => {
await page.setViewportSize({ width: 1280, height: 900 });
await expect.poll(() => page.evaluate(() => window.innerWidth)).toBe(1280);
+ // No early return here. Returning on a null or zero box let a failed
+ // render or a collapsed sidebar pass as success — the very outcome this
+ // test exists to notice (#5941, CodeRabbit).
const box = await sidebar(page).boundingBox();
- if (box === null || box.width === 0) return;
+ expect(box, 'sidebar has no layout box at 1280px — it did not render').not.toBeNull();
+ expect(box!.width).toBeGreaterThan(0);
// Well clear of the 640px the clamp would allow here, and at or above the
// 188px floor — i.e. the clamp is not what is deciding this width.
- expect(box.width).toBeGreaterThanOrEqual(188);
- expect(box.width).toBeLessThan(640);
+ expect(box!.width).toBeGreaterThanOrEqual(188);
+ expect(box!.width).toBeLessThan(640);
});
test('resizing back to full width restores the layout', async ({ page }) => {