Skip to content

focusTrap: the first-launch wizard holds keyboard focus; overlays tolerate a body pointer lock - #3855

Merged
mstomar125 merged 4 commits into
mainfrom
fix/3837-wizard-modal-focus
Oct 20, 2026
Merged

mstomar125 merged 4 commits into
mainfrom
fix/3837-wizard-modal-focus

Conversation

@mstomar125

@mstomar125 mstomar125 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

The second finding of #3837: the first-launch wizard now holds keyboard focus. When another element (the daily-journal editor's autofocus) takes focus after the wizard opens, focusTrap returns it to the dialog, so Escape, Tab and typing no longer go to the journal behind the wizard. It also makes the hand-rolled modals (onboarding-wizard, delete-confirmation-modal, subtree-access-denied-modal) tolerate a body pointer-events: none by setting pointer-events: auto on their overlays.

What it does not fix

  • The click-lock against an extension dialog. That dialog is portalled to <body> at z-50, above the wizard's z-index: 0 stacking context, so it still renders over the wizard and receives the click. pointer-events: auto does not change that. The Sync extension ships a turn-taking workaround for this.
  • Suggested fixes 2 (an extension-API signal for core modals) and 3 (docs note) remain open.

focusTrap behaviour

  • Initial focus is unchanged (first focusable element).
  • A reclaim goes to the element that last held focus inside the dialog, otherwise to the dialog container itself (given tabindex="-1" if it has none). The initial focus move is not remembered, so a reclaim never defaults to the Close button.
  • focusin on document/body is ignored; a target inside another aria-modal="true" element is left alone.
  • Only the topmost open trap reclaims (module-level stack) and a reclaiming flag guards re-entry, so two traps cannot ping-pong focus.

Testing

  • vitest on src/tests/actions/focus-trap.test.ts, all of src/tests/components and src/tests/extension-api: 1133 passed. New cases: reclaim target is the last-focused inner element and not the first control; container fallback; document/body targets; two nested traps do not loop; the trap beneath reclaims once the top one is destroyed; the wizard reclaims to its primary action, not Close. The overlay pointer-events rule is asserted on the rule text (jsdom does not apply component CSS).
  • eslint and svelte-check: clean. Not verified in a real browser with an extension dialog open.

Refs #3837

🤖 Generated with Claude Code

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review (initial) - #3855

Verdict: REQUEST CHANGES

Reviewed at head, against #3837. Ran bun x vitest run src/tests/actions/focus-trap.test.ts src/tests/components/onboarding-wizard.test.ts in a throwaway worktree: 20 passed. No real-browser run (as the PR itself says).

Summary

The keyboard-focus half (focusTrap reclaims focus from the journal editor) is a sound fix with one targeting problem. The pointer-events: auto half, which is what the issue's headline symptom depends on, most likely does not fix it, and the PR is marked Closes #3837 without a browser check.

Findings

  • Blocking onboarding-wizard.svelte:675-677 (and the same in delete-confirmation-modal.svelte:45-47, subtree-access-denied-modal.svelte:39-41): pointer-events: auto does not change what the user hits in the reported scenario. The issue measured elementFromPoint at the wizard's Next = [data-slot=dialog-overlay]. That is a stacking result as well as an inheritance one: the wizard lives inside .theme-provider { position: relative; z-index: 0 } (its own stacking context) while the bits-ui overlay is portalled to <body> at z-50 and covers the viewport. The PR body states the z-index/stacking difference is unchanged, so the overlay still paints above the wizard and still receives the click, which bits-ui treats as an outside click and dismisses the extension dialog. pointer-events: auto only matters where nothing opaque is above the wizard. Either (a) verify in a real browser (the issue names the harness: wizard + ThemeProvider + open bits-ui Dialog; check elementFromPoint at Next and that one click advances) and, if it still fails, fix the stacking (e.g. portal the hand-rolled modals to <body> with a z-index above the dialog, or raise the theme provider), or (b) change Closes #3837 to Refs #3837 and state plainly which symptoms remain. The tests only assert the CSS rule text (onboarding-wizard.test.ts "backdrop keeps pointer-events: auto"), so they prove nothing about the click.

    • Related, for completeness: auto on a full-viewport backdrop does not expose content behind it (the backdrop itself is the hit target, and clicks on it just run onClose), so I see no click-through risk from the change itself. Where a bits-ui dialog is open and the wizard is on top, a click on the wizard counts as an outside click for bits-ui and dismisses the extension dialog; acceptable, but note it.
  • Non-blocking (should fix) packages/desktop-app/src/lib/actions/focus-trap.ts:80: the reclaim target is focusable()[0], which in the wizard is the Close button (first control in the DOM, onboarding-wizard.svelte:~391), not the step's primary action that the wizard's own effect anchors on (onboarding-wizard.svelte:373). In the real timeline (editor autofocus lands 0.3-2 s after mount) focus ends on Close, so a stray Enter/Space dismisses the first-launch wizard. Prefer returning focus to the last-focused element inside the trap (track it on focusin where node.contains(target)), falling back to first focusable/node.

  • Non-blocking (should fix) focus-trap.ts:73-82 and src/lib/extension-api/ui.ts:25: focusTrap is exported to extensions, and the new document focusin listener now makes every trap contest focus. The only guard against two traps fighting is that the other element carries aria-modal="true". Two traps on dialogs without that attribute (an extension's hand-rolled modal, role="alertdialog", etc.) will ping-pong: each focus() fires the other's focusin synchronously, which can recurse until the stack overflows. All core callers do have aria-modal, and bits-ui's content sets it (bits-ui dialog.svelte.js:206), and base-node.svelte:973's date picker has it, so core-vs-core and core-vs-bits-ui are fine today. Add a re-entrancy guard (ignore focusin while the trap is itself moving focus) and/or a module-level stack so only the topmost trap reclaims; document the aria-modal requirement in the action's doc comment and the extension guide.

  • Non-blocking focus-trap.ts:78-80: focus moving into content portalled out of the dialog (a bits-ui select/popover/tooltip content inside a future wizard step, which usually is not aria-modal) would be pulled back and close the popover. No such control exists in the three current callers; worth a comment since the action is exported.

  • Non-blocking cleanup: the listener is removed in destroy() (verified, and tested by "stops reclaiming focus once destroyed"). previouslyFocused?.focus?.() runs after removal, so no re-trigger. Good.

  • Non-blocking a11y: aria-modal exception correctly uses closest() and excludes the trap's own subtree (!node.contains(otherModal)), so a trap whose own node is aria-modal is not mistaken for "another modal". Tab/Escape wiring is unchanged and now actually receives events. Good.

  • Non-blocking (test) the new focus tests only cover a plain textarea stealing focus; add a case for focus going to document.body/null target (no-op expected) and one for two traps to pin the loop behaviour above.

  • Informational suggested fixes 2 and 3 from the issue (extension-api modal signal, docs note) are not done; fine as scoped, but then the issue should not auto-close.

Requirements (#3837)

  • [~] Wizard holds keyboard focus / Escape works: addressed (see focus target comment).
  • Wizard takes clicks beside an open extension dialog: not demonstrated, likely not fixed (blocking finding).
  • Extension signal / docs: not in scope of this PR.

@mstomar125 mstomar125 changed the title Modals: overlays stay clickable under a body pointer lock; focusTrap reclaims focus focusTrap: the first-launch wizard holds keyboard focus; overlays tolerate a body pointer lock Oct 20, 2026

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review (re-review of 91af378 / 50a6ff3) - #3855

Verdict: APPROVE WITH NITS

Reviewed against #3837, covering the commits since the earlier REQUEST CHANGES. Ran bun x vitest run src/tests/actions/focus-trap.test.ts src/tests/components/onboarding-wizard.test.ts in a throwaway worktree: 25 passed (16 + 9). No real-browser run.

Earlier findings

  • Blocking (Closes with an unproven pointer-events fix): resolved. The text is Refs #3837 and "What it does not fix" states the extension-dialog click-lock (z-50 portal over the wizard's z-index 0 context) is still open. The comment in the three modals ("overlay inherits it and takes no click") is accurate only for the body-inherits case. Nothing more is needed.
  • Reclaim target was the Close button: resolved. focus-trap.ts:82-109 reclaims to the last-focused element inside the trap, else the container (given tabindex=-1), and focus-trap.ts:117-120 registers the listener after the initial focus move so that move is not remembered. The wizard test pins the primary action.
  • Two traps ping-pong: resolved. Module-level stack (:35, push :119, splice :129-130) plus the reclaiming guard (:80, :92, :98-108). Destroy removes by indexOf, so out-of-order destruction is safe and the next-topmost trap becomes active. Nothing holds a node after destroy(), so no leak. All DOM access is inside the action, so SSR import is safe.
  • body/document focusin: resolved (:90), tested.

New / remaining findings

  • Non-blocking focus-trap.ts:100-105: if lastFocusedInside is still connected but now disabled or hidden (the wizard's Next/primary after a step change), .focus() is a silent no-op and the editor keeps focus; the container fallback is only reached when it is disconnected. After the call, check node.contains(document.activeElement) and fall back to the container.
  • Non-blocking focus-trap.ts:95-96: only aria-modal="true" content is exempt. Portalled non-modal content (bits-ui select/popover/menu, tooltips) opened from a future step would be pulled back and close. No current caller has one; add one sentence to the doc comment (:15-17) since the action is exported to extensions (extension-api/ui.ts).
  • Non-blocking focus-trap.ts:103: setting tabindex=-1 on the container mutates the DOM and can show a focus ring on the dialog on reclaim; confirm in a real browser.
  • Non-blocking (test) no case for destroying a lower trap while a higher one stays open, or for a disabled last-focused element. The stack code handles the first by inspection.
  • Informational the pointer-events: auto change (onboarding-wizard.svelte, delete-confirmation-modal.svelte, subtree-access-denied-modal.svelte) is only asserted as rule text and not shown to change a click in a browser. Fine under Refs.

No blocking issues.

mstomar125 and others added 4 commits October 20, 2026 13:58
…cusTrap reclaim focus

The onboarding wizard, delete-confirmation and subtree-access-denied overlays
set pointer-events: auto so an open bits-ui dialog's body pointer lock is not
inherited. focusTrap now listens for focusin and returns focus into the dialog
when an outside element (the journal editor's autofocus) takes it, except
when the target sits in another aria-modal element.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The focusin reclaim returned focus to the first focusable element (the wizard's
Close button). It now returns to the element that last held focus inside the
dialog, or the dialog container, ignores document/body targets, and only the
topmost open trap reclaims.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant