Skip to content
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

Prevent scrolling when a MenuItem is selected with the spacebar #6697

Merged
merged 3 commits into from
Feb 7, 2024

Conversation

ericjeney
Copy link
Contributor

This fixes a small bug that caused the page to scroll when a user selected a <MenuItem> using the spacebar, which is visible on the Menu docs page.

@adidahiya
Copy link
Contributor

Prevent scroll in clickElementOnKeyPress

Build artifact links for this commit: documentation | landing | table | demo

This is an automated comment from the deploy-preview CircleCI job.

keys.some(key => e.key === key) && e.target.dispatchEvent(new MouseEvent("click", { ...e, view: undefined }));
return (e: React.KeyboardEvent) => {
if (keys.some(key => e.key === key)) {
e.preventDefault(); // Prevent spacebar from scrolling the page
Copy link
Contributor

Choose a reason for hiding this comment

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

this could possibly cause issues if a MenuItem has a text input inside it, I think. it's not really recommended, but I wouldn't be surprised if something like this exists in the wild:

https://codesandbox.io/p/devbox/distracted-cookies-sv5mlx

image

we should test / verify this behavior in the docs by adding an item which contains an <InputGroup> to the Menu docs example

one idea for preventing a regression here is to check if the keypress occurred in a text input, using another utility fn in this same module:

if (elementIsTextInput(e.target as HTMLElement)) {
  // don't prevent default
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't consider someone could be nesting a text element in a MenuItem. I can add a check and docs example for that.

Though if that were the case today, it would be issuing a click on the MenuItem any time they hit the spacebar. I assume we'd want to keep that behavior to avoid any breaks?

Copy link
Contributor

Choose a reason for hiding this comment

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

it would be issuing a click on the MenuItem any time they hit the spacebar

Yeah I suppose so, but they might be doing their own preventDefault to work around this. Agree that we should preserve the existing behavior as much as possible to avoid surprises

return (e: React.KeyboardEvent) => {
if (keys.some(key => e.key === key)) {
// Prevent spacebar from scrolling the page unless we're in a text field
if (!elementIsTextInput(e.target as HTMLElement)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will allow the space character to enter the TextInput, but will still click the element. That's a bit odd, but technically prevents a break here.

@@ -39,6 +40,12 @@ export function MenuExample(props: ExampleProps) {
<MenuItem icon="new-object" text="New object" />
<MenuItem icon="new-link" text="New link" />
<MenuDivider />
<MenuItem
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We didn't have any menu examples that performed an action on click previously.

@adidahiya
Copy link
Contributor

Add more Menu docs examples

Build artifact links for this commit: documentation | landing | table | demo

This is an automated comment from the deploy-preview CircleCI job.

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

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

nice, I like the improved docs example, it works nicely when I hit spacebar

image

@adidahiya adidahiya merged commit 8768a30 into develop Feb 7, 2024
12 checks passed
@adidahiya adidahiya deleted the ericj/fix-clickElementOnKeyPress branch February 7, 2024 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants