Skip to content

Commit

Permalink
Close all menus on esc
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki committed Jan 28, 2019
1 parent 97b7a8b commit e5de5d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/vaadin-contextmenu-items-mixin.html
Expand Up @@ -208,13 +208,19 @@
menu.dispatchEvent(new CustomEvent('item-selected', {detail: e.detail}));
});

subMenu.addEventListener('close-all-menus', () => {
menu.dispatchEvent(new CustomEvent('close-all-menus'));
});
menu.addEventListener('close-all-menus', menu.close);
menu.addEventListener('item-selected', menu.close);
menu.$.overlay.$.backdrop.addEventListener('click', () => menu.close());

menu.$.overlay.addEventListener('keydown', e => {
if (e.keyCode === 37 || e.keyCode === 27) {
if (e.keyCode === 37) {
menu.close();
menu.listenOn.focus();
} else if (e.keyCode === 27) {
menu.dispatchEvent(new CustomEvent('close-all-menus'));
}
});

Expand Down
6 changes: 2 additions & 4 deletions test/items.html
Expand Up @@ -181,11 +181,9 @@
expect(focusSpy).to.have.been.called;
});

it('should close the submenu on esc', () => {
const focusSpy = sinon.spy(menuComponents()[0], 'focus');
it('should close all menus on esc', () => {
fire(menuComponents(subMenu)[0], 'keydown', {}, {keyCode: 27, key: 'Esc'});
expect(subMenu.opened).to.be.false;
expect(focusSpy).to.have.been.called;
expect(rootMenu.opened).to.be.false;
});

it('should not close on disabled item click', () => {
Expand Down

0 comments on commit e5de5d8

Please sign in to comment.