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 panels animate their outer width while keeping the content at
66 // full width. The slot moves without resizing, so measure on shell resizes too.
67 const panel = element.closest('[data-preview-panel-mode="inline"]');
68 if (panel) observed.push(panel);
69 // A sidebar opening or closing shifts the whole column, so a slot that keeps
70 // its size and its offset inside the column (the mini player on the left)
71 // still moves on screen. The gap element carries the sidebar's width.
72 observed.push(...document.querySelectorAll('[data-slot="sidebar-gap"]'));
73 const stopObserving = observeResize(observed, update);
74 window.addEventListener("resize", update);
75 window.addEventListener("scroll", update, true);
76 return () => {
77 stopObserving();
78 window.removeEventListener("resize", update);
79 window.removeEventListener("scroll", update, true);
80 if (updateRef.current === update) updateRef.current = null;
81 lease.release();
82 };
83 }, [fitSourceContent, tabId]);
84
85 useLayoutEffect(() => {
86 presentationRef.current = { visible, cornerRadius, zIndex };
87 updateRef.current?.();
88 }, [
89 cornerRadius,
90 // 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.
91 layoutVersion,
92 visible,
93 zIndex,
94 ]);
95
96 return <div ref={elementRef} className={className} data-browser-surface-slot={tabId} />;
97}