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

BrowserSurfaceSlot.tsx

147 lines5.5 KB

1import { type ReactNode, useLayoutEffect, useRef, useState } from "react";
2import { createPortal } from "react-dom";
3​
4import {
5 acquireBrowserSurface,
6 browserSurfaceElementId,
7 useBrowserSurfaceStore,
8} from "./browserSurfaceStore";
9import { observeResize } from "../lib/observeResize";
10​
11export function BrowserSurfaceSlot(props: {
12 readonly tabId: string;
13 readonly visible: boolean;
14 readonly cornerRadius?: number;
15 readonly zIndex?: number;
16 readonly layoutVersion?: string | number;
17 readonly className?: string;
18 readonly fitSourceContent?: boolean;
19 /** Page overlays (find bar, agent cursor) that must paint above the webview. */
20 readonly children?: ReactNode;
21}) {
22 const {
23 tabId,
24 visible,
25 cornerRadius = 0,
26 zIndex = 30,
27 layoutVersion,
28 className,
29 fitSourceContent = false,
30 children,
31 } = props;
32 const presentationRef = useRef({ visible, cornerRadius, zIndex });
33 const updateRef = useRef<(() => void) | null>(null);
34 const [lifted, setLifted] = useState<{ zIndex: number; container: Element } | null>(null);
35 const liftedRect = useBrowserSurfaceStore((state) =>
36 lifted === null ? null : (state.byTabId[tabId]?.rect ?? null),
37 );
38​
39 // The lease lives as long as this element for one tab, so the ref callback owns it.
40 const attachSurface = (element: HTMLDivElement) => {
41 let lease = acquireBrowserSurface(tabId, fitSourceContent);
42 // The webview lives in a fixed layer outside the slot, so a sheet's stacking context would cover
43 // it. Lift it above the sheet, and its overlays with it, since they cannot leave the sheet's context.
44 const sheet = element.closest('[data-slot="sheet-viewport"]');
45 const minZIndex = sheet ? Number(getComputedStyle(sheet).zIndex) + 1 || 0 : 0;
46 if (sheet?.parentElement && minZIndex > 0) {
47 setLifted({ zIndex: minZIndex, container: sheet.parentElement });
48 }
49 const update = () => {
50 const rect = element.getBoundingClientRect();
51 const presentation = presentationRef.current;
52 const zIndex = Math.max(presentation.zIndex, minZIndex);
53 const presented = lease.present(
54 {
55 x: Math.round(rect.x),
56 y: Math.round(rect.y),
57 width: Math.max(1, Math.round(rect.width)),
58 height: Math.max(1, Math.round(rect.height)),
59 },
60 presentation.visible && rect.width > 0 && rect.height > 0,
61 presentation.cornerRadius,
62 zIndex,
63 );
64 if (presentation.visible && !presented) {
65 lease.release();
66 lease = acquireBrowserSurface(tabId, fitSourceContent);
67 lease.present(
68 {
69 x: Math.round(rect.x),
70 y: Math.round(rect.y),
71 width: Math.max(1, Math.round(rect.width)),
72 height: Math.max(1, Math.round(rect.height)),
73 },
74 rect.width > 0 && rect.height > 0,
75 presentation.cornerRadius,
76 zIndex,
77 );
78 }
79 };
80 updateRef.current = update;
81 update();
82 const observed: Element[] = [element];
83 // Inline shells animate without resizing the slot, so observe the shell too.
84 const panel = element.closest('[data-preview-panel-mode="inline"]');
85 if (panel) observed.push(panel);
86 // Sidebar width changes move the slot without changing its size or column offset.
87 observed.push(...document.querySelectorAll('[data-slot="sidebar-gap"]'));
88 const stopObserving = observeResize(observed, update);
89 window.addEventListener("resize", update);
90 window.addEventListener("scroll", update, true);
91 return () => {
92 stopObserving();
93 window.removeEventListener("resize", update);
94 window.removeEventListener("scroll", update, true);
95 if (updateRef.current === update) updateRef.current = null;
96 lease.release();
97 };
98 };
99​
100 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes visibility, radius and stacking to the desktop browser surface after layout
101 useLayoutEffect(() => {
102 presentationRef.current = { visible, cornerRadius, zIndex };
103 updateRef.current?.();
104 }, [
105 cornerRadius,
106 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A layoutVersion bump means the slot moved without resizing, so it re-presents at the new position.
107 layoutVersion,
108 visible,
109 zIndex,
110 ]);
111​
112 return (
113 <>
114 <div
115 ref={attachSurface}
116 className={className}
117 data-browser-surface-slot={tabId}
118 // A modal sheet aria-hides the webview's fixed layer, so the slot adopts it in the
119 // accessibility tree; the webview cannot move into the sheet without losing its guest.
120 aria-owns={lifted !== null && visible ? browserSurfaceElementId(tabId) : undefined}
121 />
122 {lifted === null
123 ? children
124 : liftedRect && visible
125 ? createPortal(
126 // In the sheet's portal and marked as a nested portal, so the dialog keeps treating
127 // it as its own content for focus, outside presses and aria-hidden.
128 <div
129 className="pointer-events-none fixed overflow-hidden"
130 style={{
131 left: liftedRect.x,
132 top: liftedRect.y,
133 width: liftedRect.width,
134 height: liftedRect.height,
135 zIndex: Math.max(zIndex, lifted.zIndex) + 1,
136 }}
137 data-browser-surface-overlay={tabId}
138 data-base-ui-portal=""
139 >
140 {children}
141 </div>,
142 lifted.container,
143 )
144 : null}
145 </>
146 );