Skip to content

Commit 6742c2d

Browse files
authored
refactor: move Tab navigation logic to KeyboardDirectionMixin (#8908)
1 parent 081f0ba commit 6742c2d

3 files changed

Lines changed: 109 additions & 41 deletions

File tree

‎packages/a11y-base/src/keyboard-direction-mixin.js‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export const KeyboardDirectionMixin = (superclass) =>
3030
return true;
3131
}
3232

33+
/**
34+
* @return {boolean}
35+
* @protected
36+
*/
37+
get _tabNavigation() {
38+
return false;
39+
}
40+
3341
/** @protected */
3442
focus() {
3543
const items = this._getItems();
@@ -67,7 +75,7 @@ export const KeyboardDirectionMixin = (superclass) =>
6775
return;
6876
}
6977

70-
const { key } = event;
78+
const { key, shiftKey } = event;
7179
const items = this._getItems() || [];
7280
const currentIdx = items.indexOf(this.focused);
7381

@@ -77,10 +85,10 @@ export const KeyboardDirectionMixin = (superclass) =>
7785
const isRTL = !this._vertical && this.getAttribute('dir') === 'rtl';
7886
const dirIncrement = isRTL ? -1 : 1;
7987

80-
if (this.__isPrevKey(key)) {
88+
if (this.__isPrevKeyPressed(key, shiftKey)) {
8189
increment = -dirIncrement;
8290
idx = currentIdx - dirIncrement;
83-
} else if (this.__isNextKey(key)) {
91+
} else if (this.__isNextKeyPressed(key, shiftKey)) {
8492
increment = dirIncrement;
8593
idx = currentIdx + dirIncrement;
8694
} else if (key === 'Home') {
@@ -93,6 +101,17 @@ export const KeyboardDirectionMixin = (superclass) =>
93101

94102
idx = this._getAvailableIndex(items, idx, increment, (item) => !isElementHidden(item));
95103

104+
if (
105+
this._tabNavigation &&
106+
key === 'Tab' &&
107+
((idx > currentIdx && event.shiftKey) || (idx < currentIdx && !event.shiftKey))
108+
) {
109+
// Prevent "roving tabindex" logic and let the normal Tab behavior if
110+
// - currently on the first focusable item and Shift + Tab is pressed,
111+
// - currently on the last focusable item and Tab is pressed.
112+
return;
113+
}
114+
96115
if (idx >= 0) {
97116
event.preventDefault();
98117
this._focus(idx, true);
@@ -101,20 +120,30 @@ export const KeyboardDirectionMixin = (superclass) =>
101120

102121
/**
103122
* @param {string} key
123+
* @param {boolean} shiftKey
104124
* @return {boolean}
105125
* @private
106126
*/
107-
__isPrevKey(key) {
108-
return this._vertical ? key === 'ArrowUp' : key === 'ArrowLeft';
127+
__isPrevKeyPressed(key, shiftKey) {
128+
if (this._vertical) {
129+
return key === 'ArrowUp';
130+
}
131+
132+
return key === 'ArrowLeft' || (this._tabNavigation && key === 'Tab' && shiftKey);
109133
}
110134

111135
/**
112136
* @param {string} key
137+
* @param {boolean} shiftKey
113138
* @return {boolean}
114139
* @private
115140
*/
116-
__isNextKey(key) {
117-
return this._vertical ? key === 'ArrowDown' : key === 'ArrowRight';
141+
__isNextKeyPressed(key, shiftKey) {
142+
if (this._vertical) {
143+
return key === 'ArrowDown';
144+
}
145+
146+
return key === 'ArrowRight' || (this._tabNavigation && key === 'Tab' && !shiftKey);
118147
}
119148

120149
/**

‎packages/a11y-base/test/keyboard-direction-mixin.test.js‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
endKeyDown,
88
fixtureSync,
99
homeKeyDown,
10+
tabKeyDown,
1011
} from '@vaadin/testing-helpers';
1112
import sinon from 'sinon';
1213
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
@@ -33,6 +34,10 @@ customElements.define(
3334
get _vertical() {
3435
return this.hasAttribute('vertical');
3536
}
37+
38+
get _tabNavigation() {
39+
return this.hasAttribute('tab-navigation');
40+
}
3641
},
3742
);
3843

@@ -86,11 +91,22 @@ describe('keyboard-direction-mixin', () => {
8691
expect(element.focused).to.be.equal(items[1]);
8792
});
8893

89-
it('should move focus to prev element on "arrow-right" keydown in LTR', () => {
94+
it('should move focus to prev element on "arrow-left" keydown in LTR', () => {
9095
arrowRightKeyDown(items[0]);
9196
arrowLeftKeyDown(items[1]);
9297
expect(element.focused).to.be.equal(items[0]);
9398
});
99+
100+
it('should move focus to last element on first element "arrow-left" keydown', () => {
101+
arrowLeftKeyDown(items[0]);
102+
expect(element.focused).to.equal(items[5]);
103+
});
104+
105+
it('should move focus to first element on last element "arrow-right" keydown', () => {
106+
arrowLeftKeyDown(items[0]);
107+
arrowRightKeyDown(items[5]);
108+
expect(element.focused).to.equal(items[0]);
109+
});
94110
});
95111

96112
describe('RTL', () => {
@@ -108,6 +124,17 @@ describe('keyboard-direction-mixin', () => {
108124
arrowRightKeyDown(items[1]);
109125
expect(element.focused).to.be.equal(items[0]);
110126
});
127+
128+
it('should move focus to last element on first element "arrow-right" keydown', () => {
129+
arrowRightKeyDown(items[0]);
130+
expect(element.focused).to.equal(items[5]);
131+
});
132+
133+
it('should move focus to first element on last element "arrow-left" keydown', () => {
134+
arrowRightKeyDown(items[0]);
135+
arrowLeftKeyDown(items[5]);
136+
expect(element.focused).to.equal(items[0]);
137+
});
111138
});
112139
});
113140

@@ -189,4 +216,33 @@ describe('keyboard-direction-mixin', () => {
189216
});
190217
});
191218
});
219+
220+
describe('Tab navigation', () => {
221+
beforeEach(() => {
222+
element.setAttribute('tab-navigation', '');
223+
element.focus();
224+
});
225+
226+
it('should move focus to next element on Tab keydown', () => {
227+
tabKeyDown(items[0]);
228+
expect(element.focused).to.be.equal(items[1]);
229+
});
230+
231+
it('should move focus to prev element on Shift + Tab keydown', () => {
232+
tabKeyDown(items[0]);
233+
tabKeyDown(items[1], ['shift']);
234+
expect(element.focused).to.be.equal(items[0]);
235+
});
236+
237+
it('should not move focus to last element on first element Shift + Tab keydown', () => {
238+
tabKeyDown(items[0], ['shift']);
239+
expect(element.focused).to.not.equal(items[5]);
240+
});
241+
242+
it('should not move focus to first element on last element Tab keydown', () => {
243+
items[5].focus();
244+
tabKeyDown(items[5]);
245+
expect(element.focused).to.not.equal(items[0]);
246+
});
247+
});
192248
});

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { DisabledMixin } from '@vaadin/a11y-base/src/disabled-mixin.js';
77
import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js';
8-
import { isElementFocused, isElementHidden, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
8+
import { isElementFocused, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
99
import { KeyboardDirectionMixin } from '@vaadin/a11y-base/src/keyboard-direction-mixin.js';
1010
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
1111
import { I18nMixin } from '@vaadin/component-base/src/i18n-mixin.js';
@@ -235,6 +235,17 @@ export const MenuBarMixin = (superClass) =>
235235
return false;
236236
}
237237

238+
/**
239+
* Override getter from `KeyboardDirectionMixin`.
240+
*
241+
* @return {boolean}
242+
* @protected
243+
* @override
244+
*/
245+
get _tabNavigation() {
246+
return this.tabNavigation;
247+
}
248+
238249
/**
239250
* Override getter from `ResizeMixin` to observe parent.
240251
*
@@ -759,12 +770,7 @@ export const MenuBarMixin = (superClass) =>
759770
*/
760771
_setFocused(focused) {
761772
if (focused) {
762-
let target = this.querySelector('[tabindex="0"]');
763-
if (this.tabNavigation) {
764-
// Switch submenu on menu button Tab / Shift Tab
765-
target = this.querySelector('[focused]');
766-
this.__switchSubMenu(target);
767-
}
773+
const target = this.tabNavigation ? this.querySelector('[focused]') : this.querySelector('[tabindex="0"]');
768774
if (target) {
769775
this._buttons.forEach((btn) => {
770776
this._setTabindex(btn, btn === target);
@@ -883,38 +889,15 @@ export const MenuBarMixin = (superClass) =>
883889
if (e.keyCode === 38 && item === list.items[0]) {
884890
this._close(true);
885891
}
886-
// ArrowLeft, or ArrowRight on non-parent submenu item
892+
// ArrowLeft, or ArrowRight on non-parent submenu item,
887893
if (e.keyCode === 37 || (e.keyCode === 39 && !item._item.children)) {
888894
// Prevent ArrowLeft from being handled in context-menu
889895
e.stopImmediatePropagation();
890896
this._onKeyDown(e);
891-
} else if (e.keyCode === 9 && this.tabNavigation) {
892-
// Switch opened submenu on submenu item Tab / Shift Tab
893-
const items = this._getItems() || [];
894-
const currentIdx = items.indexOf(this.focused);
895-
const increment = e.shiftKey ? -1 : 1;
896-
let idx = currentIdx + increment;
897-
idx = this._getAvailableIndex(items, idx, increment, (item) => !isElementHidden(item));
898-
899-
if ((idx > currentIdx && e.shiftKey) || (idx < currentIdx && !e.shiftKey)) {
900-
// Prevent "roving tabindex" logic and let the normal Tab behavior if
901-
// - currently in the first button submenu and Shift + Tab is pressed,
902-
// - currently in the last button submenu and Tab is pressed.
903-
return;
904-
}
905-
906-
this.__switchSubMenu(items[idx]);
907897
}
908-
}
909-
}
910898

911-
/** @private */
912-
__switchSubMenu(target) {
913-
const wasExpanded = this._expandedButton != null && this._expandedButton !== target;
914-
if (wasExpanded) {
915-
this._close();
916-
if (target.item && target.item.children) {
917-
this.__openSubMenu(target, true, { keepFocus: true });
899+
if (e.key === 'Tab' && this.tabNavigation) {
900+
this._onKeyDown(e);
918901
}
919902
}
920903
}

0 commit comments

Comments
 (0)