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

usePreviewPanelInlineSize.ts

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