Repository navigation
[button] Fix custom tag keyboard click handlers - #4838
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,285.32 ms +74.46 ms(+6.1%) | Renders: 78 (+0) | Paint: 2,005.49 ms +125.08 ms(+6.7%)
11 tests within noise — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
e37d146 to
8805a21
Compare
There was a problem hiding this comment.
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
useButtonkeyboard handlers to call.click()(and mark the Base UI handler as prevented) for Enter/Space activation on non-native buttons. - Add/expand tests for
ButtonandToolbar.Buttonto verify real click dispatch from keyboard activation and cover related disabled/focusable-disabled behaviors. - Simplify/replace the prior “duplicate click” test scenario for composed
useButtoninstances.
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.
flaviendelangle
left a comment
There was a problem hiding this comment.
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 newactivateElement, which fires exactly one click;preventBaseUIHandler()correctly suppresses the duplicate in nested/mergeduseButtoncases (verified against the rewrittenuseButton.test.tsxnested test). - The
event.defaultPreventedearly-return for Enter (:158) matches native semantics (Enter activates on keydown), andRadioRoot's new EnterpreventDefaultcorrectly leans on it. Space still activates on keyup, also matching native — the keydown-preventDefaultasymmetry 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')hasdetail===0,pointerType==='',isTrusted===false, soisVirtualClickstill returnstruevia its third branch —SelectItem's rewrittenonClick(withlastKeyRefremoved) 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
…custom-keyboard-click # Conflicts: # packages/react/src/select/item/SelectItem.tsx
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,
useButtoncalled the capturedonClickprop directly:That skipped other click handlers on the rendered element, including cases like:
Changes
useButtoninstances. With keyboard activation now using a real.click(), that contrived case follows normal click semantics instead of a keyboard-only dedupe path.