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