Skip to content

Commit e64e89e

Browse files
authored
fix: restore focused attribute behavior (#3577)
* fix: restore focused attribute behavior * add test for shift-tabbing out of component * update test to move focus from focusElement
1 parent aa79465 commit e64e89e

2 files changed

Lines changed: 52 additions & 39 deletions

File tree

‎packages/field-base/src/shadow-focus-mixin.js‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,18 @@ export const ShadowFocusMixin = (superClass) =>
3131

3232
/**
3333
* Override an event listener from `KeyboardMixin`
34-
* to prevent setting `focused` on Shift Tab.
34+
* to prevent focusing the host element on Shift Tab.
3535
* @param {KeyboardEvent} event
3636
* @protected
3737
* @override
3838
*/
3939
_onKeyDown(event) {
4040
super._onKeyDown(event);
4141

42-
// When focus moves with Shift + Tab, do not mark host as focused.
43-
// The flag set here will be later used in focusin event listener.
42+
// When focus moves with Shift + Tab, skip focusing the host element
43+
// by focusing it before the default browser focus handling runs
4444
if (!event.defaultPrevented && event.keyCode === 9 && event.shiftKey) {
45-
this._isShiftTabbing = true;
4645
HTMLElement.prototype.focus.apply(this);
47-
this._setFocused(false);
48-
setTimeout(() => (this._isShiftTabbing = false), 0);
4946
}
5047
}
5148

@@ -61,15 +58,13 @@ export const ShadowFocusMixin = (superClass) =>
6158
if (!this.disabled && this.focusElement) {
6259
const path = event.composedPath();
6360

64-
// When focus moves from outside and not with Shift + Tab, delegate it to focusElement.
61+
// When focus moves to the host element itself, then delegate it to the focusElement
6562
// This should only move focus when using keyboard navigation, for clicks we don't want to interfere,
6663
// for example when the user tries to select some text
67-
if (this._keyboardActive && path[0] === this && !this.contains(event.relatedTarget) && !this._isShiftTabbing) {
64+
if (path[0] === this && this._keyboardActive) {
6865
this.focusElement.focus();
69-
return true;
7066
}
71-
72-
if (path.includes(this.focusElement)) {
67+
if (path[0] === this || path.includes(this.focusElement)) {
7368
return true;
7469
}
7570
}

‎packages/field-base/test/shadow-focus-mixin.test.js‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { aTimeout, fixtureSync, focusin, focusout, keyboardEventFor, keyDownOn } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, focusin, focusout, keyDownOn } from '@vaadin/testing-helpers';
33
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
@@ -12,6 +12,7 @@ customElements.define(
1212
return html`
1313
<input id="input" />
1414
<input id="secondInput" />
15+
<input id="thirdInput" />
1516
<slot></slot>
1617
`;
1718
}
@@ -35,6 +36,13 @@ customElements.define(
3536
}
3637
);
3738

39+
async function click(element) {
40+
const rect = element.getBoundingClientRect();
41+
const middleX = Math.floor(rect.x + rect.width / 2);
42+
const middleY = Math.floor(rect.y + rect.height / 2);
43+
await sendMouse({ type: 'click', position: [middleX, middleY] });
44+
}
45+
3846
describe('shadow-focus-mixin', () => {
3947
let customElement, focusElement;
4048

@@ -97,19 +105,6 @@ describe('shadow-focus-mixin', () => {
97105
});
98106

99107
describe('focus-ring', () => {
100-
it('should set _isShiftTabbing when pressing shift-tab', () => {
101-
const event = keyboardEventFor('keydown', 9, 'shift');
102-
customElement.dispatchEvent(event);
103-
expect(customElement._isShiftTabbing).to.be.true;
104-
});
105-
106-
it('should skip setting _isShiftTabbing if event is defaultPrevented', () => {
107-
const evt = keyboardEventFor('keydown', 9, 'shift');
108-
evt.preventDefault();
109-
customElement.dispatchEvent(evt);
110-
expect(customElement._isShiftTabbing).not.to.be.ok;
111-
});
112-
113108
it('should set the focus-ring attribute when TAB is pressed and focus is received', () => {
114109
keyDownOn(document.body, 9);
115110
focusin(focusElement);
@@ -198,11 +193,6 @@ describe('shadow-focus-mixin', () => {
198193
expect(spy.calledOnce).to.be.true;
199194
});
200195

201-
it('should not set focused attribute on host click', () => {
202-
customElement.click();
203-
expect(customElement.hasAttribute('focused')).to.be.false;
204-
});
205-
206196
it('should set focused attribute on focusin event', () => {
207197
focusin(focusElement);
208198
expect(customElement.hasAttribute('focused')).to.be.true;
@@ -213,6 +203,7 @@ describe('shadow-focus-mixin', () => {
213203
const spy = sinon.spy(focusElement, 'focus');
214204
await sendKeys({ press: 'Tab' });
215205
expect(spy.called).to.be.true;
206+
expect(customElement.hasAttribute('focused')).to.be.true;
216207
});
217208

218209
it('should not delegate focus when clicking on non-focusable child', async () => {
@@ -221,21 +212,48 @@ describe('shadow-focus-mixin', () => {
221212
customElement.appendChild(span);
222213

223214
const spy = sinon.spy(focusElement, 'focus');
224-
// Click center of span child element
225-
const rect = span.getBoundingClientRect();
226-
const middleX = Math.floor(rect.x + rect.width / 2);
227-
const middleY = Math.floor(rect.y + rect.height / 2);
228-
await sendMouse({ type: 'click', position: [middleX, middleY] });
229-
// Clicking on some text content should not move focus
215+
await click(span);
216+
// Clicking on some text content should not move focus to focus element
230217
expect(spy.called).to.be.false;
231218
});
232219

233220
it('should not delegate focus if the focus is not received from outside', () => {
234-
const child = document.createElement('div');
235-
customElement.appendChild(child);
221+
customElement.focusElement.focus();
222+
const spy = sinon.spy(focusElement, 'focus');
223+
customElement.$.secondInput.focus();
224+
customElement.$.thirdInput.focus();
225+
customElement.$.secondInput.focus();
226+
expect(spy.called).to.be.false;
227+
});
236228

237-
focusin(customElement, child);
229+
it('should skip host element in tab order when shift-tabbing', async () => {
230+
const siblingInput = document.createElement('input');
231+
customElement.parentElement.insertBefore(siblingInput, customElement);
238232

233+
customElement.focusElement.focus();
234+
// Move focus back to body
235+
await sendKeys({ down: 'Shift' });
236+
await sendKeys({ press: 'Tab' });
237+
await sendKeys({ up: 'Shift' });
238+
expect(document.activeElement).to.equal(siblingInput);
239+
});
240+
241+
it('should set focused attribute when clicking on non-focusable slotted element', async () => {
242+
const span = document.createElement('span');
243+
span.textContent = 'test';
244+
customElement.appendChild(span);
245+
246+
await click(span);
247+
expect(customElement.hasAttribute('focused')).to.be.true;
248+
});
249+
250+
it('should remove focused attribute when moving to focusable slotted element', async () => {
251+
const input = document.createElement('input');
252+
customElement.appendChild(input);
253+
254+
customElement.focus();
255+
expect(customElement.hasAttribute('focused')).to.be.true;
256+
input.focus();
239257
expect(customElement.hasAttribute('focused')).to.be.false;
240258
});
241259

0 commit comments

Comments
 (0)