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

PreviewPanelShell.tsx

155 lines5.0 KB

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 getPreviewPanelMaxWidth,
13 type PreviewPanelInlineSize,
14 usePreviewPanelInlineSize,
15} from "~/hooks/usePreviewPanelInlineSize";
16import { RESIZABLE_WIDTH_PROPERTY } from "~/hooks/useResizableWidth";
17​
18export { getPreviewPanelMaxWidth };
19import { cn } from "~/lib/utils";
20​
21import { RightPanelResizeHandle } from "./RightPanelResizeHandle";
22​
23export type PreviewPanelMode = "inline" | "sheet" | "sidebar" | "embedded";
24​
25interface PreviewPanelShellProps {
26 mode: PreviewPanelMode;
27 maximized?: boolean;
28 inlineSize?: PreviewPanelInlineSize;
29 open?: boolean;
30 /**
31 * Callers embedding this shell for a different surface (e.g. the pull
32 * requests page) should pass their own key so resizing one panel doesn't
33 * clobber the other's remembered width.
34 */
35 widthStorageKey?: string;
36 defaultWidth?: number;
37 children: ReactNode;
39​
40export function PreviewPanelShell(props: PreviewPanelShellProps) {
41 if (props.inlineSize) {
42 return <PreviewPanelShellFrame {...props} inlineSize={props.inlineSize} />;
43 }
44​
45 return <ResizablePreviewPanelShell {...props} />;
47​
48function ResizablePreviewPanelShell(props: PreviewPanelShellProps) {
49 const hostRef = useRef<HTMLDivElement | null>(null);
50 const inlineSize = usePreviewPanelInlineSize(hostRef, {
51 enabled: props.mode === "inline" && !props.maximized,
52 widthStorageKey: props.widthStorageKey,
53 defaultWidth: props.defaultWidth,
54 });
55 return <PreviewPanelShellFrame {...props} inlineSize={inlineSize} hostRef={hostRef} />;
57​
58function PreviewPanelShellFrame(
59 props: PreviewPanelShellProps & {
60 inlineSize: PreviewPanelInlineSize;
61 hostRef?: RefObject<HTMLDivElement | null>;
62 },
63) {
64 const useDragRegion = isElectron && props.mode !== "sheet" && props.mode !== "embedded";
65 const isInline = props.mode === "inline";
66 const collapsible = isInline && props.open !== undefined;
67 const open = props.open ?? true;
68 const maximized = props.maximized ?? false;
69 const localHostRef = useRef<HTMLDivElement | null>(null);
70 const hostRef = props.hostRef ?? localHostRef;
71 const { width, handlers } = props.inlineSize;
72 // Derive suppression before the layout commits so the browser never creates
73 // a width transition for resize or maximize changes.
74 const [layoutTransition, setLayoutTransition] = useState(() => ({
75 open,
76 width,
77 maximized,
78 suppressed: false,
79 }));
80 if (
81 layoutTransition.open !== open ||
82 layoutTransition.width !== width ||
83 layoutTransition.maximized !== maximized
84 ) {
85 setLayoutTransition({
86 open,
87 width,
88 maximized,
89 suppressed:
90 collapsible &&
91 layoutTransition.open === open &&
92 (layoutTransition.width !== width || layoutTransition.maximized !== maximized),
93 });
94 }
95 const suppressWidthTransition = layoutTransition.suppressed;
96 useLayoutEffect(() => {
97 if (!suppressWidthTransition) return;
98 let restoreFrame = 0;
99 const paintFrame = window.requestAnimationFrame(() => {
100 restoreFrame = window.requestAnimationFrame(() => {
101 setLayoutTransition((current) => ({ ...current, suppressed: false }));
102 });
103 });
104 return () => {
105 window.cancelAnimationFrame(paintFrame);
106 window.cancelAnimationFrame(restoreFrame);
107 };
108 }, [suppressWidthTransition]);
109 return (
110 <div
111 ref={hostRef}
112 className={cn(
113 "relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
114 isInline
115 ? maximized
116 ? "flex-1 border-l border-border"
117 : "shrink-0 border-l border-border"
118 : "w-full",
119 collapsible &&
120 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
121 collapsible && open && "[[data-panel-animations=true]_&]:starting:w-0!",
122 collapsible && !open && "pointer-events-none",
123 )}
124 style={
125 isInline
126 ? ({
127 [RESIZABLE_WIDTH_PROPERTY]: `${width}px`,
128 width: maximized
129 ? "100%"
130 : collapsible && !open
131 ? "0px"
132 : `var(${RESIZABLE_WIDTH_PROPERTY})`,
133 transitionDuration: suppressWidthTransition ? "0ms" : undefined,
134 } as CSSProperties)
135 : undefined
136 }
137 data-preview-panel-mode={props.mode}
138 data-preview-panel-maximized={maximized ? "true" : "false"}
139 >
140 {isInline && !maximized ? <RightPanelResizeHandle handlers={handlers} /> : null}
141 <div className={cn("h-full min-h-0 w-full", collapsible && "overflow-clip")}>
142 <div
143 className="flex h-full min-h-0 min-w-0 flex-col"
144 style={
145 collapsible && !maximized
146 ? { width: `calc(var(${RESIZABLE_WIDTH_PROPERTY}) - 1px)` }
147 : undefined
148 }
149 >
150 {props.children}
151 </div>
152 </div>
153 </div>
154 );