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

PreviewPanelShell.tsx

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