Skip to content

fix(shortcuts): keep keyboard shortcuts working after a click in a mail body - #142

Closed
rcsarv wants to merge 2 commits into
mainfrom
fix/email-link-keyboard-shortcuts
Closed

rcsarv wants to merge 2 commits into
mainfrom
fix/email-link-keyboard-shortcuts

Conversation

@rcsarv

@rcsarv rcsarv commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Problem

After a click on a link in an email (or anywhere in its text), every keyboard shortcut stopped working: j/k, e, #, /, Escape. They stayed dead until the user clicked somewhere outside the message body.

Root cause

A mail body is rendered in a sandboxed <iframe>, which is a separate document. That is true of SandboxedEmailBody and of each chat-view bubble in @sarv-in/email-chat-view. A click inside it makes that frame the focused frame, so Chromium sends every later keydown to the frame's document. The shortcut listeners are on the app's document.

Coming back from the browser that the link opened in did not help. The window-focus restore in App.tsx leaves an element that already has focus alone, and the iframe is that element. Confirmed in real Chromium: after the click, document.activeElement is the IFRAME.

Fix

New apps/desktop/src/utils/frame-key-forwarding.ts, installed once in main.tsx:

  • Trigger. When the window blurs with a script-less frame as the active element (the same signal useClickAway uses), that frame's document starts forwarding keydowns.
  • Replay. Each key is replayed as a KeyboardEvent on the <iframe> element in the app's document, so it reaches the same listeners a key pressed in the app would. If an app listener handles it (calls preventDefault), the original key is prevented too.
  • Copy is unaffected. Focus is not pulled back out of the frame, because the text selection lives there. Unhandled keys such as Cmd/Ctrl+C keep their default.
  • Which frames. Only frames with sandbox set and no allow-scripts: the mail bodies. Extension panels run script and own their own keyboard, so they are left alone. The PDF viewer frame is unsandboxed and cross-origin, so it is not touched either.
  • Typing is not forwarded. Keys typed into a field or contenteditable region inside a mail, and IME composition, stay in the mail. The typing check now works across documents: useKeyboardShortcuts reuses the shared isTypingTarget, because an element inside a frame is not an instanceof the app's HTMLElement.
  • New documents keep forwarding. A frame's next document (a srcdoc change after a re-colour or image load) forwards too.
  • Attached once. Each document gets one listener however often the frame is focused, so a key never acts twice.
  • Sandbox unchanged. The frame sandbox and CSP are not changed.
  • All platforms. Behaviour is the same on macOS, Windows and Linux, because this is Chromium frame focus, not OS focus.

Also: the go-to-mode console.log lines in useKeyboardShortcuts now use the shared logger at debug level, and no longer log the key.

Tests

apps/desktop/test/unit/src/utils/frame-key-forwarding.test.tsx has 15 tests using real srcdoc frames in happy-dom. Each test names the regression it guards. They cover:

  • the replay: target, modifiers and repeat copied;
  • preventDefault passed back only when an app listener handled the key;
  • fields, IME and Process keys not forwarded;
  • scripted frames ignored;
  • a blur with no frame focused;
  • attaching only once per document;
  • a removed frame;
  • a reloaded document;
  • a document that cannot be reached;
  • uninstalling;
  • end to end with the real useKeyboardShortcuts: after a link click, d deletes and Escape closes, while d typed into a field in the mail deletes nothing.

Results:

  • 100% line and function coverage of the new file.
  • Mutation checks: removing the blur attach, the preventDefault hand-back or the reload re-attach each turns tests red.
  • Full suite (TURBO_FORCE=true pnpm test) green before the merge with main: core 4583, storage-node 1950, desktop 11873 (17 skipped, none new), site 144.
  • Tested by hand in the dev app.

This branch now includes a merge of main (1.5.0). The only conflict was in CHANGELOG.md: the entry sits under [Unreleased], above 1.5.0. dev already has this fix (1eac413).

🤖 Generated with Claude Code

…il body

A mail body renders in a sandboxed iframe (the standard reader and every
chat-view bubble), which is a document of its own. Clicking in it, a link
above all, made that frame the focused frame, and Chromium then delivered
every keydown to the frame's document. The shortcut listeners are on the
app's document, so j/k, e, #, / and Escape went dead until the user clicked
outside the body. Returning from the browser a link opened in did not help:
the window-focus restore leaves an already-focused element alone, and the
frame is that element.

Keydowns in a frame that runs no script (sandboxed without allow-scripts,
i.e. the mail bodies) are now replayed on the iframe element in the app's
document, attached when focus enters the frame (window blur with the frame
active) and re-attached when the frame loads a new document. If an app
listener handles the replay, the original is prevented too. Focus stays in
the frame, so copying a selection there keeps working; keys typed into a
field in the mail and IME composition are not forwarded; extension panels
(which run script) are left alone. No sandbox or CSP change.

The shortcut hook now uses the shared, realm-agnostic typing-target check,
and its go-to console.log lines move to the shared logger without the key.
@rcsarv

rcsarv commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favour of #143: the dev → main release PR already carries this fix (300e021, merged into dev as 1eac413). Merging both would duplicate the CHANGELOG entry.

@rcsarv rcsarv closed this Nov 25, 2026
@rcsarv
rcsarv deleted the fix/email-link-keyboard-shortcuts branch November 25, 2026 11:24
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