Skip to content

Commit 7090226

Browse files
authored
fix: reset tabindex to -1 when initializing menu item (#339)
1 parent 91b28a6 commit 7090226

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/vaadin-contextmenu-items-mixin.html

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225

226226
if (component instanceof Vaadin.ItemElement) {
227227
component.setAttribute('role', 'menuitem');
228+
component.tabIndex = -1;
228229
component.classList.add('vaadin-menu-item');
229230
} else if (component.localName === 'hr') {
230231
component.setAttribute('role', 'separator');

test/items.html

+43-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
describe('items', () => {
3333
let rootMenu, subMenu, target;
3434

35+
const createComponent = (text) => {
36+
const item = document.createElement('vaadin-context-menu-item');
37+
item.textContent = text;
38+
return item;
39+
};
40+
3541
const menuComponents = (menu = rootMenu) => {
3642
return Array.from(menu.$.overlay.content.firstElementChild.children);
3743
};
@@ -84,7 +90,15 @@
8490
]},
8591
]
8692
},
87-
{text: 'foo-1'}
93+
{text: 'foo-1'},
94+
{
95+
text: 'foo-2',
96+
children: [
97+
{component: createComponent('foo-2-0')},
98+
{component: createComponent('foo-2-1')},
99+
{component: createComponent('foo-2-2')}
100+
]
101+
}
88102
];
89103
open();
90104
flush(() => {
@@ -396,6 +410,34 @@
396410
expect(spy).to.be.calledOnce;
397411
});
398412

413+
it('should focus first item after re-opening when using components', async() => {
414+
subMenu.close();
415+
rootMenu.$.overlay.focus();
416+
417+
const rootItem = getMenuItems(rootMenu)[2];
418+
419+
// Open the sub-menu with item components
420+
open(rootItem);
421+
await nextRender();
422+
const subMenu2 = getSubMenu(rootMenu);
423+
const items = getMenuItems(subMenu2);
424+
425+
// Arrow Down to focus next item
426+
items[0].focus();
427+
fire(items[0], 'keydown', {}, {keyCode: 40, key: 'ArrowDown'});
428+
expect(items[1].hasAttribute('focus-ring')).to.be.true;
429+
430+
// Arrow Left to close the sub-menu
431+
fire(items[1], 'keydown', {}, {keyCode: 37, key: 'ArrowLeft'});
432+
await nextRender();
433+
expect(rootItem.hasAttribute('focus-ring')).to.be.true;
434+
435+
// Re-open sub-menu and check focus
436+
open(rootItem);
437+
await nextRender();
438+
expect(items[0].hasAttribute('focus-ring')).to.be.true;
439+
});
440+
399441
// elementFromPoint doesn't work properly with shadydom on ios 10.3
400442
if (!window.ShadyDOM) {
401443
it('should have modeless sub menus', () => {

0 commit comments

Comments
 (0)