Skip to content

[MenuItem][MacOS] Fix "onClick" being called twice when invoking menu item via ENTER / SPACE - #3933

Merged
Lawrence Win (lawrencewin) merged 3 commits into
microsoft:mainfrom
lawrencewin:menuitem-duplicate-keydown-fix
Jul 17, 2025
Merged

[MenuItem][MacOS] Fix "onClick" being called twice when invoking menu item via ENTER / SPACE#3933
Lawrence Win (lawrencewin) merged 3 commits into
microsoft:mainfrom
lawrencewin:menuitem-duplicate-keydown-fix

Conversation

@lawrencewin

@lawrencewin Lawrence Win (lawrencewin) commented Jul 11, 2025

Copy link
Copy Markdown
Contributor

Platforms Impacted

  • iOS
  • macOS
  • win32 (Office)
  • windows
  • android

Description of changes

React Native MacOS pressables handle a special case for ENTER / SPACE keys for the keyDown / keyDown event handlers: these keys should also be interpreted as presses, thus causing the event to be passed to the onPress handler. The code for this is found here: https://github.com/microsoft/react-native-macos/blob/48e607706bfcc25b4c28eba1aa352f409c02975e/packages/react-native/Libraries/Pressability/Pressability.js#L580-L617

For the MenuItem, we pass onInvoke, the handler for clicks and keypresses, into both press and keyDown / keyUp handlers. The code snippet above, then causes onClick to be called twice on the platform. We can fix this by simply marking the event as handled, so onClick is only called once.

Verification

Tested locally, video:

menu.item.working.mp4

Pull request checklist

This PR has considered (when applicable):

  • Automated Tests
  • Documentation and examples
  • Keyboard Accessibility
  • Voiceover
  • Internationalization and Right-to-left Layouts

Comment thread packages/components/Menu/src/MenuItem/useMenuItem.ts Outdated
@lawrencewin
Lawrence Win (lawrencewin) force-pushed the menuitem-duplicate-keydown-fix branch from d20108d to c658d7d Compare July 15, 2025 01:26
@lawrencewin
Lawrence Win (lawrencewin) merged commit 93700bb into microsoft:main Jul 17, 2025
11 checks passed
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.

3 participants