Skip to content

Commit

Permalink
feat(menu): additional keydown events and tests
Browse files Browse the repository at this point in the history
refactor(menu): removed focus action and passed setReturnFocus to menu/items

test(menu): fixed failing keydown tests

test(menu): close the menu with Escape, and return focus to another element

refactor(menu): removed `setReturnFocus`
  • Loading branch information
roomman committed Nov 22, 2021
1 parent dfee0f4 commit d4944e5
Show file tree
Hide file tree
Showing 6 changed files with 1,468 additions and 225 deletions.
1 change: 1 addition & 0 deletions addon/components/menu.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
goToPreviousItem=this.goToPreviousItem
goToItem=this.goToItem
search=(perform this.searchTask)
searchTaskIsRunning=this.searchTask.isRunning
)
)
}}
1 change: 1 addition & 0 deletions addon/components/menu/button.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{! template-lint-disable no-down-event-binding }}
<button
data-test-headlessui-menu-button
type='button'
aria-haspopup={{true}}
aria-controls={{if @isOpen @itemsGuid}}
Expand Down
2 changes: 1 addition & 1 deletion addon/components/menu/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class Button extends Component {
});
}
break;
case 'ArrowUp':
case Keys.ArrowUp:
event.preventDefault();
event.stopPropagation();
this.args.openMenu();
Expand Down
26 changes: 23 additions & 3 deletions addon/components/menu/items.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';

import { Keys } from 'ember-headlessui/utils/keyboard';

export default class Items extends Component {
get menuItemsElementSelector() {
return `#${this.args.itemsGuid}`;
Expand All @@ -10,20 +12,38 @@ export default class Items extends Component {
onKeydown(event) {
switch (event.key) {
// Ref: https://www.w3.org/TR/wai-aria-practices-1.2/#keyboard-interaction-12
case 'Enter':
// `Escape` key is handled by focus-trap
case Keys.Space:
if (this.args.searchTaskIsRunning) {
event.preventDefault();
event.stopPropagation();
return this.args.search(event.key);
}
// eslint-disable-next-line no-fallthrough
case Keys.Enter:
if (this.args.activeItem) {
this.args.activeItem.element.click();
}
this.args.closeMenu();
break;
case 'ArrowDown':
case Keys.ArrowDown:
event.preventDefault();
event.stopPropagation();
return this.args.goToNextItem();
case 'ArrowUp':
case Keys.ArrowUp:
event.preventDefault();
event.stopPropagation();
return this.args.goToPreviousItem();
case Keys.Home:
case Keys.PageUp:
event.preventDefault();
event.stopPropagation();
return this.args.goToFirstItem();
case Keys.End:
case Keys.PageDown:
event.preventDefault();
event.stopPropagation();
return this.args.goToLastItem();
default:
if (event.key.length === 1) {
return this.args.search(event.key);
Expand Down
Loading

0 comments on commit d4944e5

Please sign in to comment.