Skip to content

Commit 3df6504

Browse files
authored
fix: restore focus on context menu overlay close (#5881)
1 parent 47e8f73 commit 3df6504

5 files changed

Lines changed: 165 additions & 5 deletions

File tree

‎packages/context-menu/src/vaadin-menu-overlay-mixin.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import type { Constructor } from '@open-wc/dedupe-mixin';
7+
import type { OverlayFocusMixinClass } from '@vaadin/overlay/src/vaadin-overlay-focus-mixin.js';
78
import type { PositionMixinClass } from '@vaadin/overlay/src/vaadin-overlay-position-mixin.js';
89

910
export declare function MenuOverlayMixin<T extends Constructor<HTMLElement>>(
1011
base: T,
11-
): Constructor<MenuOverlayMixinClass> & Constructor<PositionMixinClass> & T;
12+
): Constructor<MenuOverlayMixinClass> & Constructor<OverlayFocusMixinClass> & Constructor<PositionMixinClass> & T;
1213

1314
export declare class MenuOverlayMixinClass {
1415
protected readonly parentOverlay: HTMLElement | undefined;

‎packages/context-menu/src/vaadin-menu-overlay-mixin.js‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
* Copyright (c) 2016 - 2023 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { getClosestElement } from '@vaadin/component-base/src/dom-utils.js';
7+
import { OverlayFocusMixin } from '@vaadin/overlay/src/vaadin-overlay-focus-mixin.js';
68
import { PositionMixin } from '@vaadin/overlay/src/vaadin-overlay-position-mixin.js';
79

810
/**
911
* @polymerMixin
1012
*/
1113
export const MenuOverlayMixin = (superClass) =>
12-
class MenuOverlayMixin extends PositionMixin(superClass) {
14+
class MenuOverlayMixin extends OverlayFocusMixin(PositionMixin(superClass)) {
1315
static get properties() {
1416
return {
1517
/**
@@ -30,6 +32,8 @@ export const MenuOverlayMixin = (superClass) =>
3032
ready() {
3133
super.ready();
3234

35+
this.restoreFocusOnClose = true;
36+
3337
this.addEventListener('keydown', (e) => {
3438
if (!e.defaultPrevented && e.composedPath()[0] === this.$.overlay && [38, 40].indexOf(e.keyCode) > -1) {
3539
const child = this.getFirstChild();
@@ -116,4 +120,48 @@ export const MenuOverlayMixin = (superClass) =>
116120
}
117121
}
118122
}
123+
124+
/**
125+
* Override method inherited from `OverlayFocusMixin` to disable
126+
* focus restoration on sub-menu overlay close. Focus should
127+
* be only restored when the root menu closes.
128+
*
129+
* @protected
130+
* @override
131+
* @return {boolean}
132+
*/
133+
_shouldRestoreFocus() {
134+
if (this.parentOverlay) {
135+
// Do not restore focus on sub-menu close.
136+
return false;
137+
}
138+
139+
return super._shouldRestoreFocus();
140+
}
141+
142+
/**
143+
* Override method inherited from `OverlayFocusMixin` to return
144+
* true if the overlay contains the given node, including
145+
* those within descendant menu overlays.
146+
*
147+
* @protected
148+
* @override
149+
* @param {Node} node
150+
* @return {boolean}
151+
*/
152+
_deepContains(node) {
153+
// Find the closest menu overlay for the given node.
154+
let overlay = getClosestElement(this.localName, node);
155+
while (overlay) {
156+
if (overlay === this) {
157+
// The node is inside a descendant menu overlay.
158+
return true;
159+
}
160+
161+
// Traverse the overlay hierarchy to check parent overlays.
162+
overlay = overlay.parentOverlay;
163+
}
164+
165+
return false;
166+
}
119167
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
3+
import { sendKeys } from '@web/test-runner-commands';
4+
import './not-animated-styles.js';
5+
import '../vaadin-context-menu.js';
6+
import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js';
7+
import { getMenuItems, outsideClick } from './helpers.js';
8+
9+
describe('a11y', () => {
10+
describe('focus restoration', () => {
11+
let contextMenu, contextMenuButton, beforeButton, afterButton;
12+
13+
beforeEach(() => {
14+
const wrapper = fixtureSync(`
15+
<div>
16+
<button>Before</button>
17+
<vaadin-context-menu open-on="click">
18+
<button>Open context menu</button>
19+
</vaadin-context-menu>
20+
<button>After</button>
21+
`);
22+
[beforeButton, contextMenu, afterButton] = wrapper.children;
23+
contextMenu.items = [{ text: 'Item 0' }, { text: 'Item 1', children: [{ text: 'Item 1/0' }] }];
24+
contextMenuButton = contextMenu.querySelector('button');
25+
contextMenuButton.focus();
26+
});
27+
28+
it('should move focus to the menu on open', async () => {
29+
contextMenuButton.click();
30+
await nextRender();
31+
const menuItem = getMenuItems(contextMenu)[0];
32+
expect(getDeepActiveElement()).to.equal(menuItem);
33+
});
34+
35+
it('should restore focus on outside click', async () => {
36+
contextMenuButton.click();
37+
await nextRender();
38+
outsideClick();
39+
await nextRender();
40+
expect(getDeepActiveElement()).to.equal(contextMenuButton);
41+
});
42+
43+
it('should restore focus on outside click when a sub-menu is open', async () => {
44+
contextMenuButton.click();
45+
await nextRender();
46+
// Move focus to Item 1
47+
await sendKeys({ press: 'ArrowDown' });
48+
// Open Item 1
49+
await sendKeys({ press: 'ArrowRight' });
50+
await nextRender();
51+
outsideClick();
52+
await nextRender();
53+
expect(getDeepActiveElement()).to.equal(contextMenuButton);
54+
});
55+
56+
it('should restore focus on root menu item selection', async () => {
57+
contextMenuButton.click();
58+
await nextRender();
59+
// Select Item 0
60+
await sendKeys({ press: 'Enter' });
61+
await nextRender();
62+
expect(getDeepActiveElement()).to.equal(contextMenuButton);
63+
});
64+
65+
it('should restore focus on sub-menu item selection', async () => {
66+
contextMenuButton.click();
67+
await nextRender();
68+
// Move focus to Item 1
69+
await sendKeys({ press: 'ArrowDown' });
70+
// Open Item 1
71+
await sendKeys({ press: 'ArrowRight' });
72+
await nextRender();
73+
// Select Item 1/1
74+
await sendKeys({ press: 'Enter' });
75+
await nextRender();
76+
expect(getDeepActiveElement()).to.equal(contextMenuButton);
77+
});
78+
79+
it('should move focus to the prev element outside the menu on Shift+Tab pressed inside', async () => {
80+
contextMenuButton.click();
81+
await nextRender();
82+
await sendKeys({ down: 'Shift' });
83+
await sendKeys({ press: 'Tab' });
84+
await sendKeys({ up: 'Shift' });
85+
expect(getDeepActiveElement()).to.equal(beforeButton);
86+
});
87+
88+
it('should move focus to the next element outside the menu on Tab pressed inside', async () => {
89+
contextMenuButton.click();
90+
await nextRender();
91+
await sendKeys({ press: 'Tab' });
92+
expect(getDeepActiveElement()).to.equal(afterButton);
93+
});
94+
});
95+
});

‎packages/context-menu/test/helpers.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ export async function openSubMenus(menu) {
2828
await openSubMenus(subMenu);
2929
}
3030
}
31+
32+
export function outsideClick() {
33+
// Move focus to body
34+
document.body.tabIndex = 0;
35+
document.body.focus();
36+
document.body.tabIndex = -1;
37+
// Outside click
38+
document.body.click();
39+
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ export const OverlayFocusMixin = (superClass) =>
159159
// which was focused before opening the overlay.
160160
const restoreFocusNode = this.restoreFocusNode || this.__restoreFocusNode;
161161
if (restoreFocusNode) {
162-
// Focusing the restoreFocusNode doesn't always work synchronously on Firefox and Safari
163-
// (e.g. combo-box overlay close on outside click).
164-
setTimeout(() => restoreFocusNode.focus());
162+
if (getDeepActiveElement() === document.body) {
163+
// In Firefox and Safari, focusing the restoreFocusNode synchronously
164+
// doesn't work as expected when the overlay is closing on outside click.
165+
// These browsers force focus to move to the body element and retain it
166+
// there until the next event loop iteration.
167+
setTimeout(() => restoreFocusNode.focus());
168+
} else {
169+
restoreFocusNode.focus();
170+
}
171+
165172
this.__restoreFocusNode = null;
166173
}
167174

0 commit comments

Comments
 (0)