Space on a `type="submit"` or `type="reset"` Button calls onPress and onClick twice
Provide a general summary of the issue here
Pressing Space on a RAC Button with type="submit" or type="reset" calls onPress and onClick twice. Enter calls them once.
Expected Behavior?
onPress and onClick run once per key press.
Current Behavior
type=submit key=Space: ["onPress","onClick","onPress","onClick","form-submit"]
type=submit key=Enter: ["form-submit","onPress","onClick"]
type=reset key=Space: ["onPress","onClick","onPress","onClick","form-reset"]
type=reset key=Enter: ["form-reset","onPress","onClick"]The form submits (or resets) once; only the handlers run twice.
Possible Solution
In react-aria/dist/private/interactions/usePress.mjs (3.52.1):
shouldPreventDefaultUpreturns false for submit and reset buttons (704-708), so the Space keyup is not prevented and the browser fires its activation click (on purpose, #1593).onKeyUpfiresonPressthroughtriggerPressEndEventandonClickthroughtriggerSyntheticClick(309-311), then clearsstate.isPressed(322).- The browser's activation click then reaches
pressProps.onClickwith!state.isPressedand a virtual click (284), which runs a second press.
Enter is unaffected because its activation click fires on keydown, while state.isPressed is still true. Related: #7891 (the onClick alias).
Context
Found while building a design system on React Aria Components. Observed in headless Chromium (Playwright 1.63, real key and mouse events) with react-aria-components 1.21.1 / react-aria 3.52.1 / react-stately 3.50.0 and no other code.
️ Steps to Reproduce
import { Button } from "react-aria-components";
export function Repro() {
return ["submit", "reset"].map((type) => (
<form
key={type}
onSubmit={(e) => { e.preventDefault(); console.log("form-submit"); }}
onReset={() => console.log("form-reset")}
>
<Button type={type} onPress={() => console.log("onPress")} onClick={() => console.log("onClick")}>
{type}
</Button>
</form>
));
}Focus a button, press Space. Repeat with Enter.
Version
react-aria-components 1.21.1, react-aria 3.52.1, react-stately 3.50.0, React 19.3.0
What browsers are you seeing the problem on?
Chrome
What operating system are you using?
macOS
Your Company/Team
No response
Tracking Issue
No response
Source: adobe/react-spectrum