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

BrowserSurfaceSlot.tsx

99 lines3.5 KB

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