Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(editor): restore focus when a modal closes, commit the dialog ref in its openers

EtienneLescot95e350e0aab1
diff --git a/src/components/ai-edition/Modals.tsx b/src/components/ai-edition/Modals.tsx
index 6ee0200bd..cc077efd1 100644
--- a/src/components/ai-edition/Modals.tsx
+++ b/src/components/ai-edition/Modals.tsx
@@ -98,12 +98,21 @@ export function ModalShell({
const tc = useScopedT("common");
const dialogRef = useRef<HTMLDivElement | null>(null);
useEscape(open && closeOnEscape, onClose);
- // Move focus into the dialog as it opens. The app menu closes without restoring focus to
- // its trigger — right for a pointer user — so otherwise the opener is left on
- // document.body: Tab then walks the editor *behind* the backdrop, and a screen reader is
- // never told a dialog appeared.
+ // Move focus into the dialog as it opens, and hand it back to whatever had it when the
+ // dialog goes. The app menu closes without restoring focus to its trigger — right for a
+ // pointer user — so otherwise the opener is left on document.body: Tab then walks the
+ // editor *behind* the backdrop, and a screen reader is never told a dialog appeared.
+ // Closing has the mirror problem: the focused node is the one being unmounted.
useEffect(() => {
- if (open) dialogRef.current?.focus();
+ if (!open) return;
+ // Whatever opened this. Often document.body (the app menu unmounts its own row before
+ // the dialog mounts), in which case restoring is a no-op; the panel's gear is still
+ // there, and gets it back.
+ const opener = document.activeElement;
+ dialogRef.current?.focus();
+ return () => {
+ if (opener instanceof HTMLElement && opener.isConnected) opener.focus();
+ };
}, [open]);
if (!open) return null;
return (
diff --git a/src/components/ai-edition/NewEditorShell.dialogShortcuts.test.tsx b/src/components/ai-edition/NewEditorShell.dialogShortcuts.test.tsx
new file mode 100644
index 000000000..c0f3bdad7
--- /dev/null
+++ b/src/components/ai-edition/NewEditorShell.dialogShortcuts.test.tsx
@@ -0,0 +1,179 @@
+// @vitest-environment jsdom
+// The editor's shortcuts are bound on `window` and only skip inputs, textareas and
+// contentEditable targets. A modal's own controls are buttons, and the app menu closes without
+// restoring focus, so with a dialog open `e.target` is document.body and every shortcut used to
+// run underneath the backdrop — Delete destroying the selected region, Ctrl+O stacking a second
+// aria-modal dialog, `?` stacking the shortcuts dialog on top of the one already there.
+//
+// The guard reads `isDialogOpen()` (EditorDialogsContext, answered from a ref) and
+// `isConfigOpen`. Both are asserted through the shell's real keydown handler here.
+
+import "@testing-library/jest-dom";
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const openConfig = vi.fn();
+
+// The shortcuts dialog is one of the two things the guard has to suppress, and its opener is
+// the cheapest observable in the whole handler: `?` is the only shortcut that survives the
+// `hasProject` gate, so it works without loading a project.
+vi.mock("@/contexts/ShortcutsContext", async () => {
+ const { DEFAULT_SHORTCUTS } = await import("@/lib/shortcuts");
+ return {
+ useShortcuts: () => ({
+ shortcuts: DEFAULT_SHORTCUTS,
+ isMac: false,
+ isConfigOpen: false,
+ openConfig,
+ closeConfig: () => {
+ /* not exercised here */
+ },
+ setShortcuts: () => {
+ /* not exercised here */
+ },
+ persistShortcuts: () => Promise.resolve(true),
+ }),
+ };
+});
+
+vi.mock("@/contexts/I18nContext", () => ({
+ useI18n: () => ({
+ locale: "en",
+ setLocale: () => {
+ /* fixed locale */
+ },
+ }),
+ useScopedT: () => (key: string) => key,
+}));
+
+import { EditorDialogsProvider, useEditorDialogActions } from "@/contexts/EditorDialogsContext";
+import { NewEditorShell } from "./NewEditorShell";
+
+let dialogActions: ReturnType<typeof useEditorDialogActions> | null = null;
+
+function CaptureDialogActions() {
+ dialogActions = useEditorDialogActions();
+ return null;
+}
+
+function renderShell() {
+ return render(
+ <EditorDialogsProvider>
+ <CaptureDialogActions />
+ <NewEditorShell />
+ </EditorDialogsProvider>,
+ );
+}
+
+/** Shortcuts are bound on `window` and read `e.target`; with a modal open that is the body. */
+function pressOnBody(init: KeyboardEventInit) {
+ fireEvent.keyDown(document.body, init);
+}
+
+beforeEach(() => {
+ openConfig.mockClear();
+ dialogActions = null;
+ // No preload in jsdom, and no scrolling either; the chat transcript pins itself to the
+ // bottom on every render.
+ (window as unknown as { electronAPI?: unknown }).electronAPI = {
+ onAiEditionChatEvent: () => () => {
+ /* unsubscribe */
+ },
+ setTitleBarOverlay: () => {
+ /* no native titlebar */
+ },
+ setHasUnsavedChanges: () => {
+ /* no window close guard */
+ },
+ onRequestCloseConfirm: () => () => {
+ /* unsubscribe */
+ },
+ onRequestSaveBeforeClose: () => () => {
+ /* unsubscribe */
+ },
+ sendCloseConfirmResponse: () => {
+ /* nothing is closing this window */
+ },
+ // The only two other members the editor tree reaches without optional chaining. Both
+ // are user-driven, not mount-driven; they are here so a stray call is a no-op rather
+ // than a crash that reads as a failure of the thing under test.
+ findRecordingCamera: () => Promise.resolve(null),
+ preparePreviewAudioTrack: () => Promise.resolve(null),
+ };
+ Element.prototype.scrollTo = () => {
+ /* no scrolling in jsdom */
+ };
+ // jsdom ships neither; the stage and the timeline both measure themselves.
+ (globalThis as unknown as { ResizeObserver?: unknown }).ResizeObserver = class {
+ observe() {
+ /* never fires: nothing has a layout in jsdom */
+ }
+ unobserve() {
+ /* see observe */
+ }
+ disconnect() {
+ /* see observe */
+ }
+ };
+});
+
+afterEach(() => {
+ cleanup();
+ (window as unknown as { electronAPI?: unknown }).electronAPI = undefined;
+});
+
+describe("NewEditorShell shortcuts, with a dialog over the editor", () => {
+ it("routes ? to the shortcuts dialog while nothing is open", () => {
+ renderShell();
+
+ pressOnBody({ key: "?" });
+
+ expect(openConfig).toHaveBeenCalledTimes(1);
+ });
+
+ it("suppresses ? once a dialog owns the screen, and resumes when it closes", () => {
+ renderShell();
+
+ act(() => {
+ dialogActions?.openDialog("providers");
+ });
+ pressOnBody({ key: "?" });
+ expect(openConfig).not.toHaveBeenCalled();
+
+ act(() => {
+ dialogActions?.closeDialog();
+ });
+ pressOnBody({ key: "?" });
+ expect(openConfig).toHaveBeenCalledTimes(1);
+ });
+
+ // Ctrl+O is handled before the `hasProject` gate, so it fired whatever the editor's state —
+ // this is the one that put a SECOND aria-modal dialog on screen, both of them emitting the
+ // hardcoded `id="modal-title"`. Its handler is async (it awaits the unsaved-changes prompt
+ // before opening the picker), hence the async act: a synchronous assertion here would pass
+ // whether the guard is there or not.
+ it("opens the project picker on Ctrl+O while nothing is open", async () => {
+ renderShell();
+
+ await act(async () => {
+ pressOnBody({ key: "o", ctrlKey: true });
+ });
+
+ // The provider dialog is mounted in App.tsx, not here, so the shell renders no dialog of
+ // its own unless Ctrl+O got through.
+ expect(screen.getByRole("dialog")).toBeInTheDocument();
+ });
+
+ it("suppresses Ctrl+O once a dialog owns the screen", async () => {
+ renderShell();
+
+ act(() => {
+ dialogActions?.openDialog("providers");
+ });
+ await act(async () => {
+ pressOnBody({ key: "o", ctrlKey: true });
+ });
+
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ });
+});
diff --git a/src/contexts/EditorDialogsContext.tsx b/src/contexts/EditorDialogsContext.tsx
index e05885512..4da8d9f9a 100644
--- a/src/contexts/EditorDialogsContext.tsx
+++ b/src/contexts/EditorDialogsContext.tsx
@@ -50,15 +50,22 @@ export function useEditorDialogActions(): EditorDialogsActions {
export function EditorDialogsProvider({ children }: { children: ReactNode }) {
const [section, setSection] = useState<EditorDialogSection | null>(null);
// Mirrored so `isDialogOpen` can read the current section without the actions value having
- // to depend on it. Written during render rather than from an effect: a keystroke arriving
- // between the state change and the effect would otherwise get the previous answer.
- const sectionRef = useRef(section);
- sectionRef.current = section;
+ // to depend on it. Written by the two openers, not during render and not from an effect:
+ // during render a discarded one would leave the ref claiming a dialog that never committed,
+ // and from an effect a keystroke landing between the click and the commit would still get
+ // the previous answer. `setSection` is called from nowhere else, so the two cannot drift.
+ const sectionRef = useRef<EditorDialogSection | null>(null);
const actions = useMemo<EditorDialogsActions>(
() => ({
- openDialog: (next) => setSection(next),
- closeDialog: () => setSection(null),
+ openDialog: (next) => {
+ sectionRef.current = next;
+ setSection(next);
+ },
+ closeDialog: () => {
+ sectionRef.current = null;
+ setSection(null);
+ },
isDialogOpen: () => sectionRef.current !== null,
}),
[],