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

previewFocus.ts

32 lines1.5 KB

1export function isPreviewFocused(): boolean {
2 const activeElement = document.activeElement;
3 if (!(activeElement instanceof HTMLElement)) return false;
4 if (!activeElement.isConnected) return false;
5 if (activeElement.tagName.toLowerCase() === "webview") return true;
6 // Page overlays of a sheet-hosted browser render outside the panel, above its webview.
7 if (activeElement.closest("[data-browser-surface-overlay]")) return true;
8 // A side chat shares the preview's right panel but is a chat, not the preview.
9 return (
10 activeElement.closest("[data-preview-panel-mode]") !== null &&
11 activeElement.closest("[data-side-chat-pane]") === null
12 );
14​
15/** Narrower than isPreviewFocused, whose panel also hosts PR, diff, files and other surfaces. */
16export function isBrowserSurfaceFocused(): boolean {
17 const activeElement = document.activeElement;
18 if (!(activeElement instanceof HTMLElement) || !activeElement.isConnected) return false;
19 if (activeElement.tagName.toLowerCase() === "webview") return true;
20 if (activeElement.closest("[data-browser-surface-overlay]")) return true;
21 const panel = activeElement.closest("[data-preview-panel-mode]");
22 return panel?.querySelector("[data-preview-view]") != null;
24​
25export function isPanelFindAvailable(): boolean {
26 if (isBrowserSurfaceFocused()) return true;
27 const panel = document.activeElement?.closest("[data-preview-panel-mode]");
28 if (!panel) return false;
29 return Array.from(panel.querySelectorAll("[data-panel-find-responder]")).some(
30 (responder) => responder.getClientRects().length > 0,
31 );