Skip to content
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
9 changes: 6 additions & 3 deletions packages/a11y-base/src/focus-restoration-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ export class FocusRestorationController {
return;
}

const preventScroll = options ? options.preventScroll : false;
const focusOptions = {
preventScroll: options ? options.preventScroll : false,
focusVisible: options ? options.focusVisible : false,
};

if (getDeepActiveElement() === document.body) {
// In Firefox and Safari, focusing the node synchronously
// doesn't work as expected when the overlay is closing on outside click.
// These browsers force focus to move to the body element and retain it
// there until the next event loop iteration.
setTimeout(() => focusNode.focus({ preventScroll }));
setTimeout(() => focusNode.focus(focusOptions));
} else {
focusNode.focus({ preventScroll });
focusNode.focus(focusOptions);
}

this.focusNode = null;
Expand Down
112 changes: 78 additions & 34 deletions packages/a11y-base/test/focus-restoration-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,87 @@ describe('focus-restoration-controller', () => {
expect(getDeepActiveElement()).to.equal(button2);
});

it('should not prevent scroll when restoring focus synchronously by default', () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
controller.restoreFocus();
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.eql({ preventScroll: false });
});
describe('preventScroll', () => {
it('should not prevent scroll when restoring focus synchronously by default', () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
controller.restoreFocus();
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ preventScroll: false });
});

it('should prevent scroll when restoring focus synchronously with preventScroll', () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
controller.restoreFocus({ preventScroll: true });
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.eql({ preventScroll: true });
});
it('should prevent scroll when restoring focus synchronously with preventScroll', () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
controller.restoreFocus({ preventScroll: true });
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ preventScroll: true });
});

it('should not prevent scroll when restoring focus asynchronously by default', async () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
outsideClick();
controller.restoreFocus();
await aTimeout(0);
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.eql({ preventScroll: false });
it('should not prevent scroll when restoring focus asynchronously by default', async () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
outsideClick();
controller.restoreFocus();
await aTimeout(0);
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ preventScroll: false });
});

it('should prevent scroll when restoring focus asynchronously with preventScroll', async () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
outsideClick();
controller.restoreFocus({ preventScroll: true });
await aTimeout(0);
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ preventScroll: true });
});
});

it('should prevent scroll when restoring focus asynchronously with preventScroll', async () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
outsideClick();
controller.restoreFocus({ preventScroll: true });
await aTimeout(0);
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.eql({ preventScroll: true });
describe('focusVisible', () => {
it('should not set focusVisible when restoring focus synchronously by default', () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
controller.restoreFocus();
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ focusVisible: false });
});

it('should set focusVisible when restoring focus synchronously with focusVisible', () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
controller.restoreFocus({ focusVisible: true });
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ focusVisible: true });
});

it('should not set focusVisible when restoring focus asynchronously by default', async () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
outsideClick();
controller.restoreFocus();
await aTimeout(0);
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ focusVisible: false });
});

it('should set focusVisible when restoring focus asynchronously with focusVisible', async () => {
button1.focus();
const spy = sinon.spy(button2, 'focus');
controller.saveFocus(button2);
outsideClick();
controller.restoreFocus({ focusVisible: true });
await aTimeout(0);
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ focusVisible: true });
});
});
});
5 changes: 3 additions & 2 deletions packages/overlay/src/vaadin-overlay-focus-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export const OverlayFocusMixin = (superClass) =>
}

if (this.restoreFocusOnClose && this._shouldRestoreFocus()) {
const preventScroll = !isKeyboardActive();
this.__focusRestorationController.restoreFocus({ preventScroll });
const focusVisible = isKeyboardActive();
const preventScroll = !focusVisible;
this.__focusRestorationController.restoreFocus({ preventScroll, focusVisible });
}
}

Expand Down
28 changes: 26 additions & 2 deletions packages/overlay/test/restore-focus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ describe('restore focus', () => {
mousedown(document.body);
overlay.opened = false;
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.eql({ preventScroll: true });
expect(spy.firstCall.args[0]).to.deep.include({ preventScroll: true });
});

it('should not prevent scroll when restoring focus on close after keydown', async () => {
Expand All @@ -154,7 +154,31 @@ describe('restore focus', () => {
escKeyDown(document.body);
overlay.opened = false;
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.eql({ preventScroll: false });
expect(spy.firstCall.args[0]).to.deep.include({ preventScroll: false });
});
});

describe('focusVisible', () => {
it('should set focusVisible: false when restoring focus on close after mousedown', async () => {
focusable.focus();
overlay.opened = true;
await oneEvent(overlay, 'vaadin-overlay-open');
const spy = sinon.spy(focusable, 'focus');
mousedown(document.body);
overlay.opened = false;
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ focusVisible: false });
});

it('should set focusVisible: true when restoring focus on close after keydown', async () => {
focusable.focus();
overlay.opened = true;
await oneEvent(overlay, 'vaadin-overlay-open');
const spy = sinon.spy(focusable, 'focus');
escKeyDown(document.body);
overlay.opened = false;
expect(spy).to.be.calledOnce;
expect(spy.firstCall.args[0]).to.deep.include({ focusVisible: true });
});
});
});
Expand Down