fix(web): reserve sibling column width when resizing the right panel (#6279)

This commit is contained in:
Vividh Mahajan authored and GitHub committed 2026-08-15 12:01:47 +00:00
1 parent 143f713c79
commit 06dd9993b6
2 files changed
+90 -10

No files matched your search

@@ -20,4 +20,30 @@ describe("getPreviewPanelMaxWidth", () => {
expect(markup).toContain("max-w-full");
});
it("reserves the sibling column minimum when the flex row is known", () => {
// Fullscreen 14" MacBook: viewport 1512, sidebar ~256 → row of 1256.
// The 70% fraction (1058) would leave the chat column only ~198px;
// the container clamp caps the panel at 1256 − 360 instead.
expect(getPreviewPanelMaxWidth(1_512, 1_256)).toBe(896);
});
it("keeps the fraction cap when the row is wide enough for both columns", () => {
expect(getPreviewPanelMaxWidth(3_000, 2_900)).toBe(2_100);
});
it("rounds fractional row widths down", () => {
expect(getPreviewPanelMaxWidth(1_512, 1_256.6)).toBe(896);
});
it("never drops below the panel minimum when the row cannot fit both columns", () => {
// ~1000px window with an expanded sidebar → row of 700. The sibling
// reservation (700 − 360 = 340) would undercut the panel's own 360
// minimum and invert the resize clamp, so the floor wins.
expect(getPreviewPanelMaxWidth(1_000, 700)).toBe(360);
});
it("stays at the panel minimum even when the row is narrower than the reservation", () => {
expect(getPreviewPanelMaxWidth(1_512, 300)).toBe(360);
});
});
@@ -1,4 +1,11 @@
import { type ReactNode, useEffect, useState } from "react";
import {
type ReactNode,
type RefObject,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import { isElectron } from "~/env";
import { useResizableWidth } from "~/hooks/useResizableWidth";
@@ -10,12 +17,31 @@ export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
const PREVIEW_PANEL_WIDTH_STORAGE_KEY = "t3code:preview-panel-width";
const PREVIEW_PANEL_MIN_WIDTH = 360;
/** Fraction of the viewport allowed, preserving the remaining space for chat. */
/**
* Upper bound as a fraction of the viewport; only binds on wide screens.
* On narrow windows the container clamp below is what preserves the
* sibling column's space.
*/
const PREVIEW_PANEL_MAX_WIDTH_FRACTION = 0.7;
const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
/**
* Width reserved for the sibling column (chat, pull-request list) sharing the
* panel's flex row. The viewport fraction alone is not enough: the app
* sidebar sits outside the row, so on narrow windows (any MacBook, even
* fullscreen) the remaining 30% of the viewport minus the sidebar left the
* sibling below its usable width and the composer overflowed.
*/
const SIBLING_COLUMN_MIN_WIDTH = 360;
export function getPreviewPanelMaxWidth(viewportWidth: number): number {
return Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
export function getPreviewPanelMaxWidth(viewportWidth: number, containerWidth?: number): number {
const fractionCap = Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
const containerCap =
containerWidth === undefined ? Infinity : Math.floor(containerWidth) - SIBLING_COLUMN_MIN_WIDTH;
// Never below the panel's own minimum: when the row cannot fit both
// columns' minimums the sibling yields, and useResizableWidth's clamp
// must not see max < min (it would resolve the inversion to min and,
// via drag-end persistence, overwrite the user's stored width).
return Math.max(PREVIEW_PANEL_MIN_WIDTH, Math.min(fractionCap, containerCap));
}
/**
@@ -39,7 +65,10 @@ export function PreviewPanelShell(props: {
}) {
const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
const isInline = props.mode === "inline";
const maxWidth = useViewportClampedMaxWidth();
const hostRef = useRef<HTMLDivElement | null>(null);
// Only inline non-maximized mode applies `width`/`maxWidth`; skip the
// container measurement (and its re-renders) everywhere else.
const maxWidth = useClampedMaxWidth(hostRef, isInline && !props.maximized);
const { width, handlers } = useResizableWidth({
storageKey: props.widthStorageKey ?? PREVIEW_PANEL_WIDTH_STORAGE_KEY,
defaultWidth: props.defaultWidth ?? PREVIEW_PANEL_DEFAULT_WIDTH,
@@ -50,6 +79,7 @@ export function PreviewPanelShell(props: {
return (
<div
ref={hostRef}
className={cn(
"relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
isInline
@@ -70,12 +100,17 @@ export function PreviewPanelShell(props: {
}
/**
* Track viewport width to derive a sensible upper bound for the panel.
* Resize-aware so dragging the OS window narrower re-clamps the stored
* width on the next render (the hook's clamp picks this up automatically).
* Track viewport and flex-row widths to derive an upper bound for the panel.
* Resize-aware so dragging the OS window narrower (or expanding the app
* sidebar) re-clamps the stored width on the next render (the hook's clamp
* picks this up automatically). The row is observed rather than the panel
* itself because the panel competes with its sibling column for row space.
* Row measurement only runs when `enabled`; modes without a resize handle
* never apply the resulting width, so they skip the observer entirely.
*/
function useViewportClampedMaxWidth(): number {
function useClampedMaxWidth(hostRef: RefObject<HTMLDivElement | null>, enabled: boolean): number {
const [vw, setVw] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth));
const [containerWidth, setContainerWidth] = useState<number | undefined>(undefined);
useEffect(() => {
if (typeof window === "undefined") return;
let frame = 0;
@@ -93,5 +128,24 @@ function useViewportClampedMaxWidth(): number {
if (frame !== 0) window.cancelAnimationFrame(frame);
};
}, []);
return getPreviewPanelMaxWidth(vw);
useLayoutEffect(() => {
if (!enabled) return;
const parent = hostRef.current?.parentElement;
if (!parent) return;
// Measure before first paint: the persisted width must be clamped
// against the row on the initial render, not one observer tick later
// (the panel would flash over-wide on every mount). clientWidth is
// integral, so sub-pixel resize deltas bail out of re-rendering.
const measure = () => {
setContainerWidth(parent.clientWidth);
};
measure();
if (typeof ResizeObserver === "undefined") return;
const observer = new ResizeObserver(measure);
observer.observe(parent);
return () => {
observer.disconnect();
};
}, [hostRef, enabled]);
return getPreviewPanelMaxWidth(vw, containerWidth);
}