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 type PreviewPanelInlineSize,
13 usePreviewPanelInlineSize,
14} from "~/hooks/usePreviewPanelInlineSize";
15import { RESIZABLE_WIDTH_PROPERTY } from "~/hooks/useResizableWidth";
16import { cn } from "~/lib/utils";
17
18import { RightPanelResizeHandle } from "./RightPanelResizeHandle";
19
20export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
21
22interface PreviewPanelShellProps {
23 mode: PreviewPanelMode;
24 maximized?: boolean;
25 inlineSize?: PreviewPanelInlineSize;
26 open?: boolean;
27 /** Use a distinct key per surface to keep remembered widths independent. */
28 widthStorageKey?: string;
29 defaultWidth?: number;
30 children: ReactNode;
31}
32
33export function PreviewPanelShell(props: PreviewPanelShellProps) {
34 if (props.inlineSize) {
35 return <PreviewPanelShellFrame {...props} inlineSize={props.inlineSize} />;
36 }
37
38 return <ResizablePreviewPanelShell {...props} />;
39}
40
41function ResizablePreviewPanelShell(props: PreviewPanelShellProps) {
42 const hostRef = useRef<HTMLDivElement | null>(null);
43 const inlineSize = usePreviewPanelInlineSize(hostRef, {
44 enabled: props.mode === "inline" && !props.maximized,
45 widthStorageKey: props.widthStorageKey,
46 defaultWidth: props.defaultWidth,
47 });
48 return <PreviewPanelShellFrame {...props} inlineSize={inlineSize} hostRef={hostRef} />;
49}
50
51function PreviewPanelShellFrame(
52 props: PreviewPanelShellProps & {
53 inlineSize: PreviewPanelInlineSize;
54 hostRef?: RefObject<HTMLDivElement | null>;
55 },
56) {
57 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
58 const isInline = props.mode === "inline";
59 const collapsible = isInline && props.open !== undefined;
60 const open = props.open ?? true;
61 const maximized = props.maximized ?? false;
62 const localHostRef = useRef<HTMLDivElement | null>(null);
63 const hostRef = props.hostRef ?? localHostRef;
64 const returnedFocus = useRef(false);
65 const { width, handlers } = props.inlineSize;
66 // Suppress transitions before commit so resize/maximize changes never start width transitions.
67 const [layoutTransition, setLayoutTransition] = useState(() => ({
68 open,
69 width,
70 maximized,
71 suppressed: false,
72 }));
73 if (
74 layoutTransition.open !== open ||
75 layoutTransition.width !== width ||
76 layoutTransition.maximized !== maximized
77 ) {
78 setLayoutTransition({
79 open,
80 width,
81 maximized,
82 suppressed:
83 collapsible &&
84 layoutTransition.open === open &&
85 (layoutTransition.width !== width || layoutTransition.maximized !== maximized),
86 });
87 }
88 const suppressWidthTransition = layoutTransition.suppressed;
89 useLayoutEffect(() => {
90 if (!suppressWidthTransition) return;
91 let restoreFrame = 0;
92 const paintFrame = window.requestAnimationFrame(() => {
93 restoreFrame = window.requestAnimationFrame(() => {
94 setLayoutTransition((current) => ({ ...current, suppressed: false }));
95 });
96 });
97 return () => {
98 window.cancelAnimationFrame(paintFrame);
99 window.cancelAnimationFrame(restoreFrame);
100 };
101 }, [suppressWidthTransition]);
102 useLayoutEffect(() => {
103 const host = hostRef.current;
104 if (!host || !collapsible) return;
105 return () => {
106 if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
107 // Unmount moves the toggle; restore focus at its committed location if removal sent focus to body.
108 queueMicrotask(() => {
109 if (host.ownerDocument.activeElement === host.ownerDocument.body) {
110 host.ownerDocument.querySelector<HTMLButtonElement>("[data-right-panel-toggle]")?.focus();
111 }
112 });
113 };
114 }, [collapsible, hostRef]);
115 useLayoutEffect(() => {
116 const host = hostRef.current;
117 if (!host) return;
118 const collapsed = collapsible && !open;
119 if (!collapsed) returnedFocus.current = false;
120 // Move focus before making its current owner inert, while the toggle is reachable.
121 if (collapsed && host.contains(host.ownerDocument.activeElement)) {
122 const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
123 "[data-right-panel-toggle]",
124 );
125 toggle?.focus();
126 returnedFocus.current = toggle !== null && host.ownerDocument.activeElement === toggle;
127 }
128 host.toggleAttribute("inert", collapsed);
129 }, [collapsible, open, hostRef]);
130 return (
131 <div
132 ref={hostRef}
133 className={cn(
134 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
135 isInline
136 ? maximized
137 ? // Maximizing meets the sidebar border or window edge; omit the duplicate border.
138 "flex-1"
139 : "shrink-0 border-l border-border"
140 : "w-full",
141 collapsible &&
142 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
143 collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!",
144 collapsible && !open && "pointer-events-none",
145 )}
146 style={
147 isInline
148 ? ({
149 [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
150 width: maximized
151 ? "100%"
152 : collapsible && !open
153 ? "0px"
154 : `var(${RESIZABLE_WIDTH_PROPERTY})`,
155 transitionDuration: suppressWidthTransition ? "0ms" : undefined,
156 } as CSSProperties)
157 : undefined
158 }
159 data-preview-panel-mode={props.mode}
160 data-preview-panel-maximized={maximized ? "true" : "false"}
161 >
162 {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
163 <div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
164 <div
165 className="flex h-full min-h-0 min-w-0 flex-col"
166 style={
167 collapsible && !maximized
168 ? { width: `calc(var(${RESIZABLE_WIDTH_PROPERTY}) - 1px)` }
169 : undefined
170 }
171 >
172 {props.children}
173 </div>
174 </div>
175 </div>
176 );
177}