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

BrowserSurfaceSlot.tsx

103 lines3.7 KB

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