Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usePreviewPanelInlineSize.ts

91 lines3.5 KB

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): PreviewPanelInlineSize {
28 const maxWidth = useViewportClampedMaxWidth(hostRef);
29 return useResizableWidth({
30 storageKey: PREVIEW_PANEL_WIDTH_STORAGE_KEY,
31 defaultWidth: PREVIEW_PANEL_DEFAULT_WIDTH,
32 minWidth: PREVIEW_PANEL_MIN_WIDTH,
33 maxWidth,
34 edge: "left",
35 });
37​
38/**
39 * Keep the resizable panel's upper bound in sync with the window AND the row
40 * it sits in. Resize-aware so dragging the OS window narrower (or expanding
41 * the app sidebar) re-clamps the stored width on the next render.
42 */
43function useViewportClampedMaxWidth(hostRef?: RefObject<HTMLElement | null>): number {
44 const [vw, setVw] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth));
45 const [containerWidth, setContainerWidth] = useState<number | undefined>(undefined);
46 useEffect(() => {
47 if (typeof window === "undefined") return;
48 let frame = 0;
49 const onResize = () => {
50 if (frame !== 0) return;
51 frame = window.requestAnimationFrame(() => {
52 frame = 0;
53 setVw(window.innerWidth);
54 });
55 };
56 window.addEventListener("resize", onResize);
57 return () => {
58 window.removeEventListener("resize", onResize);
59 if (frame !== 0) window.cancelAnimationFrame(frame);
60 };
61 }, []);
62 useLayoutEffect(() => {
63 const parent = hostRef?.current?.parentElement;
64 if (!parent) return;
65 // Measure before first paint: the persisted width must be clamped against
66 // the row on the initial render, not one observer tick later (the panel
67 // would flash over-wide on every mount). clientWidth is integral, so
68 // sub-pixel resize deltas bail out of re-rendering.
69 const measure = () => {
70 setContainerWidth(parent.clientWidth);
71 };
72 measure();
73 if (typeof ResizeObserver === "undefined") return;
74 const observer = new ResizeObserver(measure);
75 observer.observe(parent);
76 return () => {
77 observer.disconnect();
78 };
79 }, [hostRef]);
80 return getPreviewPanelMaxWidth(vw, containerWidth);
82export function getPreviewPanelMaxWidth(viewportWidth: number, containerWidth?: number): number {
83 const fractionCap = Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
84 const containerCap =
85 containerWidth === undefined ? Infinity : Math.floor(containerWidth) - SIBLING_COLUMN_MIN_WIDTH;
86 // Never below the panel's own minimum: when the row cannot fit both
87 // columns' minimums the sibling yields, and useResizableWidth's clamp must
88 // not see max < min (it would resolve the inversion to min and, via
89 // drag-end persistence, overwrite the user's stored width).
90 return Math.max(PREVIEW_PANEL_MIN_WIDTH, Math.min(fractionCap, containerCap));