Skip to content

Commit 4614fb8

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

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/lib/badge/_badge-theme.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ $mat-badge-large-size: $mat-badge-default-size + 6;
163163
pointer-events: none;
164164
}
165165

166+
.ng-animate-disabled .mat-badge-content,
167+
.mat-badge-content._mat-animation-noopable {
168+
transition: none;
169+
}
170+
166171
// The active class is added after the element is added
167172
// so it can animate scale to default
168173
.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;
@@ -128,9 +129,10 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
128129
private _elementRef: ElementRef<HTMLElement>,
129130
private _ariaDescriber: AriaDescriber,
130131
/** @breaking-change 8.0.0 Make _renderer a required param and remove _document. */
131-
private _renderer?: Renderer2) {
132-
super();
133-
}
132+
private _renderer?: Renderer2,
133+
@Optional() @Inject(ANIMATION_MODULE_TYPE) private _animationMode?: string) {
134+
super();
135+
}
134136

135137
/** Whether the badge is above the host or not */
136138
isAbove(): boolean {
@@ -180,14 +182,18 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
180182
badgeElement.classList.add('mat-badge-content');
181183
badgeElement.textContent = this.content;
182184

185+
if (this._animationMode === 'NoopAnimations') {
186+
badgeElement.classList.add('_mat-animation-noopable');
187+
}
188+
183189
if (this.description) {
184190
badgeElement.setAttribute('aria-label', this.description);
185191
}
186192

187193
this._elementRef.nativeElement.appendChild(badgeElement);
188194

189195
// animate in after insertion
190-
if (typeof requestAnimationFrame === 'function') {
196+
if (typeof requestAnimationFrame === 'function' && this._animationMode !== 'NoopAnimations') {
191197
this._ngZone.runOutsideAngular(() => {
192198
requestAnimationFrame(() => {
193199
badgeElement.classList.add(activeClass);

tools/public_api_guard/lib/badge.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, C
1111
position: MatBadgePosition;
1212
size: MatBadgeSize;
1313
constructor(_document: any, _ngZone: NgZone, _elementRef: ElementRef<HTMLElement>, _ariaDescriber: AriaDescriber,
14-
_renderer?: Renderer2 | undefined);
14+
_renderer?: Renderer2 | undefined, _animationMode?: string | undefined);
1515
isAbove(): boolean;
1616
isAfter(): boolean;
1717
ngOnDestroy(): void;

0 commit comments

Comments
 (0)