Skip to content

fix(cdk/a11y): allow for origin of already focused element to be changed #20966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cdk/a11y/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ng_test_library(
"//src/cdk/platform",
"//src/cdk/portal",
"//src/cdk/testing/private",
"@npm//@angular/common",
"@npm//@angular/platform-browser",
"@npm//rxjs",
],
Expand Down
64 changes: 61 additions & 3 deletions src/cdk/a11y/focus-monitor/focus-monitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createMouseEvent,
dispatchEvent,
} from '@angular/cdk/testing/private';
import {DOCUMENT} from '@angular/common';
import {Component, NgZone} from '@angular/core';
import {ComponentFixture, fakeAsync, flush, inject, TestBed, tick} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
Expand All @@ -25,13 +26,39 @@ describe('FocusMonitor', () => {
let buttonElement: HTMLElement;
let focusMonitor: FocusMonitor;
let changeHandler: (origin: FocusOrigin) => void;
let fakeActiveElement: HTMLElement | null;

beforeEach(() => {
fakeActiveElement = null;

TestBed.configureTestingModule({
imports: [A11yModule],
declarations: [
PlainButton,
],
declarations: [PlainButton],
providers: [{
provide: DOCUMENT,
useFactory: () => {
// We have to stub out the `document` in order to be able to fake `activeElement`.
const fakeDocument = {body: document.body};

[
'createElement',
'dispatchEvent',
'querySelectorAll',
'addEventListener',
'removeEventListener'
].forEach(method => {
(fakeDocument as any)[method] = function() {
return (document as any)[method].apply(document, arguments);
};
});

Object.defineProperty(fakeDocument, 'activeElement', {
get: () => fakeActiveElement || document.activeElement
});

return fakeDocument;
}
}]
}).compileComponents();
});

Expand Down Expand Up @@ -294,6 +321,37 @@ describe('FocusMonitor', () => {
expect(parent.classList).toContain('cdk-mouse-focused');
}));

it('focusVia should change the focus origin when called on the focused node', fakeAsync(() => {
spyOn(buttonElement, 'focus').and.callThrough();
focusMonitor.focusVia(buttonElement, 'keyboard');
flush();
fakeActiveElement = buttonElement;

expect(buttonElement.classList.length)
.toBe(2, 'button should have exactly 2 focus classes');
expect(buttonElement.classList.contains('cdk-focused'))
.toBe(true, 'button should have cdk-focused class');
expect(buttonElement.classList.contains('cdk-keyboard-focused'))
.toBe(true, 'button should have cdk-keyboard-focused class');
expect(changeHandler).toHaveBeenCalledTimes(1);
expect(changeHandler).toHaveBeenCalledWith('keyboard');
expect(buttonElement.focus).toHaveBeenCalledTimes(1);

focusMonitor.focusVia(buttonElement, 'mouse');
flush();
fakeActiveElement = buttonElement;

expect(buttonElement.classList.length)
.toBe(2, 'button should have exactly 2 focus classes');
expect(buttonElement.classList.contains('cdk-focused'))
.toBe(true, 'button should have cdk-focused class');
expect(buttonElement.classList.contains('cdk-mouse-focused'))
.toBe(true, 'button should have cdk-mouse-focused class');
expect(changeHandler).toHaveBeenCalledTimes(2);
expect(changeHandler).toHaveBeenCalledWith('mouse');
expect(buttonElement.focus).toHaveBeenCalledTimes(1);
}));

});

describe('FocusMonitor with "eventual" detection', () => {
Expand Down
30 changes: 21 additions & 9 deletions src/cdk/a11y/focus-monitor/focus-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,20 @@ export class FocusMonitor implements OnDestroy {
options?: FocusOptions): void {

const nativeElement = coerceElement(element);
const focusedElement = this._getDocument().activeElement;

this._setOriginForCurrentEventQueue(origin);
// If the element is focused already, calling `focus` again won't trigger the event listener
// which means that the focus classes won't be updated. If that's the case, update the classes
// directly without waiting for an event.
if (nativeElement === focusedElement && this._elementInfo.has(nativeElement)) {
this._originChanged(nativeElement, origin, this._elementInfo.get(nativeElement)!);
} else {
this._setOriginForCurrentEventQueue(origin);

// `focus` isn't available on the server
if (typeof nativeElement.focus === 'function') {
// Cast the element to `any`, because the TS typings don't have the `options` parameter yet.
(nativeElement as any).focus(options);
// `focus` isn't available on the server
if (typeof nativeElement.focus === 'function') {
nativeElement.focus(options);
}
}
}

Expand Down Expand Up @@ -438,10 +445,7 @@ export class FocusMonitor implements OnDestroy {
return;
}

const origin = this._getFocusOrigin(event);
this._setClasses(element, origin);
this._emitOrigin(elementInfo.subject, origin);
this._lastFocusOrigin = origin;
this._originChanged(element, this._getFocusOrigin(event), elementInfo);
}

/**
Expand Down Expand Up @@ -541,6 +545,14 @@ export class FocusMonitor implements OnDestroy {
clearTimeout(this._originTimeoutId);
}
}

/** Updates all the state on an element once its focus origin has changed. */
private _originChanged(element: HTMLElement, origin: FocusOrigin,
elementInfo: MonitoredElementInfo) {
this._setClasses(element, origin);
this._emitOrigin(elementInfo.subject, origin);
this._lastFocusOrigin = origin;
}
}

/** Gets the target of an event, accounting for Shadow DOM. */
Expand Down
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,9 @@ describe('MDC-based MatMenu', () => {
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

// Reset the automatic focus when the menu is opened.
(document.activeElement as HTMLElement)?.blur();

const panel = overlayContainerElement.querySelector('.mat-mdc-menu-panel')!;
const items = Array.from(panel.querySelectorAll('.mat-mdc-menu-item')) as HTMLElement[];
items.forEach(patchElementFocus);
Expand Down
3 changes: 3 additions & 0 deletions src/material/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ describe('MatMenu', () => {
fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

// Reset the automatic focus when the menu is opened.
(document.activeElement as HTMLElement)?.blur();

const panel = overlayContainerElement.querySelector('.mat-menu-panel')!;
const items = Array.from(panel.querySelectorAll('.mat-menu-item')) as HTMLElement[];
items.forEach(patchElementFocus);
Expand Down