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
25interface PreviewPanelShellProps {
26 mode: PreviewPanelMode;
27 maximized?: boolean;
28 inlineSize?: PreviewPanelInlineSize;
29 open?: boolean;
30 /**
31 * Callers embedding this shell for a different surface (e.g. the pull
32 * requests page) should pass their own key so resizing one panel doesn't
33 * clobber the other's remembered width.
34 */
35 widthStorageKey?: string;
36 defaultWidth?: number;
37 children: ReactNode;
38}
39
40export function PreviewPanelShell(props: PreviewPanelShellProps) {
41 if (props.inlineSize) {
42 return <PreviewPanelShellFrame {...props} inlineSize={props.inlineSize} />;
43 }
44
45 return <ResizablePreviewPanelShell {...props} />;
46}
47
48function ResizablePreviewPanelShell(props: PreviewPanelShellProps) {
49 const hostRef = useRef<HTMLDivElement | null>(null);
50 const inlineSize = usePreviewPanelInlineSize(hostRef, {
51 enabled: props.mode === "inline" && !props.maximized,
52 widthStorageKey: props.widthStorageKey,
53 defaultWidth: props.defaultWidth,
54 });
55 return <PreviewPanelShellFrame {...props} inlineSize={inlineSize} hostRef={hostRef} />;
56}
57
58function PreviewPanelShellFrame(
59 props: PreviewPanelShellProps & {
60 inlineSize: PreviewPanelInlineSize;
61 hostRef?: RefObject<HTMLDivElement | null>;
62 },
63) {
64 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
65 const isInline = props.mode === "inline";
66 const collapsible = isInline && props.open !== undefined;
67 const open = props.open ?? true;
68 const maximized = props.maximized ?? false;
69 const localHostRef = useRef<HTMLDivElement | null>(null);
70 const hostRef = props.hostRef ?? localHostRef;
71 const returnedFocus = useRef(false);
72 const { width, handlers } = props.inlineSize;
73 // Derive suppression before the layout commits so the browser never creates
74 // a width transition for resize or maximize changes.
75 const [layoutTransition, setLayoutTransition] = useState(() => ({
76 open,
77 width,
78 maximized,
79 suppressed: false,
80 }));
81 if (
82 layoutTransition.open !== open ||
83 layoutTransition.width !== width ||
84 layoutTransition.maximized !== maximized
85 ) {
86 setLayoutTransition({
87 open,
88 width,
89 maximized,
90 suppressed:
91 collapsible &&
92 layoutTransition.open === open &&
93 (layoutTransition.width !== width || layoutTransition.maximized !== maximized),
94 });
95 }
96 const suppressWidthTransition = layoutTransition.suppressed;
97 useLayoutEffect(() => {
98 if (!suppressWidthTransition) return;
99 let restoreFrame = 0;
100 const paintFrame = window.requestAnimationFrame(() => {
101 restoreFrame = window.requestAnimationFrame(() => {
102 setLayoutTransition((current) => ({ ...current, suppressed: false }));
103 });
104 });
105 return () => {
106 window.cancelAnimationFrame(paintFrame);
107 window.cancelAnimationFrame(restoreFrame);
108 };
109 }, [suppressWidthTransition]);
110 useLayoutEffect(() => {
111 const host = hostRef.current;
112 if (!host || !collapsible) return;
113 return () => {
114 if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
115 // The toggle moves when the shell unmounts, after an animation or immediately.
116 // Restore focus at its committed location if removal cleared focus to body.
117 queueMicrotask(() => {
118 if (host.ownerDocument.activeElement === host.ownerDocument.body) {
119 host.ownerDocument.querySelector<HTMLButtonElement>("[data-right-panel-toggle]")?.focus();
120 }
121 });
122 };
123 }, [collapsible, hostRef]);
124 useLayoutEffect(() => {
125 const host = hostRef.current;
126 if (!host) return;
127 const collapsed = collapsible && !open;
128 if (!collapsed) returnedFocus.current = false;
129 // Move focus before making its current owner inert, while the toggle is reachable.
130 if (collapsed && host.contains(host.ownerDocument.activeElement)) {
131 const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
132 "[data-right-panel-toggle]",
133 );
134 toggle?.focus();
135 returnedFocus.current = toggle !== null && host.ownerDocument.activeElement === toggle;
136 }
137 host.toggleAttribute("inert", collapsed);
138 }, [collapsible, open, hostRef]);
139 return (
140 <div
141 ref={hostRef}
142 className={cn(
143 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
144 isInline
145 ? maximized
146 ? // Maximized, the panel's left edge meets the sidebar's own border
147 // (or the window edge), so a second border would double it.
148 "flex-1"
149 : "shrink-0 border-l border-border"
150 : "w-full",
151 collapsible &&
152 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
153 collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!",
154 collapsible && !open && "pointer-events-none",
155 )}
156 style={
157 isInline
158 ? ({
159 [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
160 width: maximized
161 ? "100%"
162 : collapsible && !open
163 ? "0px"
164 : `var(${RESIZABLE_WIDTH_PROPERTY})`,
165 transitionDuration: suppressWidthTransition ? "0ms" : undefined,
166 } as CSSProperties)
167 : undefined
168 }
169 data-preview-panel-mode={props.mode}
170 data-preview-panel-maximized={maximized ? "true" : "false"}
171 >
172 {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
173 <div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
174 <div
175 className="flex h-full min-h-0 min-w-0 flex-col"
176 style={
177 collapsible && !maximized
178 ? { width: `calc(var(${RESIZABLE_WIDTH_PROPERTY}) - 1px)` }
179 : undefined
180 }
181 >
182 {props.children}
183 </div>
184 </div>
185 </div>
186 );
187}