Open source CapCut alternative.
TypeScript
fix(site): a dialog on a hidden page no longer holds the keyboard
David Leeb365d2d4070
diff --git a/site/src/cut/lib/shortcutGate.test.ts b/site/src/cut/lib/shortcutGate.test.tsindex 9fa8c875..708e092f 100644--- a/site/src/cut/lib/shortcutGate.test.ts+++ b/site/src/cut/lib/shortcutGate.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from "bun:test";-import { isDeleteKey, tellingKey, textEntryOf, typingOf } from "./shortcutGate";+import { isDeleteKey, popupOnTop, tellingKey, textEntryOf, typingOf } from "./shortcutGate";const key = (k: string, code = "", mods: Partial<KeyboardEvent> = {}) =>({ key: k, code, metaKey: false, ctrlKey: false, altKey: false, shiftKey: false, ...mods }) as KeyboardEvent;@@ -59,3 +59,22 @@ describe("what a decline is worth reporting", () => {expect(tellingKey(key("b", "KeyB", { ctrlKey: true, altKey: true }))).toBe(false);});});++describe("which dialog holds the keyboard", () => {+ const popup = (attrs: string[], shown: boolean) => ({+ hasAttribute: (name: string) => attrs.includes(name),+ checkVisibility: () => shown,+ });++ test("an open dialog on screen holds it", () => {+ expect(popupOnTop(popup(["data-open"], true))).toBe(true);+ });++ test("a dialog on its way out lets go", () => {+ expect(popupOnTop(popup(["data-closed"], true))).toBe(false);+ });++ test("a dialog left open on a page the router keeps hidden lets go", () => {+ expect(popupOnTop(popup(["data-open"], false))).toBe(false);+ });+});diff --git a/site/src/cut/lib/shortcutGate.ts b/site/src/cut/lib/shortcutGate.tsindex bca62165..9d16132b 100644--- a/site/src/cut/lib/shortcutGate.ts+++ b/site/src/cut/lib/shortcutGate.ts@@ -76,15 +76,28 @@ export function keyboardToEditor(): void {if (isTextEntry(el)) (el as HTMLElement).blur();}+/** The part of a dialog's popup that decides whether it holds the keyboard. */+export interface Popup {+ hasAttribute: (name: string) => boolean;+ checkVisibility: () => boolean;+}+/**- * Whether a dialog stands over the editor, which owns the keyboard while it is- * open. A popup keeps its element through its exit animation, and on a machine- * dropping frames that runs long; Base UI marks those `data-closed`, and a- * dialog on its way out has no claim on the keys.+ * Whether this popup stands over the editor. A popup keeps its element through+ * its exit animation, and on a machine dropping frames that runs long; Base UI+ * marks those `data-closed`, and a dialog on its way out has no claim on the+ * keys. A popup nobody can see has none either: the router keeps a page you+ * navigated away from mounted, hidden, and a dialog left open on it is still+ * in the document with the editor drawn on top of it.*/+export const popupOnTop = (el: Popup): boolean =>+ !el.hasAttribute("data-closed") && el.checkVisibility();++/** Whether a dialog stands over the editor, which owns the keyboard while it+ * is open. */export function dialogOnTop(): boolean {for (const el of document.querySelectorAll('[data-slot="dialog-content"]')) {- if (!el.hasAttribute("data-closed")) return true;+ if (popupOnTop(el)) return true;}return false;}