Open History to see the last commit
1import { type RefObject, useEffect, useLayoutEffect, useState } from "react";
2
3import { type ResizableWidthHandlers, useResizableWidth } from "./useResizableWidth";
4
5export interface PreviewPanelInlineSize {
6 readonly width: number;
7 readonly handlers: ResizableWidthHandlers;
8}
9
10const PREVIEW_PANEL_WIDTH_STORAGE_KEY = "t3code:preview-panel-width";
11const PREVIEW_PANEL_MIN_WIDTH = 360;
12/**
13 * Upper bound as a fraction of the viewport; only binds on wide screens.
14 * On narrow windows the container clamp below is what preserves the
15 * sibling column's usable width.
16 */
17const PREVIEW_PANEL_MAX_WIDTH_FRACTION = 0.7;
18/**
19 * Floor for the column beside the panel: below this the chat column's
20 * composer overflows, so the panel yields instead.
21 */
22const SIBLING_COLUMN_MIN_WIDTH = 360;
23const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
24
25export function usePreviewPanelInlineSize(
26 hostRef?: RefObject<HTMLElement | null>,
27 options: {
28 readonly enabled?: boolean | undefined;
29 readonly widthStorageKey?: string | undefined;
30 readonly defaultWidth?: number | undefined;
31 } = {},
32): PreviewPanelInlineSize {
33 const maxWidth = useViewportClampedMaxWidth(hostRef, options.enabled ?? true);
34 return useResizableWidth({
35 storageKey: options.widthStorageKey ?? PREVIEW_PANEL_WIDTH_STORAGE_KEY,
36 defaultWidth: options.defaultWidth ?? PREVIEW_PANEL_DEFAULT_WIDTH,
37 minWidth: PREVIEW_PANEL_MIN_WIDTH,
38 maxWidth,
39 edge: "left",
40 });
41}
42
43/**
44 * Keep the resizable panel's upper bound in sync with the window AND the row
45 * it sits in. Resize-aware so dragging the OS window narrower (or expanding
46 * the app sidebar) re-clamps the stored width on the next render.
47 */
48function useViewportClampedMaxWidth(
49 hostRef: RefObject<HTMLElement | null> | undefined,
50 enabled: boolean,
51): number {
52 const [vw, setVw] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth));
53 const [containerWidth, setContainerWidth] = useState<number | undefined>(undefined);
54 useEffect(() => {
55 if (typeof window === "undefined") return;
56 let frame = 0;
57 const onResize = () => {
58 if (frame !== 0) return;
59 frame = window.requestAnimationFrame(() => {
60 frame = 0;
61 setVw(window.innerWidth);
62 });
63 };
64 window.addEventListener("resize", onResize);
65 return () => {
66 window.removeEventListener("resize", onResize);
67 if (frame !== 0) window.cancelAnimationFrame(frame);
68 };
69 }, []);
70 useLayoutEffect(() => {
71 if (!enabled) return;
72 const parent = hostRef?.current?.parentElement;
73 if (!parent) return;
74 // Measure before first paint: the persisted width must be clamped against
75 // the row on the initial render, not one observer tick later (the panel
76 // would flash over-wide on every mount). clientWidth is integral, so
77 // sub-pixel resize deltas bail out of re-rendering.
78 const measure = () => {
79 setContainerWidth(parent.clientWidth);
80 };
81 measure();
82 if (typeof ResizeObserver === "undefined") return;
83 const observer = new ResizeObserver(measure);
84 observer.observe(parent);
85 return () => {
86 observer.disconnect();
87 };
88 }, [hostRef, enabled]);
89 return getPreviewPanelMaxWidth(vw, containerWidth);
90}
91export function getPreviewPanelMaxWidth(viewportWidth: number, containerWidth?: number): number {
92 const fractionCap = Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
93 const containerCap =
94 containerWidth === undefined ? Infinity : Math.floor(containerWidth) - SIBLING_COLUMN_MIN_WIDTH;
95 // Never below the panel's own minimum: when the row cannot fit both
96 // columns' minimums the sibling yields, and useResizableWidth's clamp must
97 // not see max < min (it would resolve the inversion to min and, via
98 // drag-end persistence, overwrite the user's stored width).
99 return Math.max(PREVIEW_PANEL_MIN_WIDTH, Math.min(fractionCap, containerCap));
100}