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

usePreviewPanelInlineSize.ts

77 lines3.0 KB

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/** Below this, the sibling chat column's composer overflows. */
15const SIBLING_COLUMN_MIN_WIDTH = 360;
16const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
17​
18export function usePreviewPanelInlineSize(
19 hostRef?: RefObject<HTMLElement | null>,
20 options: {
21 readonly enabled?: boolean | undefined;
22 readonly widthStorageKey?: string | undefined;
23 readonly defaultWidth?: number | undefined;
24 readonly container?: HTMLElement | null | undefined;
25 } = {},
26): PreviewPanelInlineSize {
27 const maxWidth = useViewportClampedMaxWidth(hostRef, options.enabled ?? true, options.container);
28 return useResizableWidth({
29 storageKey: options.widthStorageKey ?? PREVIEW_PANEL_WIDTH_STORAGE_KEY,
30 defaultWidth: options.defaultWidth ?? PREVIEW_PANEL_DEFAULT_WIDTH,
31 minWidth: PREVIEW_PANEL_MIN_WIDTH,
32 maxWidth,
33 edge: "left",
34 });
36​
37function useViewportClampedMaxWidth(
38 hostRef: RefObject<HTMLElement | null> | undefined,
39 enabled: boolean,
40 container: HTMLElement | null | undefined,
41): number {
42 const [maxWidth, setMaxWidth] = useState(() =>
43 getPreviewPanelMaxWidth(typeof window === "undefined" ? 1280 : window.innerWidth),
44 );
45 useLayoutEffect(() => {
46 if (typeof window === "undefined") return;
47 const row = container ?? (enabled ? hostRef?.current?.parentElement : null) ?? null;
48 // Clamp before paint to avoid flashing; store only the cap, which rarely changes during sidebar drags.
49 const measure = () => {
50 setMaxWidth(getPreviewPanelMaxWidth(window.innerWidth, row?.clientWidth));
51 };
52 measure();
53 let frame = 0;
54 const onResize = () => {
55 if (frame !== 0) return;
56 frame = window.requestAnimationFrame(() => {
57 frame = 0;
58 measure();
59 });
60 };
61 window.addEventListener("resize", onResize);
62 const stopObserving = row ? observeResize(row, measure) : undefined;
63 return () => {
64 window.removeEventListener("resize", onResize);
65 if (frame !== 0) window.cancelAnimationFrame(frame);
66 stopObserving?.();
67 };
68 }, [container, hostRef, enabled]);
69 return maxWidth;
71function getPreviewPanelMaxWidth(viewportWidth: number, containerWidth?: number): number {
72 const fractionCap = Math.floor(viewportWidth * PREVIEW_PANEL_MAX_WIDTH_FRACTION);
73 const containerCap =
74 containerWidth === undefined ? Infinity : Math.floor(containerWidth) - SIBLING_COLUMN_MIN_WIDTH;
75 // Keep max >= min; an inverted clamp would overwrite the saved width on drag end.
76 return Math.max(PREVIEW_PANEL_MIN_WIDTH, Math.min(fractionCap, containerCap));