Skip to content

refactor: update select to use overlay focus restoration logic #7601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/select/src/vaadin-select-base-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ export const SelectBaseMixin = (superClass) =>
this.removeAttribute('focus-ring');
}
} else if (wasOpened) {
this.focus();
if (this._openedWithFocusRing) {
this.setAttribute('focus-ring', '');
}
Expand Down
7 changes: 7 additions & 0 deletions packages/select/src/vaadin-select-overlay-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export const SelectOverlayMixin = (superClass) =>
return ['_updateOverlayWidth(opened, owner)'];
}

/** @protected */
ready() {
super.ready();

this.restoreFocusOnClose = true;
}

/** @protected */
_getMenuElement() {
return Array.from(this.children).find((el) => el.localName !== 'style');
Expand Down
19 changes: 17 additions & 2 deletions packages/select/test/keyboard.common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
import { aTimeout, fixtureSync, nextRender, nextUpdate, outsideClick } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';

Expand Down Expand Up @@ -67,7 +67,7 @@ describe('keyboard', () => {
});
});

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

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

it('should focus value button element on overlay closing with outside click', async () => {
await sendKeys({ press: 'Tab' });

await sendKeys({ press: 'Enter' });
await nextRender();

const focusedSpy = sinon.spy(valueButton, 'focus');

outsideClick();
await nextUpdate(select);
await aTimeout(0);

expect(focusedSpy.calledOnce).to.be.true;
});

it('should restore focus-ring attribute on overlay closing', async () => {
await sendKeys({ press: 'Tab' });

Expand Down
Loading