Skip to content

Commit 45db560

Browse files
authored
refactor: update select to use overlay focus restoration logic (#7601) (#7604)
1 parent 124e60d commit 45db560

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

packages/select/src/vaadin-lit-select-overlay.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class SelectOverlay extends PositionMixin(OverlayMixin(ThemableMixin(DirMixin(Po
5757
`;
5858
}
5959

60+
/** @protected */
61+
ready() {
62+
super.ready();
63+
64+
this.restoreFocusOnClose = true;
65+
}
66+
6067
/** @protected */
6168
updated(props) {
6269
super.updated(props);

packages/select/src/vaadin-select-base-mixin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,6 @@ export const SelectBaseMixin = (superClass) =>
367367
this.removeAttribute('focus-ring');
368368
}
369369
} else if (wasOpened) {
370-
this.focus();
371370
if (this._openedWithFocusRing) {
372371
this.setAttribute('focus-ring', '');
373372
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export class SelectOverlay extends PositionMixin(OverlayMixin(DirMixin(ThemableM
5959
ready() {
6060
super.ready();
6161

62+
this.restoreFocusOnClose = true;
63+
6264
// When setting `opened` as an attribute, the overlay is already teleported to body
6365
// by the time when `ready()` callback of the `vaadin-select` is executed by Polymer,
6466
// so querySelector() would return null. So we use this workaround to set properties.

packages/select/test/keyboard.common.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextRender, nextUpdate, outsideClick } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55

@@ -67,7 +67,7 @@ describe('keyboard', () => {
6767
});
6868
});
6969

70-
it('should focus value button element on overlay closing', async () => {
70+
it('should focus value button element on overlay closing with Esc', async () => {
7171
await sendKeys({ press: 'Tab' });
7272

7373
await sendKeys({ press: 'Enter' });
@@ -81,6 +81,21 @@ describe('keyboard', () => {
8181
expect(focusedSpy.calledOnce).to.be.true;
8282
});
8383

84+
it('should focus value button element on overlay closing with outside click', async () => {
85+
await sendKeys({ press: 'Tab' });
86+
87+
await sendKeys({ press: 'Enter' });
88+
await nextRender();
89+
90+
const focusedSpy = sinon.spy(valueButton, 'focus');
91+
92+
outsideClick();
93+
await nextUpdate(select);
94+
await aTimeout(0);
95+
96+
expect(focusedSpy.calledOnce).to.be.true;
97+
});
98+
8499
it('should restore focus-ring attribute on overlay closing', async () => {
85100
await sendKeys({ press: 'Tab' });
86101

0 commit comments

Comments
 (0)