Skip to content

Write HTML. Render video. Built for agents.

fix(studio): playback keys do nothing while a dialog is open (#4623)

Miguel Ángel358c04e2e214
diff --git a/packages/studio/src/player/hooks/useTimelinePlayer.test.ts b/packages/studio/src/player/hooks/useTimelinePlayer.test.ts
index b45c46d0c8..cc8e996f20 100644
--- a/packages/studio/src/player/hooks/useTimelinePlayer.test.ts
+++ b/packages/studio/src/player/hooks/useTimelinePlayer.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { Window } from "happy-dom";
import {
buildStandaloneRootTimelineElement,
@@ -597,3 +597,95 @@ describe("shouldIgnorePlaybackShortcutEvent", () => {
).toBe(false);
});
});
+
+describe("shouldIgnorePlaybackShortcutEvent while a modal dialog is open", () => {
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ // happy-dom has no layout, so the `hidden` attribute stands in for checkVisibility.
+ function stubStudioDocument(markup: string): Document {
+ const document = createDocument(markup);
+ const elementProto = document.defaultView!.Element.prototype as {
+ checkVisibility?: (this: Element) => boolean;
+ };
+ elementProto.checkVisibility = function () {
+ return this.closest("[hidden]") === null;
+ };
+ vi.stubGlobal("document", document);
+ return document;
+ }
+
+ it("ignores playback keys with focus still on the page behind the dialog", () => {
+ const document = stubStudioDocument(
+ `<div role="dialog" aria-modal="true"><p>Lint results</p></div>`,
+ );
+ for (const code of ["Space", "ArrowRight", "KeyL"]) {
+ expect(
+ shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent(code, { target: document.body })),
+ ).toBe(true);
+ }
+ });
+
+ it("ignores playback keys with focus inside the dialog", () => {
+ const document = stubStudioDocument(
+ `<div role="dialog" aria-modal="true" tabindex="-1"><p>Lint results</p></div>`,
+ );
+ const dialog = document.querySelector("[role='dialog']");
+ const text = document.querySelector("p");
+ expect(shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("Space", { target: dialog }))).toBe(
+ true,
+ );
+ expect(
+ shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("ArrowLeft", { target: text })),
+ ).toBe(true);
+ });
+
+ it("keeps playback keys for dialogs that are hidden, inert or not modal", () => {
+ for (const markup of [
+ `<div role="dialog" aria-modal="true" hidden></div>`,
+ `<div inert><div role="dialog" aria-modal="true"></div></div>`,
+ `<div role="dialog"></div>`,
+ ]) {
+ const document = stubStudioDocument(markup);
+ expect(
+ shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("Space", { target: document.body })),
+ ).toBe(false);
+ }
+ });
+
+ it("counts a dialog under fullscreen only when the fullscreen element holds it", () => {
+ const document = stubStudioDocument(
+ `<div id="stage"><div id="inner" role="dialog" aria-modal="true"></div></div>` +
+ `<div id="outer" role="dialog" aria-modal="true"></div>`,
+ );
+ const space = () =>
+ shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("Space", { target: document.body }));
+ const fullscreenOn = (id: string) =>
+ Object.defineProperty(document, "fullscreenElement", {
+ configurable: true,
+ value: document.getElementById(id),
+ });
+ fullscreenOn("stage");
+ expect(space()).toBe(true);
+ document.getElementById("inner")!.remove();
+ expect(space()).toBe(false);
+ });
+
+ it("counts an aria-modal dialog where the browser has no checkVisibility", () => {
+ const document = createDocument(`<div role="dialog" aria-modal="true"></div>`);
+ delete (document.defaultView!.Element.prototype as { checkVisibility?: unknown })
+ .checkVisibility;
+ vi.stubGlobal("document", document);
+ expect(
+ shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("Space", { target: document.body })),
+ ).toBe(true);
+ });
+
+ it("keeps playback keys when no dialog is open", () => {
+ const document = stubStudioDocument(`<main><p>Preview</p></main>`);
+ expect(
+ shouldIgnorePlaybackShortcutEvent(mockKeyboardEvent("Space", { target: document.body })),
+ ).toBe(false);
+ });
+});
diff --git a/packages/studio/src/player/lib/playbackShortcuts.ts b/packages/studio/src/player/lib/playbackShortcuts.ts
index 4fb5b1853a..eef7b5e0fb 100644
--- a/packages/studio/src/player/lib/playbackShortcuts.ts
+++ b/packages/studio/src/player/lib/playbackShortcuts.ts
@@ -41,6 +41,23 @@ export function shouldIgnorePlaybackShortcutTarget(target: EventTarget | null):
);
}
+const MODAL_DIALOG_SELECTOR = "[role=dialog][aria-modal=true]";
+
+// An open modal owns the keyboard wherever focus sits, preview iframe included. It counts only
+// when shown: visible, not inert, and not behind a fullscreen element that leaves it out.
+function isModalDialogOpen(): boolean {
+ const doc = globalThis.document;
+ if (!doc) return false;
+ const fullscreen = doc.fullscreenElement;
+ return Array.from(doc.querySelectorAll(MODAL_DIALOG_SELECTOR)).some(
+ (dialog) =>
+ (!fullscreen || fullscreen.contains(dialog)) &&
+ !dialog.closest("[inert]") &&
+ (typeof dialog.checkVisibility !== "function" ||
+ dialog.checkVisibility({ visibilityProperty: true })),
+ );
+}
+
interface PlaybackShortcutCaptionState {
isCaptionEditMode: boolean;
selectedCaptionSegmentCount: number;
@@ -60,6 +77,7 @@ export function shouldIgnorePlaybackShortcutEvent(
): boolean {
if (event.metaKey || event.ctrlKey || event.altKey) return true;
if (shouldIgnorePlaybackShortcutTarget(event.target)) return true;
+ if (isModalDialogOpen()) return true;
return (
PLAYBACK_FRAME_STEP_CODES.has(event.code) &&
captionState.isCaptionEditMode &&