Skip to content

Commit

Permalink
fix(material/button): cdk-focus classes not being applied (#25619)
Browse files Browse the repository at this point in the history
Fixes a bug in the Angular Material `button` component where
cdk-focused, cdk-keyboard-focused, cdk-mouse-focused were
not being applied upon respective focus. This is because
there was no FocusManager set up with the MDC buttons like
the legacy button did.

Fixes #25618
  • Loading branch information
kevinjihwanlee committed Sep 30, 2022
1 parent 8ae0b2c commit 9f0071d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/material/button/_button-theme-private.scss
Expand Up @@ -18,8 +18,11 @@
opacity: map.get($opacities, hover);
}

&:focus .mat-mdc-button-persistent-ripple::before {
opacity: map.get($opacities, focus);
&.cdk-program-focused,
&.cdk-keyboard-focused {
.mat-mdc-button-persistent-ripple::before {
opacity: map.get($opacities, focus);
}
}

&:active .mat-mdc-button-persistent-ripple::before {
Expand Down
34 changes: 29 additions & 5 deletions src/material/button/button-base.ts
Expand Up @@ -6,8 +6,18 @@
* found in the LICENSE file at https://angular.io/license
*/

import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
import {Platform} from '@angular/cdk/platform';
import {Directive, ElementRef, NgZone, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {
AfterViewInit,
Directive,
ElementRef,
inject,
NgZone,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import {
CanColor,
CanDisable,
Expand All @@ -17,7 +27,6 @@ import {
mixinDisabled,
mixinDisableRipple,
} from '@angular/material/core';
import {FocusOrigin} from '@angular/cdk/a11y';

/** Inputs common to all buttons. */
export const MAT_BUTTON_INPUTS = ['disabled', 'disableRipple', 'color'];
Expand Down Expand Up @@ -83,8 +92,10 @@ export const _MatButtonMixin = mixinColor(
@Directive()
export class MatButtonBase
extends _MatButtonMixin
implements CanDisable, CanColor, CanDisableRipple
implements CanDisable, CanColor, CanDisableRipple, AfterViewInit, OnDestroy
{
private readonly _focusMonitor = inject(FocusMonitor);

/** Whether this button is a FAB. Used to apply the correct class on the ripple. */
_isFab = false;

Expand Down Expand Up @@ -112,9 +123,21 @@ export class MatButtonBase
}
}

ngAfterViewInit() {
this._focusMonitor.monitor(this._elementRef, true);
}

ngOnDestroy() {
this._focusMonitor.stopMonitoring(this._elementRef);
}

/** Focuses the button. */
focus(_origin: FocusOrigin = 'program', options?: FocusOptions): void {
this._elementRef.nativeElement.focus(options);
if (_origin) {
this._focusMonitor.focusVia(this._elementRef.nativeElement, _origin, options);
} else {
this._elementRef.nativeElement.focus(options);
}
}

/** Gets whether the button has one of the given attributes. */
Expand Down Expand Up @@ -166,7 +189,8 @@ export class MatAnchorBase extends MatButtonBase implements OnInit, OnDestroy {
});
}

ngOnDestroy(): void {
override ngOnDestroy(): void {
super.ngOnDestroy();
this._elementRef.nativeElement.removeEventListener('click', this._haltDisabledEvents);
}

Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/button.md
Expand Up @@ -5,6 +5,7 @@
```ts

import { _AbstractConstructor } from '@angular/material/core';
import { AfterViewInit } from '@angular/core';
import { BooleanInput } from '@angular/cdk/coercion';
import { CanColor } from '@angular/material/core';
import { CanDisable } from '@angular/material/core';
Expand Down

0 comments on commit 9f0071d

Please sign in to comment.