Skip to content

fix(app): show focus rings only during keyboard navigation - #65650

Closed
Hona wants to merge 1 commit into
devfrom
focus-ring-visibility
Closed

Hona wants to merge 1 commit into
devfrom
focus-ring-visibility

Conversation

@Hona

@Hona Hona commented Nov 23, 2026

Copy link
Copy Markdown
Member

Focus rings showed at random: around the whole timeline, around the composer after the app moved focus there, and still there after returning to the window. Rings now show only while the keyboard is moving through the app.

Before and after

Before After
Type, then the app moves focus to the composer Ring around the composer No ring
Tab into the composer Ring Ring around the composer

Why the rings were random

  • Chromium matches :focus-visible after any key without Ctrl/Alt/Meta, typing included, until a mouse click moves focus to another element. Click in the timeline, press ↓ or PgDn, and the whole timeline rings.
  • Focus a script moves inherits that state, so after typing a prompt anything the app focused got a ring.
  • The composer ringed on every focus that was not a direct click, and focus rescue forced a ring when the last input was any key, Ctrl and Alt included.

The rule

stateDiagram-v2
  [*] --> pointer
  pointer --> moving: Tab, arrow, Home, End
  moving --> keyboard: focus moved to another element
  moving --> pointer: key press ended, focus stayed
  pointer --> keyboard: Enter, Space or arrow on a focused control
  keyboard --> pointer: pointer press
Loading
  • Typing, shortcuts, lone modifiers, Escape, keys in a text field or on a scroll region, leaving and returning to the window, and focus the app moves itself never change the mode.
  • Focus the app moves shows a ring only if the mode is already on.
  • Enter or Space on a clicked control shows its ring, as Chromium and Fluent do.
  • Settings › Appearance › Always show focus rings rings every focus, a click included.

How

  • packages/ui/src/context/keyboard-mode-machine.ts is the pure transition. keyboard-mode.tsx feeds it key, pointer and focus events and marks the focused element, through shadow roots, with data-focus-visible.
  • Styles read the mark instead of the pseudo-class:
/* before */
[data-component="home-session-row"]:focus-visible { outline: 2px solid var(--v2-border-border-focus); }

/* after */
[data-component="home-session-row"][data-focus-visible] { outline: 2px solid var(--v2-border-border-focus); }
  • Tailwind's focus-visible:, group-focus-visible: and peer-focus-visible: keep working, redefined in one line:
@custom-variant focus-visible (&[data-focus-visible]);
  • base.css removes the browser's own ring, and bun run lint now fails on a raw :focus-visible (script/check-focus-rings.ts).
  • The composer and Home search draw their frame ring from the same mark, so they no longer decide for themselves.

Why a mark on the element, not a flag on the root

Cost of one mode switch (the first Tab, or the first click after Tab), with the app's stylesheets in the session story:

Elements on the page Flag on <html> Mark on the focused element
1,300 19 ms 0.1 ms
8,000 137 ms 0.1 ms
25,000 419 ms 0.1 ms

A root flag re-styles the whole page, because the base ring rule has no class or attribute to narrow it. The mark touches one element, and a mouse-only user never triggers a write.

Tests

  • focus-ring.spec.ts gains "shows rings for keyboard navigation and operation only", which fails on the old behaviour.
  • "leaves no ring on the review toggle after a pointer click" is removed: a lone Shift can no longer ring anything, which the "a lone Shift" case in keyboard-mode-machine.test.ts and the new story test prove.
  • Tests that used locator.focus() or a lone Shift to stand in for keyboard arrival press Tab first.

Notes

  • No flag: this fixes existing behaviour.
  • The ring bookkeeping in field-focus.tsx, overlay-focus.tsx, dialog.tsx and focus-rescue.tsx no longer affects what shows. Removing it is a follow-up.
  • Screen readers are not detected. "Always show focus rings" is the route for anyone who wants rings on every focus.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I recommend merging. I reproduced the stray ring on the base and it's gone with this change. The new "shows rings for keyboard navigation and operation only" test fails on the base (typing a key, then focus moved from code, draws a solid ring) and passes here. The screenshots show the same Button story after the same steps on the base and with this change. focus-ring.spec.ts and the focus/ring tests in composer.spec.ts pass (12), keyboard-mode-machine.test.ts passes (23), check-focus-rings.ts passes, and tsc is clean for ui and app. I also compiled the redefined Tailwind variant: focus-visible:, group-focus-visible:, peer-focus-visible: and group-has-[[data-focus-visible]] all produce the expected selectors.

Public API is unchanged. "Always show focus rings" is a client-side appearance setting stored by the app, not a config key.

No blocking concerns. One small gap for the follow-up: @pierre/trees keeps its own :focus-visible styles inside its shadow root, and the reset in base.css doesn't reach there. Those tree rows still follow Chromium's rule, as they did before this change.

Before

After

@Hona

Hona commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Superseded by #65690, which removes the same stray rings with a much smaller change: no ring on scroll areas or on the composer, and the browser's rule left in place for every other control.

@Hona Hona closed this Nov 23, 2026
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