Skip to content

Commit f293e81

Browse files
authored
fix: close modeless dialog on Esc when dialog itself has focus (#10037)
1 parent 0703bd1 commit f293e81

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

packages/dialog/test/dialog.test.js

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import {
3-
aTimeout,
4-
click,
5-
esc,
6-
fixtureSync,
7-
listenOnce,
8-
nextRender,
9-
nextUpdate,
10-
oneEvent,
11-
} from '@vaadin/testing-helpers';
2+
import { sendKeys } from '@vaadin/test-runner-commands';
3+
import { aTimeout, click, fixtureSync, listenOnce, nextRender, nextUpdate, oneEvent } from '@vaadin/testing-helpers';
124
import sinon from 'sinon';
135
import '../src/vaadin-dialog.js';
146
import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js';
@@ -61,14 +53,22 @@ describe('vaadin-dialog', () => {
6153
});
6254

6355
describe('opened', () => {
64-
let dialog, backdrop, overlay;
56+
let dialog, focusable, backdrop, overlay;
6557

6658
beforeEach(async () => {
67-
dialog = fixtureSync('<vaadin-dialog opened></vaadin-dialog>');
59+
[dialog, focusable] = fixtureSync(`
60+
<div>
61+
<vaadin-dialog opened></vaadin-dialog>
62+
<input />
63+
</div>
64+
`).children;
6865
await nextRender();
6966

7067
dialog.renderer = (root) => {
71-
root.innerHTML = '<div>Simple dialog</div>';
68+
root.innerHTML = `
69+
<div>Simple dialog</div>
70+
<input />
71+
`;
7272
};
7373
await nextUpdate(dialog);
7474

@@ -83,15 +83,35 @@ describe('vaadin-dialog', () => {
8383

8484
describe('no-close-on-esc', () => {
8585
it('should close itself on ESC press by default', async () => {
86-
esc(document.body);
86+
await sendKeys({ press: 'Escape' });
8787
await nextUpdate(dialog);
8888
expect(dialog.opened).to.be.false;
8989
});
9090

9191
it('should not close itself on ESC press when no-close-on-esc is true', async () => {
9292
dialog.noCloseOnEsc = true;
93-
await nextUpdate(dialog);
94-
esc(document.body);
93+
await sendKeys({ press: 'Escape' });
94+
expect(dialog.opened).to.be.true;
95+
});
96+
97+
it('should close on Escape press when modeless and dialog itself is focused', async () => {
98+
dialog.modeless = true;
99+
dialog.focus();
100+
await sendKeys({ press: 'Escape' });
101+
expect(dialog.opened).to.be.false;
102+
});
103+
104+
it('should close on Escape press when modeless and content element is focused', async () => {
105+
dialog.modeless = true;
106+
dialog.querySelector('input').focus();
107+
await sendKeys({ press: 'Escape' });
108+
expect(dialog.opened).to.be.false;
109+
});
110+
111+
it('should not close on Escape press when modeless and not focused', async () => {
112+
dialog.modeless = true;
113+
focusable.focus();
114+
await sendKeys({ press: 'Escape' });
95115
expect(dialog.opened).to.be.true;
96116
});
97117
});

packages/overlay/src/vaadin-overlay-mixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ export const OverlayMixin = (superClass) =>
557557
}
558558

559559
// Only close modeless overlay on Esc press when it contains focus
560-
if (!this._shouldAddGlobalListeners() && !event.composedPath().includes(this.$.overlay)) {
560+
if (!this._shouldAddGlobalListeners() && !event.composedPath().includes(this._focusTrapRoot)) {
561561
return;
562562
}
563563

0 commit comments

Comments
 (0)