Open History to see the last commit
1import { type RefObject, useLayoutEffect, useState } from "react";
2
3import { type ResizableWidthHandlers, useResizableWidth } from "./useResizableWidth";
4import { observeResize } from "../lib/observeResize";
5
6export interface PreviewPanelInlineSize {
7 readonly width: number;
8 readonly handlers: ResizableWidthHandlers;
9}
10
11const PREVIEW_PANEL_WIDTH_STORAGE_KEY = "t3code:preview-panel-width";
12const PREVIEW_PANEL_MIN_WIDTH = 360;
13const PREVIEW_PANEL_MAX_WIDTH_FRACTION = 0.7;
14/**
15 * Floor for the column beside the panel: below this the chat column's
16 * composer overflows, so the panel yields instead.
17 */
18const SIBLING_COLUMN_MIN_WIDTH = 360;
19const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
20
21export function usePreviewPanelInlineSize(
22 hostRef?: RefObject<HTMLElement | null>,
23 options: {
24 readonly enabled?: boolean | undefined;
25 readonly widthStorageKey?: string | undefined;
26 readonly defaultWidth?: number | undefined;
27 readonly container?: HTMLElement | null | undefined;
28 } = {},
29): PreviewPanelInlineSize {
30 const maxWidth = useViewportClampedMaxWidth(hostRef, options.enabled ?? true, options.container);
31 return useResizableWidth({
32 storageKey: options.widthStorageKey ?? PREVIEW_PANEL_WIDTH_STORAGE_KEY,
33 defaultWidth: options.defaultWidth ?? PREVIEW_PANEL_DEFAULT_WIDTH,
34 minWidth: PREVIEW_PANEL_MIN_WIDTH,
35 maxWidth,
36 edge: "left",
37 });
38}
39
40function useViewportClampedMaxWidth(
41 hostRef: RefObject<HTMLElement | null> | undefined,
42 enabled: boolean,
43 container: HTMLElement | null | undefined,
44): number {
45 const [maxWidth, setMaxWidth] = useState(() =>
46 getPreviewPanelMaxWidth(typeof window === "undefined" ? 1280 : window.innerWidth),
47 );
48 useLayoutEffect(() => {
49 if (typeof window === "undefined") return;
50 const row = container ?? (enabled ? hostRef?.current?.parentElement : null) ?? null;
51 // Measure before first paint: the persisted width must be clamped against
52 // the row on the initial render, not one observer tick later (the panel
53 // would flash over-wide on every mount). Only the derived cap is stored: the
54 // row resizes every frame of a sidebar drag, but the cap rarely moves, and an
55 // unchanged cap skips re-rendering the panel's owner.
56 const measure = () => {
57 setMaxWidth(getPreviewPanelMaxWidth(window.innerWidth, row?.clientWidth));
58 };
59 measure();
60 let frame = 0;
61 const onResize = () => {
62 if (frame !== 0) return;
63 frame = window.requestAnimationFrame(() => {
64 frame = 0;
65 measure();
66 });
67 };
68 window.addEventListener("resize", onResize);
69 const stopObserving = row ? observeResize(row, measure) : undefined;
70 return () => {
71 window.removeEventListener("resize", onResize);
72 if (frame !== 0) window.cancelAnimationFrame(frame);
73 stopObserving?.();
74 };
75 }, [container, hostRef, enabled]);
76 return maxWidth;
77}
78export function getPreviewPanelMaxWidth(viewportWidth: number, containerWidth?: number): number {
79 const fractionCap = Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
80 const containerCap =
81 containerWidth === undefined ? Infinity : Math.floor(containerWidth) - SIBLING_COLUMN_MIN_WIDTH;
82 // Never below the panel's own minimum: when the row cannot fit both
83 // columns' minimums the sibling yields, and useResizableWidth's clamp must
84 // not see max < min (it would resolve the inversion to min and, via
85 // drag-end persistence, overwrite the user's stored width).
86 return Math.max(PREVIEW_PANEL_MIN_WIDTH, Math.min(fractionCap, containerCap));
87}