Skip to content

Commit 8e45b82

Browse files
committed
fix: ensure overflow button always has correct tabindex
1 parent 3a05b79 commit 8e45b82

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

packages/menu-bar/src/vaadin-menu-bar-mixin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export const MenuBarMixin = (superClass) =>
268268
set _hasOverflow(hasOverflow) {
269269
if (this._overflow) {
270270
this._overflow.toggleAttribute('hidden', !hasOverflow);
271+
this._overflow.setAttribute('tabindex', hasOverflow ? '0' : '-1');
271272
}
272273
}
273274

@@ -761,8 +762,7 @@ export const MenuBarMixin = (superClass) =>
761762
*/
762763
_setFocused(focused) {
763764
if (focused) {
764-
const selector = this.tabNavigation ? '[focused]' : '[tabindex="0"]';
765-
const target = this.querySelector(`vaadin-menu-bar-button${selector}`);
765+
const target = this._buttons.find((btn) => isElementFocused(btn));
766766
if (target) {
767767
this._buttons.forEach((btn) => {
768768
this._setTabindex(btn, btn === target);

packages/menu-bar/test/dom/__snapshots__/menu-bar.test.snap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ snapshots["menu-bar basic"] =
5656
hidden=""
5757
role="menuitem"
5858
slot="overflow"
59-
tabindex="0"
59+
tabindex="-1"
6060
>
6161
<div aria-hidden="true">
6262
···
@@ -156,7 +156,7 @@ snapshots["menu-bar opened"] =
156156
hidden=""
157157
role="menuitem"
158158
slot="overflow"
159-
tabindex="0"
159+
tabindex="-1"
160160
>
161161
<div aria-hidden="true">
162162
···

packages/menu-bar/test/keyboard-navigation.test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { sendKeys } from '@vaadin/test-runner-commands';
3-
import { fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
3+
import { fixtureSync, nextRender, nextResize, nextUpdate } from '@vaadin/testing-helpers';
44
import sinon from 'sinon';
55
import '../src/vaadin-menu-bar.js';
66

@@ -138,6 +138,62 @@ describe('keyboard navigation', () => {
138138
expect(buttons[3].hasAttribute('focused')).to.be.true;
139139
});
140140
});
141+
142+
describe('overflow', () => {
143+
beforeEach(async () => {
144+
menu.items = [{ text: 'Item Foo' }, { text: 'Item Bar' }];
145+
await nextRender();
146+
buttons = menu._buttons;
147+
});
148+
149+
it('should move focus back to the overflow button on Shift + Tab after Tab', async () => {
150+
// Hide all buttons except overflow
151+
menu.style.maxWidth = '100px';
152+
await nextResize(menu);
153+
154+
firstGlobalFocusable.focus();
155+
await sendKeys({ press: 'Tab' });
156+
expect(document.activeElement).to.equal(menu._overflow);
157+
158+
await sendKeys({ press: 'Tab' });
159+
expect(document.activeElement).to.equal(lastGlobalFocusable);
160+
161+
await sendKeys({ press: 'Shift+Tab' });
162+
expect(document.activeElement).to.equal(menu._overflow);
163+
});
164+
165+
it('should move focus back to the overflow button on Tab after Shift + Tab', async () => {
166+
// Show 1 button + overflow
167+
menu.style.maxWidth = '120px';
168+
await nextResize(menu);
169+
170+
lastGlobalFocusable.focus();
171+
await sendKeys({ press: 'Shift+Tab' });
172+
expect(document.activeElement).to.equal(menu._overflow);
173+
174+
await sendKeys({ press: 'Shift+Tab' });
175+
expect(document.activeElement).to.equal(firstGlobalFocusable);
176+
177+
await sendKeys({ press: 'Tab' });
178+
expect(document.activeElement).to.equal(menu._overflow);
179+
});
180+
181+
it('should move focus back to the overflow button on Tab after button is hidden', async () => {
182+
lastGlobalFocusable.focus();
183+
await sendKeys({ press: 'Shift+Tab' });
184+
expect(document.activeElement).to.equal(buttons[1]);
185+
186+
await sendKeys({ press: 'Shift+Tab' });
187+
expect(document.activeElement).to.equal(firstGlobalFocusable);
188+
189+
// Hide all buttons except overflow
190+
menu.style.maxWidth = '100px';
191+
await nextResize(menu);
192+
193+
await sendKeys({ press: 'Tab' });
194+
expect(document.activeElement).to.equal(menu._overflow);
195+
});
196+
});
141197
});
142198

143199
describe('tab navigation mode', () => {

0 commit comments

Comments
 (0)