Skip to content

Commit d894158

Browse files
crisbetojosephperrott
authored andcommitted
fix(badge): disable animations when using NoopAnimationsModule (#11408)
1 parent b1045ee commit d894158

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/lib/badge/_badge-theme.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ $mat-badge-large-size: $mat-badge-default-size + 6;
155155
pointer-events: none;
156156
}
157157

158+
.ng-animate-disabled .mat-badge-content,
159+
.mat-badge-content._mat-animation-noopable {
160+
transition: none;
161+
}
162+
158163
// The active class is added after the element is added
159164
// so it can animate scale to default
160165
.mat-badge-content.mat-badge-active {

src/lib/badge/badge.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Renderer2,
2121
} from '@angular/core';
2222
import {ThemePalette, mixinDisabled, CanDisableCtor, CanDisable} from '@angular/material/core';
23+
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2324

2425

2526
let nextId = 0;
@@ -122,9 +123,10 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
122123
private _elementRef: ElementRef<HTMLElement>,
123124
private _ariaDescriber: AriaDescriber,
124125
/** @breaking-change 8.0.0 Make _renderer a required param and remove _document. */
125-
private _renderer?: Renderer2) {
126-
super();
127-
}
126+
private _renderer?: Renderer2,
127+
@Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string) {
128+
super();
129+
}
128130

129131
/** Whether the badge is above the host or not */
130132
isAbove(): boolean {
@@ -163,14 +165,18 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
163165
badgeElement.classList.add('mat-badge-content');
164166
badgeElement.textContent = this.content;
165167

168+
if (this._animationMode === 'NoopAnimations') {
169+
badgeElement.classList.add('_mat-animation-noopable');
170+
}
171+
166172
if (this.description) {
167173
badgeElement.setAttribute('aria-label', this.description);
168174
}
169175

170176
this._elementRef.nativeElement.appendChild(badgeElement);
171177

172178
// animate in after insertion
173-
if (typeof requestAnimationFrame === 'function') {
179+
if (typeof requestAnimationFrame === 'function' && this._animationMode !== 'NoopAnimations') {
174180
this._ngZone.runOutsideAngular(() => {
175181
requestAnimationFrame(() => {
176182
badgeElement.classList.add(activeClass);

tools/public_api_guard/lib/badge.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export declare const _MatBadgeMixinBase: CanDisableCtor & typeof MatBadgeBase;
2+
3+
export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisable {
4+
_hasContent: boolean;
5+
_id: number;
6+
color: ThemePalette;
7+
content: string;
8+
description: string;
9+
hidden: boolean;
10+
overlap: boolean;
11+
position: MatBadgePosition;
12+
size: MatBadgeSize;
13+
constructor(_document: any, _ngZone: NgZone, _elementRef: ElementRef<HTMLElement>, _ariaDescriber: AriaDescriber,
14+
_renderer?: Renderer2 | undefined, _animationMode?: string | undefined);
15+
isAbove(): boolean;
16+
isAfter(): boolean;
17+
ngOnDestroy(): void;
18+
}
19+
20+
export declare class MatBadgeBase {
21+
}
22+
23+
export declare class MatBadgeModule {
24+
}
25+
26+
export declare type MatBadgePosition = 'above after' | 'above before' | 'below before' | 'below after';
27+
28+
export declare type MatBadgeSize = 'small' | 'medium' | 'large';

0 commit comments

Comments
 (0)