Skip to content

Commit

Permalink
fix(menu): close menu when escape is pressed on list root
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 542325162
  • Loading branch information
Elliott Marquez authored and copybara-github committed Jun 21, 2023
1 parent 30937ac commit d5035db
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion menu/lib/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {requestUpdateOnAriaChange} from '../../internal/aria/delegate.js';
import {createAnimationSignal, EASING} from '../../internal/motion/animation.js';
import {List} from '../../list/lib/list.js';

import {ActivateTypeaheadEvent, DeactivateTypeaheadEvent, isElementInSubtree, MenuItem} from './shared.js';
import {ActivateTypeaheadEvent, DeactivateTypeaheadEvent, isClosableKey, isElementInSubtree, MenuItem} from './shared.js';
import {Corner, SurfacePositionController, SurfacePositionTarget} from './surfacePositionController.js';
import {TypeaheadController} from './typeaheadController.js';

Expand Down Expand Up @@ -340,6 +340,12 @@ export abstract class Menu extends LitElement {
// and we don't want the menu item to close the menu.
@eventOptions({capture: true})
private handleListKeydown(e: KeyboardEvent) {
if (e.target === this.listElement && !e.defaultPrevented &&
isClosableKey(e.code)) {
e.preventDefault();
this.close();
}

this.typeaheadController.onKeydown(e);
}

Expand Down
48 changes: 48 additions & 0 deletions menu/menu_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

// import 'jasmine'; (google3-only)
import './menu.js';

import {html, render} from 'lit';

import {createTokenTests} from '../testing/tokens.js';

Expand All @@ -15,6 +18,51 @@ describe('<md-menu>', () => {
describe('.styles', () => {
createTokenTests(MdMenu.styles);
});

let root: HTMLDivElement;

beforeEach(() => {
root = document.createElement('div');
document.body.appendChild(root);
});

afterEach(() => {
root?.remove();
});

it('escape on list root closes menu', async () => {
render(
html`
<button>OpenMenu</button>
<md-menu quick></md-menu>
`,
root);

const button = root.querySelector('button')!;
const menu = root.querySelector('md-menu')!;
menu.anchor = button;
menu.show();
await menu.updateComplete;
const listEl = menu.renderRoot.querySelector('md-list')!;
await listEl.updateComplete;
const listRoot = listEl.renderRoot.querySelector('ul')!;

expect(menu.open).toBeTrue();

const escapeKeydownEvent = new KeyboardEvent('keydown', {
key: 'Escape',
code: 'Escape',
bubbles: true,
composed: true,
cancelable: true
});
listRoot.dispatchEvent(escapeKeydownEvent);

await menu.updateComplete;

expect(menu.open).toBeFalse();
expect(escapeKeydownEvent.defaultPrevented).toBeTrue();
});
});

describe('<md-menu-item>', () => {
Expand Down

0 comments on commit d5035db

Please sign in to comment.