Skip to content

[button] Fix custom tag keyboard click handlers - #4838

Merged
atomiks merged 18 commits into
mui:masterfrom
atomiks:codex/fix-button-custom-keyboard-click
Jul 10, 2026
Merged

atomiks merged 18 commits into
mui:masterfrom
atomiks:codex/fix-button-custom-keyboard-click

Conversation

@atomiks

@atomiks atomiks commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Custom-tag Buttons handled keyboard activation by directly calling the Button prop handler. This makes Enter and Space dispatch a real click instead, so render-element and delegated click handlers run through the normal event path.

Root cause

For non-native buttons, useButton called the captured onClick prop directly:

externalOnClick?.(event);

That skipped other click handlers on the rendered element, including cases like:

<Button nativeButton={false} render={<span onClick={handleClick} />} />

Changes

  • Dispatch a real click for Enter and Space activation on non-native, non-composite buttons.
  • Add missing public Button/Toolbar coverage around the affected keyboard paths and nearby documented behavior: disabled state callbacks and focusable disabled transitions.
  • Remove the old duplicate-click test for two merged useButton instances. With keyboard activation now using a real .click(), that contrived case follows normal click semantics instead of a keyboard-only dedupe path.
  • Keep the fix narrow and avoid broader render-prop disabled guard machinery for rare handler placement on disabled render elements.

@pkg-pr-new

pkg-pr-new Bot commented May 18, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4838
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4838
    

commit: fef3193

@atomiks atomiks added component: button Changes related to the button component. type: bug It doesn't behave as expected. labels May 18, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented May 18, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react ▼-208B(-0.04%) ▼-62B(-0.04%)

Details of bundle changes

Performance

Total duration: 1,285.32 ms +74.46 ms(+6.1%) | Renders: 78 (+0) | Paint: 2,005.49 ms +125.08 ms(+6.7%)

Test Duration Renders
Checkbox mount (500 instances) 72.69 ms 🔺+12.27 ms(+20.3%) 1 (+0)
Tooltip mount (300 contained roots) 53.76 ms 🔺+10.90 ms(+25.4%) 1 (+0)
Slider mount (300 instances) 179.89 ms +29.42 ms(+19.6%) 3 (+0)

11 tests within noise — details

Metric alarms

Test Metric Change
Tooltip mount (300 contained roots) bench:paint 🔺 +19.75 ms
Slider mount (300 instances) bench:paint 🔺 +52.55 ms

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

@netlify

netlify Bot commented May 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit fef3193
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a50c7b00b3f4d0008fdff7e
😎 Deploy Preview https://deploy-preview-4838--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.

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jun 4, 2026
@atomiks
atomiks force-pushed the codex/fix-button-custom-keyboard-click branch from e37d146 to 8805a21 Compare June 8, 2026 05:47
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jun 8, 2026
@atomiks
atomiks marked this pull request as ready for review June 8, 2026 06:13
@atomiks
atomiks requested a review from Copilot June 8, 2026 20:12

Copilot AI 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.

Pull request overview

This PR fixes keyboard activation for non-native (custom element) Buttons so Enter/Space dispatch a real DOM click instead of directly calling the onClick prop, ensuring capture/bubble/delegated click handlers run through the normal event path.

Changes:

  • Update useButton keyboard handlers to call .click() (and mark the Base UI handler as prevented) for Enter/Space activation on non-native buttons.
  • Add/expand tests for Button and Toolbar.Button to verify real click dispatch from keyboard activation and cover related disabled/focusable-disabled behaviors.
  • Simplify/replace the prior “duplicate click” test scenario for composed useButton instances.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/react/src/internals/use-button/useButton.ts Switch keyboard activation from calling externalOnClick to dispatching real clicks via .click().
packages/react/src/internals/use-button/useButton.test.tsx Update/add tests for nested/composed buttons and preventBaseUIHandler keyboard suppression.
packages/react/src/button/Button.test.tsx Add coverage for non-native keyboard click dispatch and disabled state callback behavior.
packages/react/src/toolbar/button/ToolbarButton.test.tsx Add coverage for real click dispatch from Enter/Space on non-native toolbar buttons.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@atomiks atomiks changed the title [button] Fix custom keyboard click dispatch [button] Fix custom tag keyboard click handlers Jun 9, 2026
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jun 11, 2026
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jun 12, 2026
@oliviertassinari
oliviertassinari requested a review from jjenzz as a code owner June 27, 2026 20:50

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR review

This PR makes non-native (nativeButton={false}) keyboard activation dispatch a real bubbling click event instead of calling the captured onClick prop directly, so render-element and ancestor click handlers run through the normal event path. The implementation is clean and well-tested, and all CI (jsdom, chromium, E2E, React 18, React Compiler) is green. Nothing is merge-blocking. The main thing worth a maintainer's awareness is the breadth of the behavioral change: it touches every component that can render as a non-native button, and keyboard activation now produces a DOM click that bubbles where it previously did not.

Bugs (0)

No findings.

I traced each activation path and could not construct a correctness failure:

  • Composite Space (useButton.ts:138-150) and non-composite Enter/Space (:152-166, :200) all route through the new activateElement, which fires exactly one click; preventBaseUIHandler() correctly suppresses the duplicate in nested/merged useButton cases (verified against the rewritten useButton.test.tsx nested test).
  • The event.defaultPrevented early-return for Enter (:158) matches native semantics (Enter activates on keydown), and RadioRoot's new Enter preventDefault correctly leans on it. Space still activates on keyup, also matching native — the keydown-preventDefault asymmetry I initially suspected is not a defect, since native Space activation is keyup-driven (consistent with the existing keyup comment in the file).
  • The dispatched PointerEvent('click') has detail===0, pointerType==='', isTrusted===false, so isVirtualClick still returns true via its third branch — SelectItem's rewritten onClick (with lastKeyRef removed) commits correctly on keyboard activation.
  • Untrusted dispatched clicks do run native activation behavior (form submission, link navigation), so replacing currentTarget.click() with the dispatch in the composite native/link branch is behavior-preserving while additionally carrying modifier state — exactly as the code comment claims.

Tests (0)

No findings.

Coverage is appropriately expanded: render-element + capture + ancestor click counts, modifier propagation, preventBaseUIHandler/preventDefault suppression, focusable-when-disabled transitions, and the RadioGroup Enter case. The nested-button test rename matches the new real-click semantics.

Simplifications (0)

No findings. The SelectItem lastKeyRef removal and the unified activateElement branch are genuine simplifications enabled by the new approach, and the mergeProps import was correctly dropped from the test.

Docs (0)

No findings. The new inline comments in RadioRoot.tsx, SelectItem.tsx, and activateElement accurately describe the new behavior and the useButton contract they depend on.

Verdict

Approve — no correctness or test gaps survived verification; the one residual is scope-of-impact, not a defect.

ℹ️ Heads-up (not blocking): this changes activation semantics for every component that can render non-native (Menu items, Combobox items, Tabs, Accordion/Collapsible triggers, Switch, Checkbox, Select items, dialog/popover triggers, etc. — ~30 components). For those, keyboard activation now bubbles a real DOM click to ancestors and reports isTrusted: false / detail: 0, where the old code called the handler directly without bubbling. CI (including E2E) passing covers the tested integrations, but it's worth a spot-check of any place a non-native Base UI button sits under an ancestor onClick (or relies on event.isTrusted) that should not react to keyboard activation.


🤖 Review generated with Claude Code

- Extract dispatchClickWithModifiers util; reuse in checkbox/radio/switch input dispatches
- Menu: classify keyboard activation by click detail instead of isTrusted, aligning with
  PopoverStore; drag-release item click now carries detail 1 and modifier state
- useButton: prevent page scroll for Space on non-native links and pin link activation
- Simplify composite Space activation condition; document the Space preventDefault limitation
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 10, 2026
atomiks added 2 commits July 10, 2026 19:24
…custom-keyboard-click

# Conflicts:
#	packages/react/src/select/item/SelectItem.tsx
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Jul 10, 2026
@atomiks
atomiks merged commit c9c90dc into mui:master Jul 10, 2026
7 checks passed
@atomiks
atomiks deleted the codex/fix-button-custom-keyboard-click branch August 11, 2026 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: button Changes related to the button component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants