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

PreviewPanelShell.tsx

96 lines3.5 KB

1import { type ReactNode, useEffect, useState } from "react";
2​
3import { isElectron } from "~/env";
4import { useResizableWidth } from "~/hooks/useResizableWidth";
5import { cn } from "~/lib/utils";
6​
7import { RightPanelResizeHandle } from "./RightPanelResizeHandle";
8​
9export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
10​
11const PREVIEW_PANEL_WIDTH_STORAGE_KEY = "v12:preview-panel-width";
12const PREVIEW_PANEL_MIN_WIDTH = 360;
13/** Hard ceiling so a wide monitor can't yield a panel that swallows the chat. */
14const PREVIEW_PANEL_MAX_WIDTH_PX = 1400;
15/** Fraction of the viewport allowed; the panel is min(this · vw, MAX_PX). */
16const PREVIEW_PANEL_MAX_WIDTH_FRACTION = 0.7;
17const PREVIEW_PANEL_DEFAULT_WIDTH = 540;
18​
19/**
20 * Shell for the preview panel. In inline mode the panel is user-resizable
21 * via a drag handle on the left edge; width persists per browser. In
22 * sheet/sidebar modes the parent owns the size.
23 */
24export function PreviewPanelShell(props: {
25 mode: PreviewPanelMode;
26 open?: boolean;
27 maximized?: boolean;
28 children: ReactNode;
29}) {
30 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
31 const isInline = props.mode === "inline";
32 const open = props.open ?? true;
33 const maxWidth = useViewportClampedMaxWidth();
34 const { width, handlers } = useResizableWidth({
35 storageKey: PREVIEW_PANEL_WIDTH_STORAGE_KEY,
36 defaultWidth: PREVIEW_PANEL_DEFAULT_WIDTH,
37 minWidth: PREVIEW_PANEL_MIN_WIDTH,
38 maxWidth,
39 edge: "left",
40 });
41 const inlineStyle = isInline
42 ? {
43 width: open && !props.maximized ? `${width}px` : "0px",
44 flexGrow: open && props.maximized ? 1 : 0,
45 }
46 : undefined;
47​
48 return (
49 <div
50 className={cn(
51 "relative flex h-full min-h-0 min-w-0 flex-col self-stretch overflow-hidden bg-background transition-[width,flex-grow,opacity,border-color] duration-200 ease-out motion-reduce:transition-none",
52 isInline
53 ? !open
54 ? "pointer-events-none w-0 shrink-0 border-l border-transparent opacity-0"
55 : "shrink-0 border-l border-border opacity-100"
56 : "w-full",
57 )}
58 style={inlineStyle}
59 aria-hidden={isInline && !open ? true : undefined}
60 inert={isInline && !open ? true : undefined}
61 data-preview-panel-mode={props.mode}
62 data-preview-panel-maximized={props.maximized ? "true" : "false"}
63 >
64 {isInline && !props.maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
65 {useDragRegion ? <div className="electron-drag-region h-0 w-full" aria-hidden /> : null}
66 {props.children}
67 </div>
68 );
70​
71/**
72 * Track viewport width to derive a sensible upper bound for the panel.
73 * Resize-aware so dragging the OS window narrower re-clamps the stored
74 * width on the next render (the hook's clamp picks this up automatically).
75 */
76function useViewportClampedMaxWidth(): number {
77 const [vw, setVw] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth));
78 useEffect(() => {
79 if (typeof window === "undefined") return;
80 let frame = 0;
81 const onResize = () => {
82 // Coalesce rapid resize events into one rAF tick.
83 if (frame !== 0) return;
84 frame = window.requestAnimationFrame(() => {
85 frame = 0;
86 setVw(window.innerWidth);
87 });
88 };
89 window.addEventListener("resize", onResize);
90 return () => {
91 window.removeEventListener("resize", onResize);
92 if (frame !== 0) window.cancelAnimationFrame(frame);
93 };
94 }, []);
95 return Math.min(PREVIEW_PANEL_MAX_WIDTH_PX, Math.floor(vw * PREVIEW_PANEL_MAX_WIDTH_FRACTION));