Skip to content

Commit 695dde6

Browse files
authored
fix(a11y): focus monitor incorrectly detecting fake mousedown… (#15214)
In some cases screen readers dispatch a fake `mousedown` event, instead of a `keydown` which causes the `FocusMonitor` to register focus as if it's coming from a keyboard interaction. An example where this is visible is in the `mat-datepicker` calendar where having screen reader on and opening the calendar with the keyboard won't show the focus indication, whereas it's visible if the screen reader is turned off. These changes add an extra check that will detect fake mousedown events correctly.
1 parent 9a16e60 commit 695dde6

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/cdk/a11y/focus-monitor/focus-monitor.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
dispatchKeyboardEvent,
55
dispatchMouseEvent,
66
patchElementFocus,
7+
createMouseEvent,
8+
dispatchEvent,
79
} from '@angular/cdk/testing/private';
810
import {Component, NgZone} from '@angular/core';
911
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
@@ -118,6 +120,26 @@ describe('FocusMonitor', () => {
118120
expect(changeHandler).toHaveBeenCalledWith('program');
119121
}));
120122

123+
it('should detect fake mousedown from a screen reader', fakeAsync(() => {
124+
// Simulate focus via a fake mousedown from a screen reader.
125+
dispatchMouseEvent(buttonElement, 'mousedown');
126+
const event = createMouseEvent('mousedown');
127+
Object.defineProperty(event, 'buttons', {get: () => 0});
128+
dispatchEvent(buttonElement, event);
129+
130+
buttonElement.focus();
131+
fixture.detectChanges();
132+
flush();
133+
134+
expect(buttonElement.classList.length)
135+
.toBe(2, 'button should have exactly 2 focus classes');
136+
expect(buttonElement.classList.contains('cdk-focused'))
137+
.toBe(true, 'button should have cdk-focused class');
138+
expect(buttonElement.classList.contains('cdk-keyboard-focused'))
139+
.toBe(true, 'button should have cdk-keyboard-focused class');
140+
expect(changeHandler).toHaveBeenCalledWith('keyboard');
141+
}));
142+
121143
it('focusVia keyboard should simulate keyboard focus', fakeAsync(() => {
122144
focusMonitor.focusVia(buttonElement, 'keyboard');
123145
flush();

src/cdk/a11y/focus-monitor/focus-monitor.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import {Observable, of as observableOf, Subject, Subscription} from 'rxjs';
2323
import {coerceElement} from '@angular/cdk/coercion';
2424
import {DOCUMENT} from '@angular/common';
25+
import {isFakeMousedownFromScreenReader} from '../fake-mousedown';
2526

2627

2728
// This is the value used by AngularJS Material. Through trial and error (on iPhone 6S) they found
@@ -129,11 +130,14 @@ export class FocusMonitor implements OnDestroy {
129130
* Event listener for `mousedown` events on the document.
130131
* Needs to be an arrow function in order to preserve the context when it gets bound.
131132
*/
132-
private _documentMousedownListener = () => {
133+
private _documentMousedownListener = (event: MouseEvent) => {
133134
// On mousedown record the origin only if there is not touch
134135
// target, since a mousedown can happen as a result of a touch event.
135136
if (!this._lastTouchTarget) {
136-
this._setOriginForCurrentEventQueue('mouse');
137+
// In some cases screen readers fire fake `mousedown` events instead of `keydown`.
138+
// Resolve the focus source to `keyboard` if we detect one of them.
139+
const source = isFakeMousedownFromScreenReader(event) ? 'keyboard' : 'mouse';
140+
this._setOriginForCurrentEventQueue(source);
137141
}
138142
}
139143

0 commit comments

Comments
 (0)