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

PreviewPanelShell.tsx

157 lines5.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 { 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 useLayoutEffect(() => {
83 const host = hostRef.current;
84 if (!host || !collapsible) return;
85 return () => {
86 if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
87 // Unmount moves the toggle; restore focus at its committed location if removal sent focus to body.
88 queueMicrotask(() => {
89 if (host.ownerDocument.activeElement === host.ownerDocument.body) {
90 host.ownerDocument.querySelector<HTMLButtonElement>("[data-right-panel-toggle]")?.focus();
91 }
92 });
93 };
94 }, [collapsible, hostRef]);
95 useLayoutEffect(() => {
96 const host = hostRef.current;
97 if (!host) return;
98 const collapsed = collapsible && !open;
99 if (!collapsed) returnedFocus.current = false;
100 // Move focus before making its current owner inert, while the toggle is reachable.
101 if (collapsed && host.contains(host.ownerDocument.activeElement)) {
102 const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
103 "[data-right-panel-toggle]",
104 );
105 toggle?.focus();
106 returnedFocus.current = toggle !== null && host.ownerDocument.activeElement === toggle;
107 }
108 host.toggleAttribute("inert", collapsed);
109 }, [collapsible, open, hostRef]);
110 return (
111 <div
112 ref={attachHost}
113 className={cn(
114 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
115 isInline
116 ? maximized
117 ? // Maximizing meets the sidebar border or window edge; omit the duplicate border.
118 "flex-1"
119 : "shrink-0 border-l border-border"
120 : "w-full",
121 collapsible &&
122 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
123 collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!",
124 collapsible && !open && "pointer-events-none",
125 )}
126 style={
127 isInline
128 ? ({
129 [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
130 width: maximized
131 ? "100%"
132 : collapsible && !open
133 ? "0px"
134 : `var(${RESIZABLE_WIDTH_PROPERTY})`,
135 transitionDuration: suppressWidthTransition ? "0ms" : undefined,
136 } as CSSProperties)
137 : undefined
138 }
139 data-preview-panel-mode={props.mode}
140 data-preview-panel-maximized={maximized ? "true" : "false"}
141 >
142 {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
143 <div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
144 <div
145 className="flex h-full min-h-0 min-w-0 flex-col"
146 style={
147 collapsible && !maximized
148 ? { width: `calc(var(${RESIZABLE_WIDTH_PROPERTY}) - 1px)` }
149 : undefined
150 }
151 >
152 {props.children}
153 </div>
154 </div>
155 </div>
156 );