Skip to content

fix(smart-toolbar): keep keyboard focus after Remove and Restore in the phone sheet - #75

Merged
HMarzban merged 16 commits into
mainfrom
fix/phone-restore-focus
Nov 15, 2026
Merged

HMarzban merged 16 commits into
mainfrom
fix/phone-restore-focus

Conversation

@HMarzban

Copy link
Copy Markdown
Contributor

What and why

In the phone "Text format" sheet (Aa), Your formats' Remove buttons and Restore ‘X’ (PR #54) unmount the button you activate. A hardware-keyboard user (tablet or phone with a keyboard, switch access) lost their place: focus ended up on the sheet itself, and the next Tab started again from the top of the sheet.

Focus now moves to a sensible target:

  • After a remove: the next remove button. After the last one it is the previous button, and once the list is empty it is the "Your formats" heading (tabindex="-1" set at focus time, with the buttons' 2px focus ring and a scroll margin so the ring clears the sheet's top edge).
  • After Restore ‘X’: the restored format's apply button.
  • Taps: they move no focus. The buttons preventDefault on mousedown, so a tap never focused them, and focus is only moved when the activated button had it.
  • Announcements: the status pill's ("Removed ‘X’" / "Restored ‘X’") are unchanged; announce, remove-format.ts and saved-formats.ts are untouched.

Root cause

  1. The unmounted button took focus with it. Removing or restoring a format re-renders the list, and the focused button is unmounted. Its focus is dropped, and the dialog's focus manager parks it on the popup DIV itself (observed: document.activeElement was the dialog, not body), so the place in the sheet is lost. Nothing chose a successor.
    • The fix: SheetFormats records a one-shot intent in the click handler, { removedAt } or { restored }, and only when event.currentTarget had focus. A useLayoutEffect keyed on the saved formats consumes it before paint.
    • The effect moves focus only while it is still dropped, meaning activeElement is an ancestor of the list (the dialog or body). A restore that waits on the engine chunk therefore never pulls focus from somewhere the user moved meanwhile.
  2. The opening focus fired again. The ref that focuses the first remove button when the sheet opens at its Remove list (the Save dialog's cap link) was re-attached to every new first row. After removing and restoring the first format, it fired again and stole focus from the restored format. It is now one-shot.

How verified

  • New e2e in mobile-bar-followups.spec.ts ("Your formats keep keyboard focus in the sheet", plus the cap-link case). All four fail on main and pass now:
    • --repeat-each=5: 20/20 on Chromium.
    • WebKit project: 4/4.
    • The cap-link case also fails with only the one-shot fix reverted.
    • Axe runs on the heading-focused state.
  • Targeted specs: mobile-bar, mobile-bar-followups and paint-format pass 96/96 on Chromium (--workers=2).
  • Checks:
    • bun run check: the only warnings are 7 that were already on main, in other files.
    • bun run lint and bun run typecheck are clean.
    • bun run test: 1234/1234 on the rerun. The first run had 1 failure in a file this PR does not touch (shared machine), and no unit test covers these files.
  • Screenshots (before/after, 375 light and dark, dev badge hidden) are in the local scratchpad wf2/phone-restore-focus/:
    • remove middle → next button ringed
    • remove last → previous button ringed
    • list empty → heading ringed
    • restore → format ringed
    • Before, nothing is ringed in any of these states.
    • At 1440 the phone sheet does not render (the phone bar is gated below 768px), so the 1440 shots show the desktop unchanged.

Budgets

  • Eager editor chunk: 397,407 → 397,408 B gz (+1 B). This is build noise: only lazy phone-bar code changed, and the heading's CSS lands in a non-eager CSS chunk (checked with grep).
  • Palette first Alt+/: 0 B. The palette imports only formats/format-preview, which is unchanged, so PALETTE_BUDGET was not needed.

Follow-ups

  • No test holds the formatting engine chunk during a restore. By design the intent waits for the next formats change, but that cold-engine path is unverified.
  • The commit history has a small CHANGELOG correction commit (an amend was not possible here). Squash-merge folds it in.

The phone status pill spans the docked bar's width 8px above it, in
the band where the presence and Discord/GitHub/Privacy chips float, so
a refused "/" pick, a removed format's Undo or an unavailable cell's
reason covered them. Hide the chips while the bar holds a pill, as
they already hide for the selection bar; they come back when it closes.
A "/" typed while an earlier fetch of the menu chunk was still failing
(the chunk runtime fetches a failed chunk once more, a moment later)
joined that fetch. When it failed, nothing loaded the chunk again while
the menu stayed open, so the menu never showed even once the chunk could
load. A failed load is now fetched once more straight away.
@HMarzban
HMarzban merged commit 368f285 into main Nov 15, 2026
2 checks passed
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