Open History to see the last commit
1import { useLayoutEffect, useRef } from "react";
2
3import { acquireBrowserSurface } from "./browserSurfaceStore";
4import { observeResize } from "../lib/observeResize";
5
6export function BrowserSurfaceSlot(props: {
7 readonly tabId: string;
8 readonly visible: boolean;
9 readonly cornerRadius?: number;
10 readonly zIndex?: number;
11 readonly layoutVersion?: string | number;
12 readonly className?: string;
13 readonly fitSourceContent?: boolean;
14}) {
15 const {
16 tabId,
17 visible,
18 cornerRadius = 0,
19 zIndex = 30,
20 layoutVersion,
21 className,
22 fitSourceContent = false,
23 } = props;
24 const elementRef = useRef<HTMLDivElement | null>(null);
25 const presentationRef = useRef({ visible, cornerRadius, zIndex });
26 const updateRef = useRef<(() => void) | null>(null);
27
28 useLayoutEffect(() => {
29 const element = elementRef.current;
30 if (!element) return;
31 let lease = acquireBrowserSurface(tabId, fitSourceContent);
32 const update = () => {
33 const rect = element.getBoundingClientRect();
34 const presentation = presentationRef.current;
35 const presented = lease.present(
36 {
37 x: Math.round(rect.x),
38 y: Math.round(rect.y),
39 width: Math.max(1, Math.round(rect.width)),
40 height: Math.max(1, Math.round(rect.height)),
41 },
42 presentation.visible && rect.width > 0 && rect.height > 0,
43 presentation.cornerRadius,
44 presentation.zIndex,
45 );
46 if (presentation.visible && !presented) {
47 lease.release();
48 lease = acquireBrowserSurface(tabId, fitSourceContent);
49 lease.present(
50 {
51 x: Math.round(rect.x),
52 y: Math.round(rect.y),
53 width: Math.max(1, Math.round(rect.width)),
54 height: Math.max(1, Math.round(rect.height)),
55 },
56 rect.width > 0 && rect.height > 0,
57 presentation.cornerRadius,
58 presentation.zIndex,
59 );
60 }
61 };
62 updateRef.current = update;
63 update();
64 const observed: Element[] = [element];
65 // Inline shells animate without resizing the slot, so observe the shell too.
66 const panel = element.closest('[data-preview-panel-mode="inline"]');
67 if (panel) observed.push(panel);
68 // Sidebar width changes move the slot without changing its size or column offset.
69 observed.push(...document.querySelectorAll('[data-slot="sidebar-gap"]'));
70 const stopObserving = observeResize(observed, update);
71 window.addEventListener("resize", update);
72 window.addEventListener("scroll", update, true);
73 return () => {
74 stopObserving();
75 window.removeEventListener("resize", update);
76 window.removeEventListener("scroll", update, true);
77 if (updateRef.current === update) updateRef.current = null;
78 lease.release();
79 };
80 }, [fitSourceContent, tabId]);
81
82 useLayoutEffect(() => {
83 presentationRef.current = { visible, cornerRadius, zIndex };
84 updateRef.current?.();
85 }, [
86 cornerRadius,
87 // 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.
88 layoutVersion,
89 visible,
90 zIndex,
91 ]);
92
93 return <div ref={elementRef} className={className} data-browser-surface-slot={tabId} />;
94}