Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewPanelShell.tsx

159 lines5.9 KB

1import { type CSSProperties, type ReactNode, useLayoutEffect, useRef, useState } from "react";
2import {
3 type PreviewPanelInlineSize,
4 usePreviewPanelInlineSize,
5} from "~/hooks/usePreviewPanelInlineSize";
6import { RESIZABLE_WIDTH_PROPERTY } from "~/hooks/useResizableWidth";
7import { cn } from "~/lib/utils";
8​
9import { RightPanelResizeHandle } from "./RightPanelResizeHandle";
10​
11export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
12​
13interface PreviewPanelShellProps {
14 mode: PreviewPanelMode;
15 maximized?: boolean;
16 inlineSize?: PreviewPanelInlineSize;
17 open?: boolean;
18 /** Use a distinct key per surface to keep remembered widths independent. */
19 widthStorageKey?: string;
20 defaultWidth?: number;
21 children: ReactNode;
23​
24export function PreviewPanelShell(props: PreviewPanelShellProps) {
25 if (props.inlineSize) {
26 return <PreviewPanelShellFrame {...props} inlineSize={props.inlineSize} />;
27 }
28​
29 return <ResizablePreviewPanelShell {...props} />;
31​
32function ResizablePreviewPanelShell(props: PreviewPanelShellProps) {
33 const inlineSize = usePreviewPanelInlineSize({
34 enabled: props.mode === "inline" && !props.maximized,
35 widthStorageKey: props.widthStorageKey,
36 defaultWidth: props.defaultWidth,
37 });
38 return <PreviewPanelShellFrame {...props} inlineSize={inlineSize} />;
40​
41function PreviewPanelShellFrame(
42 props: PreviewPanelShellProps & { inlineSize: PreviewPanelInlineSize },
43) {
44 const isInline = props.mode === "inline";
45 const collapsible = isInline && props.open !== undefined;
46 const open = props.open ?? true;
47 const maximized = props.maximized ?? false;
48 const hostRef = useRef<HTMLDivElement | null>(null);
49 const returnedFocus = useRef(false);
50 const { width, handlers, observeRow } = props.inlineSize;
51 // Width animates only for an open or close. Any other width change disables
52 // the transition in the same commit that changes the width, so the browser
53 // never starts one; the next open or close re-enables it in its own commit,
54 // so nothing waits for a paint.
55 const [widthTransition, setWidthTransition] = useState(() => ({
56 open,
57 width,
58 maximized,
59 enabled: true,
60 }));
61 if (
62 widthTransition.open !== open ||
63 widthTransition.width !== width ||
64 widthTransition.maximized !== maximized
65 ) {
66 setWidthTransition({
67 open,
68 width,
69 maximized,
70 enabled: widthTransition.open !== open,
71 });
72 }
73 const suppressWidthTransition = collapsible && !widthTransition.enabled;
74 const attachHost = (host: HTMLDivElement) => {
75 hostRef.current = host;
76 const stopObservingRow = observeRow(host);
77 return () => {
78 hostRef.current = null;
79 stopObservingRow();
80 };
81 };
82 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- on unmount, returns DOM focus to the panel toggle if removal dropped it on the body
83 useLayoutEffect(() => {
84 const host = hostRef.current;
85 if (!host || !collapsible) return;
86 return () => {
87 if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
88 // Unmount moves the toggle; restore focus at its committed location if removal sent focus to body.
89 queueMicrotask(() => {
90 if (host.ownerDocument.activeElement === host.ownerDocument.body) {
91 host.ownerDocument.querySelector<HTMLButtonElement>("[data-right-panel-toggle]")?.focus();
92 }
93 });
94 };
95 }, [collapsible, hostRef]);
96 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- moves focus to the toggle and sets inert on the host DOM node before a collapse paints
97 useLayoutEffect(() => {
98 const host = hostRef.current;
99 if (!host) return;
100 const collapsed = collapsible && !open;
101 if (!collapsed) returnedFocus.current = false;
102 // Move focus before making its current owner inert, while the toggle is reachable.
103 if (collapsed && host.contains(host.ownerDocument.activeElement)) {
104 const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
105 "[data-right-panel-toggle]",
106 );
107 toggle?.focus();
108 returnedFocus.current = toggle !== null && host.ownerDocument.activeElement === toggle;
109 }
110 host.toggleAttribute("inert", collapsed);
111 }, [collapsible, open, hostRef]);
112 return (
113 <div
114 ref={attachHost}
115 className={cn(
116 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
117 isInline
118 ? maximized
119 ? // Maximizing meets the sidebar border or window edge; omit the duplicate border.
120 "flex-1"
121 : "shrink-0 border-l border-border"
122 : "w-full",
123 collapsible &&
124 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
125 collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!",
126 collapsible && !open && "pointer-events-none",
127 )}
128 style={
129 isInline
130 ? ({
131 [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
132 width: maximized
133 ? "100%"
134 : collapsible && !open
135 ? "0px"
136 : `var(${RESIZABLE_WIDTH_PROPERTY})`,
137 transitionDuration: suppressWidthTransition ? "0ms" : undefined,
138 } as CSSProperties)
139 : undefined
140 }
141 data-preview-panel-mode={props.mode}
142 data-preview-panel-maximized={maximized ? "true" : "false"}
143 >
144 {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
145 <div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
146 <div
147 className="flex h-full min-h-0 min-w-0 flex-col"
148 style={
149 collapsible && !maximized
150 ? { width: `calc(var(${RESIZABLE_WIDTH_PROPERTY}) - 1px)` }
151 : undefined
152 }
153 >
154 {props.children}
155 </div>
156 </div>
157 </div>
158 );