Skip to content

fix(button-stateful): stop mouseleave from stealing keyboard focus - #3224

Merged
interactivellama merged 2 commits into
salesforce:masterfrom
zigzagdev:fix/button-stateful-mouseleave-blur
Oct 26, 2026
Merged

interactivellama merged 2 commits into
salesforce:masterfrom
zigzagdev:fix/button-stateful-mouseleave-blur

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #

Additional description

Motivation: ButtonStateful's handleBlur is wired to both onBlur and onMouseLeave, and unconditionally calls e.currentTarget.blur(). That means simply moving the mouse away from a ButtonStateful that currently has keyboard focus forces it to lose focus. This is a real accessibility problem for keyboard users who also use a mouse or trackpad.

Root cause: This is a long-standing bug, not something the recent React 19 / TypeScript rewrite introduced. The original class component (components/button-stateful/index.jsx, pre #3195) had the identical handleBlur shared between onBlur and onMouseLeave with the same unconditional e.currentTarget.blur(). The rewrite additionally wrapped the onBlur forwarding in if (onBlur && 'relatedTarget' in e), but that guard doesn't actually change behavior, since relatedTarget exists on both FocusEvent and MouseEvent and is effectively always true.

What I did: Removed the forced e.currentTarget.blur() call. handleBlur now only forwards the onBlur callback for both real blur and mouseleave, matching the original's intent of treating "mouse left" as a soft blur signal for consumers, without touching actual DOM focus.


CONTRIBUTOR checklist (do not remove)

Please complete for every pull request.

Green gate — all of these must pass locally (CI runs the same):

  • npm run lint passes with 0 errors (npm run lint:fix to auto-fix). Warnings are the tracked gradual-migration backlog; do not add new errors.
  • npm run typecheck is clean (tsc --noEmit).
  • npm test -- --run passes — this runs both Vitest projects (jsdom unit + real-browser browser) and the story HTML snapshots. If you changed rendered markup on purpose, update snapshots with npm test -- --run -u and review the diff.
  • npm run build succeeds (type-check + Vite library build + .d.ts + entry shims).

Behavior & docs:

  • First-time contributors: sign the CLA (a bot will comment with instructions).
  • Added/updated Vitest + React Testing Library tests for the change (template: components/button/__tests__). New props and bug fixes should get a regression test. Tests that genuinely need a real browser (focus traversal, layout/measurement, canvas) go in a *.browser.test.jsx file (the browser project); everything else is jsdom.
  • Reviewed the component's Storybook stories: npm run storybook → http://localhost:6007/. Confirm the story actually renders and behaves (deprecated Storyshots/Karma are gone; there is no separate browser-tests server).
  • Story/example code uses the component's real prop API (e.g. render-props like onRenderContent / onRenderVisualPicker, icon props, events), not assumptions — a common source of "component looks broken" reports has been stories authored against the wrong API. Cross-check against components/<name>/__examples__.
  • Storybook Accessibility (a11y) panel shows no new violations for the affected stories.
  • If you added or changed a type, remember runtime PropTypes were removed in the TS migration — types are the contract. Ship accurate .d.ts (they're generated by the build); JS consumers rely on them via editor type-acquisition.

REVIEWER checklist (do not remove)

  • CI is green (GitHub Actions CI workflow: lint, typecheck, test, build). Chromatic visual review, if configured, has been checked.
  • Change has adequate test coverage; bug fixes include a regression test.
  • Reviewed the affected Storybook stories and their a11y panels.
  • Rendered markup still conforms to SLDS — check the story HTML snapshot diff (components/__tests__/__snapshots__/story-snapshots.test.jsx.snap).
  • Public API / package surface: if exports changed, the deep-import contract still holds (@salesforce/design-system-react/components/<name> and nested paths resolve for ESM, CJS, and types). Removing/renaming a public component or export is a breaking change → feat!:/BREAKING CHANGE: and a major version bump.

@interactivellama

Copy link
Copy Markdown
Contributor

Thanks for tracking this down, and the root-cause write-up is helpful. Correct me if I'm wrong, but one concern before we merge: SLDS shows slds-text-selected-focus ("Unfollow") on .slds-is-selected:focus, so the blur() on mouseleave is what clears that state after a mouse click. Removing it entirely would leave "Unfollow" showing after the pointer leaves, right?

Could you keep the blur when the focus isn't :focus-visible (i.e. wasn't from the keyboard), and add a *.browser.test.jsx test covering both cases? Please also drop the unrelated Prettier reformatting (or move it to a separate style: commit) so the fix is easy to review. Thanks!

@zigzagdev
zigzagdev force-pushed the fix/button-stateful-mouseleave-blur branch 2 times, most recently from c20ec2f to 1c1feb9 Compare October 24, 2026 08:40
handleBlur is wired to both onBlur and onMouseLeave and unconditionally
called e.currentTarget.blur(). That blur is intentional for mouse users:
SLDS shows `slds-text-selected-focus` ("Unfollow") on
`.slds-is-selected:focus`, so without it the label stays after a click
once the pointer leaves. But it also ran when the button had keyboard
focus, so moving the mouse away from a tabbed-to button dropped focus.

Only blur when the button is not `:focus-visible`, so mouse-initiated
focus is still cleared and keyboard focus is kept.

The `'relatedTarget' in e` guard before onBlur was a no-op (MouseEvent
has relatedTarget too), so it is simplified to `if (onBlur)` with the
same behavior.
…ocus

Adds browser-mode tests, since jsdom has neither `:focus-visible` nor the
SLDS stylesheet. A mouse click followed by mouseleave blurs the button
and hides "Unfollow"; keyboard focus survives mouseleave.
@zigzagdev
zigzagdev force-pushed the fix/button-stateful-mouseleave-blur branch from 1c1feb9 to 955e9ce Compare October 24, 2026 08:41
@zigzagdev

zigzagdev commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@interactivellama

I fixed it.

And blur is kept now and only skipped when the button has keyboard focus.

@interactivellama
interactivellama merged commit b73e536 into salesforce:master Oct 26, 2026
7 checks passed
@zigzagdev
zigzagdev deleted the fix/button-stateful-mouseleave-blur branch October 27, 2026 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants