Skip to content

Commit

Permalink
Allow selecting menu items with space (#1429)
Browse files Browse the repository at this point in the history
* allow selecting menu items with space; #1423

* update PR
  • Loading branch information
claviska authored Jul 6, 2023
1 parent 8ffbd02 commit c9e644f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
## Next

- Added tests for `<sl-qr-code>` [#1416]
- Added support for pressing [[Space]] to select/toggle selected `<sl-menu-item>` elements [#1429]
- Fixed a bug in `<sl-qr-code>` where the `background` attribute was never passed to the QR code [#1416]
- Fixed a bug in `<sl-dropdown>` where aria attributes were incorrectly applied to the default `<slot>` causing Lighthouse errors [#1417]
- Fixed a bug in `<sl-carousel>` that caused navigation to work incorrectly in some case [#1420]
Expand Down
9 changes: 2 additions & 7 deletions src/components/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,15 @@ export default class SlMenu extends ShoelaceElement {
}

private handleKeyDown(event: KeyboardEvent) {
// Make a selection when pressing enter
if (event.key === 'Enter') {
// Make a selection when pressing enter or space
if (event.key === 'Enter' || event.key === ' ') {
const item = this.getCurrentItem();
event.preventDefault();

// Simulate a click to support @click handlers on menu items that also work with the keyboard
item?.click();
}

// Prevent scrolling when space is pressed
if (event.key === ' ') {
event.preventDefault();
}

// Move the selection when pressing down or up
if (['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
const items = this.getAllItems();
Expand Down

0 comments on commit c9e644f

Please sign in to comment.