Skip to content

[DropdownMenu] Prevent passing keydown event to the focused MenuItem from MenuTrigger #1647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2022

Conversation

paperstick
Copy link

@paperstick paperstick commented Sep 6, 2022

Description

This fix should prevent passing onKeyDown to the focused DropdownMenuItem from DropdownMenuTrigger which triggered onClick event on a or button elements.


Closes #1268
Closes #1397
Closes #1622
Closes #1655

Comment on lines 129 to 136
if (['Enter', ' '].includes(event.key)) {
context.onOpenToggle();
// prevent passing keydown event to the focused MenuItem element in Firefox
if (!context.open) event.preventDefault();
}
if (event.key === 'ArrowDown') context.onOpenChange(true);
// prevent keypresses from scrolling window
if ([' ', 'ArrowDown'].includes(event.key)) event.preventDefault();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be enough no?
I don't think we need the extra if (!context.open)

Suggested change
if (['Enter', ' '].includes(event.key)) {
context.onOpenToggle();
// prevent passing keydown event to the focused MenuItem element in Firefox
if (!context.open) event.preventDefault();
}
if (event.key === 'ArrowDown') context.onOpenChange(true);
// prevent keypresses from scrolling window
if ([' ', 'ArrowDown'].includes(event.key)) event.preventDefault();
if (['Enter', ' '].includes(event.key)) context.onOpenToggle();
if (event.key === 'ArrowDown') context.onOpenChange(true);
// prevent keydown from scrolling window / first focused item to execute
// that keydown (inadvertently closing the menu)
if (['Enter', ' ', 'ArrowDown'].includes(event.key)) event.preventDefault();

Copy link
Author

Choose a reason for hiding this comment

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

Yes, looks like you are right, we can get rid of unnecessary check for open menu

@benoitgrelard benoitgrelard merged commit 8e1d06a into radix-ui:main Sep 21, 2022
@paperstick paperstick deleted the 1622-anchor-link branch September 21, 2022 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants