Skip to content

fix(material/button-toggle): fix ChromeVox focus issue on button toggle #21046

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 2 commits into from
Dec 3, 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
22 changes: 14 additions & 8 deletions src/cdk/a11y/focus-trap/focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class FocusTrap {
* Waits for the zone to stabilize, then either focuses the first element that the
* user specified, or the first tabbable element.
* @returns Returns a promise that resolves with a boolean, depending
* on whether focus was moved successfuly.
* on whether focus was moved successfully.
*/
focusInitialElementWhenReady(): Promise<boolean> {
return new Promise<boolean>(resolve => {
Expand All @@ -142,7 +142,7 @@ export class FocusTrap {
* Waits for the zone to stabilize, then focuses
* the first tabbable element within the focus trap region.
* @returns Returns a promise that resolves with a boolean, depending
* on whether focus was moved successfuly.
* on whether focus was moved successfully.
*/
focusFirstTabbableElementWhenReady(): Promise<boolean> {
return new Promise<boolean>(resolve => {
Expand All @@ -154,7 +154,7 @@ export class FocusTrap {
* Waits for the zone to stabilize, then focuses
* the last tabbable element within the focus trap region.
* @returns Returns a promise that resolves with a boolean, depending
* on whether focus was moved successfuly.
* on whether focus was moved successfully.
*/
focusLastTabbableElementWhenReady(): Promise<boolean> {
return new Promise<boolean>(resolve => {
Expand Down Expand Up @@ -195,7 +195,7 @@ export class FocusTrap {

/**
* Focuses the element that should be focused when the focus trap is initialized.
* @returns Whether focus was moved successfuly.
* @returns Whether focus was moved successfully.
*/
focusInitialElement(): boolean {
// Contains the deprecated version of selector, for temporary backwards comparability.
Expand All @@ -217,6 +217,12 @@ export class FocusTrap {
console.warn(`Element matching '[cdkFocusInitial]' is not focusable.`, redirectToElement);
}

if (!this._checker.isFocusable(redirectToElement)) {
const focusableChild = this._getFirstTabbableElement(redirectToElement) as HTMLElement;
focusableChild?.focus();
return !!focusableChild;
}

redirectToElement.focus();
return true;
}
Expand All @@ -226,7 +232,7 @@ export class FocusTrap {

/**
* Focuses the first tabbable element within the focus trap region.
* @returns Whether focus was moved successfuly.
* @returns Whether focus was moved successfully.
*/
focusFirstTabbableElement(): boolean {
const redirectToElement = this._getRegionBoundary('start');
Expand All @@ -240,7 +246,7 @@ export class FocusTrap {

/**
* Focuses the last tabbable element within the focus trap region.
* @returns Whether focus was moved successfuly.
* @returns Whether focus was moved successfully.
*/
focusLastTabbableElement(): boolean {
const redirectToElement = this._getRegionBoundary('end');
Expand All @@ -253,7 +259,7 @@ export class FocusTrap {
}

/**
* Checks whether the focus trap has successfuly been attached.
* Checks whether the focus trap has successfully been attached.
*/
hasAttached(): boolean {
return this._hasAttached;
Expand Down Expand Up @@ -396,7 +402,7 @@ export class CdkTrapFocus implements OnDestroy, AfterContentInit, OnChanges, DoC
set enabled(value: boolean) { this.focusTrap.enabled = coerceBooleanProperty(value); }

/**
* Whether the directive should automatially move focus into the trapped region upon
* Whether the directive should automatically move focus into the trapped region upon
* initialization and return focus to the previous activeElement upon destruction.
*/
@Input('cdkTrapFocusAutoCapture')
Expand Down
4 changes: 2 additions & 2 deletions src/material/button-toggle/button-toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,13 @@ describe('MatButtonToggle without forms', () => {
expect(button.getAttribute('tabindex')).toBe('3');
});

it('should clear the tabindex from the host element', () => {
it('should have role "presentation"', () => {
const fixture = TestBed.createComponent(ButtonToggleWithTabindex);
fixture.detectChanges();

const host = fixture.nativeElement.querySelector('.mat-button-toggle');

expect(host.getAttribute('tabindex')).toBe('-1');
expect(host.getAttribute('role')).toBe('presentation');
});

it('should forward focus to the underlying button when the host is focused', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/material/button-toggle/button-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,12 @@ const _MatButtonToggleMixinBase: CanDisableRippleCtor & typeof MatButtonToggleBa
'[class.mat-button-toggle-disabled]': 'disabled',
'[class.mat-button-toggle-appearance-standard]': 'appearance === "standard"',
'class': 'mat-button-toggle',
// Always reset the tabindex to -1 so it doesn't conflict with the one on the `button`,
// but can still receive focus from things like cdkFocusInitial.
'[attr.tabindex]': '-1',
'[attr.aria-label]': 'null',
'[attr.aria-labelledby]': 'null',
'[attr.id]': 'id',
'[attr.name]': 'null',
'(focus)': 'focus()',
'role': 'presentation',
}
})
export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit, AfterViewInit,
Expand Down