Skip to content

[switch] Fix focus-visible keyboard activation#5129

Draft
lyzno1 wants to merge 2 commits into
mui:masterfrom
lyzno1:codex/switch-focus-visible-firefox
Draft

[switch] Fix focus-visible keyboard activation#5129
lyzno1 wants to merge 2 commits into
mui:masterfrom
lyzno1:codex/switch-focus-visible-firefox

Conversation

@lyzno1

@lyzno1 lyzno1 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Fixes the Firefox case where a Switch remains focused after pointer interaction followed by Space/Enter activation, but does not match :focus-visible.

Changes:

  • Restore visible focus for non-native Switch keyboard activation when the element is focused but not :focus-visible.
  • Suppress the internal blur/refocus from marking the field touched or committing blur validation.
  • Add browser-only regression coverage for Tab activation and pointer-then-keyboard activation with Enter and Space.

Validation:

  • pnpm exec prettier --check packages/react/src/switch/root/SwitchRoot.tsx packages/react/src/switch/root/SwitchRoot.test.tsx
  • pnpm eslint packages/react/src/switch/root/SwitchRoot.tsx packages/react/src/switch/root/SwitchRoot.test.tsx
  • pnpm test:jsdom SwitchRoot --no-watch
  • pnpm test:chromium SwitchRoot --no-watch
  • pnpm test:chromium SwitchRoot --no-watch -t "focus-visible"
  • pnpm test:firefox SwitchRoot --no-watch -t "focus-visible"
  • pnpm test:webkit SwitchRoot --no-watch -t "focus-visible"

Fixes #5130

@pkg-pr-new

pkg-pr-new Bot commented Jun 26, 2026

Copy link
Copy Markdown

commit: 717274e

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 26, 2026

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+394B(+0.08%) 🔺+104B(+0.07%)

Details of bundle changes

Performance

Total duration: 1,071.14 ms -4.45 ms(-0.4%) | Renders: 78 (+0)

Test Duration Renders
Checkbox mount (500 instances) 72.22 ms 🔺+13.51 ms(+23.0%) 1 (+0)

13 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jun 26, 2026

Copy link
Copy Markdown

Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 717274e
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a3e72ad15e2fb000883f3d2
😎 Deploy Preview https://deploy-preview-5129--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@lyzno1

lyzno1 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Browser: Firefox

CleanShot.2026-06-26.at.14.51.00.mp4

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3b896c2e1c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/react/src/switch/root/SwitchRoot.tsx
@lyzno1 lyzno1 force-pushed the codex/switch-focus-visible-firefox branch from d6e3651 to 717274e Compare June 26, 2026 12:38

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 717274ef19

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +173 to +176
element.blur();
element.focus({
preventScroll: true,
focusVisible: true,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Suppress restore events for render handlers

When consumers provide handlers on the rendered element, e.g. <Switch.Root render={<span onBlur={...} onFocus={...} />} />, this real blur/refocus still reaches them: useRenderElement merges render.props after the Base UI props (packages/react/src/internals/useRenderElement.tsx:146), and mergeProps runs those rightmost handlers first, so the new guard cannot prevent them. Fresh evidence beyond the existing direct-prop case is that this supported render-element path still leaks the artificial blur/focus after pointer focus + Space/Enter, which can mark fields touched or run validation even though focus stays on the switch.

Useful? React with 👍 / 👎.

@lyzno1

lyzno1 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

I realized I don’t have enough context to properly fix this issue at the moment, so I’ll convert this PR to a draft for now.

If maintainers think this isn’t the right direction, or if it’s not worth keeping open, please feel free to close the PR or the issue. No worries at all.

@lyzno1 lyzno1 marked this pull request as draft June 26, 2026 12:56
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.

[switch] Focus ring is missing in Firefox after pointer focus and keyboard activation

1 participant