Open History to see the last commit
1import { type ReactNode, type RefObject, useLayoutEffect, useRef, useState } from "react";
2
3import { isElectron } from "~/env";
4import {
5 getPreviewPanelMaxWidth,
6 type PreviewPanelInlineSize,
7 usePreviewPanelInlineSize,
8} from "~/hooks/usePreviewPanelInlineSize";
9
10export { getPreviewPanelMaxWidth };
11import { cn } from "~/lib/utils";
12
13import { RightPanelResizeHandle } from "./RightPanelResizeHandle";
14
15export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
16
17/**
18 * Shell for the preview panel. In inline mode the panel is user-resizable
19 * via a drag handle on the left edge; width persists per browser. In
20 * sheet/sidebar modes the parent owns the size.
21 */
22interface PreviewPanelShellProps {
23 mode: PreviewPanelMode;
24 maximized?: boolean;
25 inlineSize?: PreviewPanelInlineSize;
26 open?: boolean;
27 /**
28 * Overrides the localStorage key used to persist the panel width. Callers
29 * embedding this shell for a different surface (e.g. the pull requests
30 * page) should pass their own key so resizing one panel doesn't clobber
31 * the other's remembered width.
32 */
33 widthStorageKey?: string;
34 /** Overrides the initial width (px) before the user has resized the panel. */
35 defaultWidth?: number;
36 children: ReactNode;
37}
38
39export function PreviewPanelShell(props: PreviewPanelShellProps) {
40 if (props.inlineSize) {
41 return <PreviewPanelShellFrame {...props} inlineSize={props.inlineSize} />;
42 }
43
44 return <ResizablePreviewPanelShell {...props} />;
45}
46
47function ResizablePreviewPanelShell(props: PreviewPanelShellProps) {
48 const hostRef = useRef<HTMLDivElement | null>(null);
49 const inlineSize = usePreviewPanelInlineSize(hostRef, {
50 enabled: props.mode === "inline" && !props.maximized,
51 widthStorageKey: props.widthStorageKey,
52 defaultWidth: props.defaultWidth,
53 });
54 return <PreviewPanelShellFrame {...props} inlineSize={inlineSize} hostRef={hostRef} />;
55}
56
57function PreviewPanelShellFrame(
58 props: PreviewPanelShellProps & {
59 inlineSize: PreviewPanelInlineSize;
60 hostRef?: RefObject<HTMLDivElement | null>;
61 },
62) {
63 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
64 const isInline = props.mode === "inline";
65 const collapsible = isInline && props.open !== undefined;
66 const open = props.open ?? true;
67 const maximized = props.maximized ?? false;
68 const localHostRef = useRef<HTMLDivElement | null>(null);
69 const hostRef = props.hostRef ?? localHostRef;
70 const { width, handlers } = props.inlineSize;
71 // Derive suppression before the layout commits so the browser never creates
72 // a width transition for resize or maximize changes.
73 const [layoutTransition, setLayoutTransition] = useState(() => ({
74 open,
75 width,
76 maximized,
77 suppressed: false,
78 }));
79 if (
80 layoutTransition.open !== open ||
81 layoutTransition.width !== width ||
82 layoutTransition.maximized !== maximized
83 ) {
84 setLayoutTransition({
85 open,
86 width,
87 maximized,
88 suppressed:
89 collapsible &&
90 layoutTransition.open === open &&
91 (layoutTransition.width !== width || layoutTransition.maximized !== maximized),
92 });
93 }
94 const suppressWidthTransition = layoutTransition.suppressed;
95 useLayoutEffect(() => {
96 if (!suppressWidthTransition) return;
97 let restoreFrame = 0;
98 const paintFrame = window.requestAnimationFrame(() => {
99 restoreFrame = window.requestAnimationFrame(() => {
100 setLayoutTransition((current) => ({ ...current, suppressed: false }));
101 });
102 });
103 return () => {
104 window.cancelAnimationFrame(paintFrame);
105 window.cancelAnimationFrame(restoreFrame);
106 };
107 }, [suppressWidthTransition]);
108 return (
109 <div
110 ref={hostRef}
111 className={cn(
112 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
113 isInline
114 ? maximized
115 ? "flex-1 border-l border-border"
116 : "shrink-0 border-l border-border"
117 : "w-full",
118 collapsible &&
119 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
120 collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!",
121 collapsible && !open && "pointer-events-none",
122 )}
123 style={
124 isInline
125 ? {
126 width: maximized ? "100%" : collapsible && !open ? "0px" : `${width}px`,
127 transitionDuration: suppressWidthTransition ? "0ms" : undefined,
128 }
129 : undefined
130 }
131 data-preview-panel-mode={props.mode}
132 data-preview-panel-maximized={maximized ? "true" : "false"}
133 >
134 {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
135 <div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
136 <div
137 className="flex h-full min-h-0 min-w-0 flex-col"
138 style={collapsible && !maximized ? { width: `calc(${width}px - 1px)` } : undefined}
139 >
140 {props.children}
141 </div>
142 </div>
143 </div>
144 );
145}