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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/core/src/common/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ function throttleImpl<T extends Function>(
}

export function clickElementOnKeyPress(keys: string[]) {
return (e: React.KeyboardEvent) =>
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)) {
// 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.

e.preventDefault();
}

e.target.dispatchEvent(new MouseEvent("click", { ...e, view: undefined }));
}
};
}
15 changes: 14 additions & 1 deletion packages/docs-app/src/examples/core-examples/menuExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

import * as React from "react";

import { Classes, H5, Icon, Menu, MenuDivider, MenuItem } from "@blueprintjs/core";
import { Classes, H5, Icon, InputGroup, Menu, MenuDivider, MenuItem } from "@blueprintjs/core";
import { Example, type ExampleProps } from "@blueprintjs/docs-theme";

import { getSizeProp, type Size, SizeSelect } from "./common/sizeSelect";

export function MenuExample(props: ExampleProps) {
const [size, setSize] = React.useState<Size>("regular");
const [count, setCount] = React.useState<number>(0);

const options = (
<>
Expand All @@ -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.

icon="calculator"
labelElement={count}
onClick={() => setCount(oldCount => oldCount + 1)}
text="Increment"
/>
<MenuItem icon="cog" labelElement={<Icon icon="share" />} text="Settings..." intent="primary" />
</Menu>
<Menu className={Classes.ELEVATION_1} {...getSizeProp(size)}>
Expand All @@ -61,6 +68,12 @@ export function MenuExample(props: ExampleProps) {
<MenuItem icon="asterisk" text="Miscellaneous">
<MenuItem icon="badge" text="Badge" />
<MenuItem icon="book" text="Long items will truncate when they reach max-width" />
<MenuItem
icon="edit"
text="Set name"
labelElement={<InputGroup small={true} placeholder="Item name..." />}
shouldDismissPopover={false}
/>
<MenuItem icon="more" text="Look in here for even more items">
<MenuItem icon="briefcase" text="Briefcase" />
<MenuItem icon="calculator" text="Calculator" />
Expand Down