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