Skip to content

Commit 0a4cd91

Browse files
committed
refactor(multiple): consolidate logic for disabling animations
Adds a common function we can use to determine whether animations are disabled globally. This reduces duplication and makes it easier to change the logic.
1 parent 1c223d9 commit 0a4cd91

File tree

43 files changed

+121
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+121
-216
lines changed

goldens/material/checkbox/index.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ export class MatCheckbox implements AfterViewInit, OnChanges, ControlValueAccess
3535
indeterminateToChecked: string;
3636
indeterminateToUnchecked: string;
3737
};
38-
// (undocumented)
39-
_animationMode?: "NoopAnimations" | "BrowserAnimations" | null | undefined;
4038
ariaControls: string;
4139
ariaDescribedby: string;
4240
ariaExpanded: boolean;

goldens/material/core/index.api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export class AnimationDurations {
5151
static EXITING: string;
5252
}
5353

54+
// @public
55+
export function _animationsDisabled(): boolean;
56+
5457
// @public
5558
export function _countGroupLabelsBeforeOption(optionIndex: number, options: QueryList<MatOption>, optionGroups: QueryList<MatOptgroup>): number;
5659

@@ -318,7 +321,7 @@ export class MatOptionSelectionChange<T = any> {
318321
export class MatPseudoCheckbox {
319322
constructor(...args: unknown[]);
320323
// (undocumented)
321-
_animationMode?: "NoopAnimations" | "BrowserAnimations" | null | undefined;
324+
_animationsDisabled: boolean;
322325
appearance: 'minimal' | 'full';
323326
disabled: boolean;
324327
state: MatPseudoCheckboxState;

goldens/material/progress-bar/index.api.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ export function MAT_PROGRESS_BAR_LOCATION_FACTORY(): MatProgressBarLocation;
2525
export class MatProgressBar implements AfterViewInit, OnDestroy {
2626
constructor(...args: unknown[]);
2727
readonly animationEnd: EventEmitter<ProgressAnimationEnd>;
28-
// (undocumented)
29-
_animationMode?: "NoopAnimations" | "BrowserAnimations" | null | undefined;
3028
get bufferValue(): number;
3129
set bufferValue(v: number);
3230
get color(): string | null | undefined;

goldens/material/sort/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export interface MatSortDefaultOptions {
100100
export class MatSortHeader implements MatSortable, OnDestroy, OnInit, AfterViewInit {
101101
constructor(...args: unknown[]);
102102
// (undocumented)
103-
protected _animationModule: "NoopAnimations" | "BrowserAnimations" | null;
103+
protected _animationsDisabled: boolean;
104104
arrowPosition: SortHeaderArrowPosition;
105105
// (undocumented)
106106
_columnDef: MatSortHeaderColumnDef | null;

goldens/material/tabs/index.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export abstract class MatPaginatedTabHeader implements AfterContentChecked, Afte
7070
constructor(...args: unknown[]);
7171
_alignInkBarToSelectedTab(): void;
7272
// (undocumented)
73-
_animationMode: "NoopAnimations" | "BrowserAnimations" | null;
73+
_animationsDisabled: boolean;
7474
// (undocumented)
7575
protected _changeDetectorRef: ChangeDetectorRef;
7676
_checkPaginationEnabled(): void;
@@ -246,7 +246,7 @@ export class MatTabGroup implements AfterViewInit, AfterContentInit, AfterConten
246246
get animationDuration(): string;
247247
set animationDuration(value: string | number);
248248
// (undocumented)
249-
_animationMode: "NoopAnimations" | "BrowserAnimations" | null;
249+
_animationsDisabled: boolean;
250250
ariaLabel: string;
251251
ariaLabelledby: string;
252252
// @deprecated

src/dev-app/checkbox/checkbox-demo.html

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -255,46 +255,6 @@ <h5>Click action: noop</h5>
255255
}
256256
</mat-checkbox>
257257
</div>
258-
<div animationsNoop>
259-
<h5>No animations</h5>
260-
<mat-checkbox
261-
[required]="demoRequired"
262-
[labelPosition]="demoLabelAfter ? 'after' : 'before'"
263-
[aria-label]="demoLabel"
264-
[aria-labelledby]="demoLabelledBy"
265-
[color]="demoColor"
266-
[disableRipple]="demoDisableRipple"
267-
[id]="demoId"
268-
[name]="demoName"
269-
[value]="demoValue"
270-
[checked]="demoChecked"
271-
[disabled]="demoDisabled"
272-
[indeterminate]="demoIndeterminate"
273-
(change)="demoChecked = $event.checked"
274-
(indeterminateChange)="demoIndeterminate = $event">
275-
@if (!demoHideLabel) {
276-
Checkbox w/ [checked] & (change)
277-
}
278-
</mat-checkbox>
279-
<mat-checkbox
280-
[required]="demoRequired"
281-
[labelPosition]="demoLabelAfter ? 'after' : 'before'"
282-
[aria-label]="demoLabel"
283-
[aria-labelledby]="demoLabelledBy"
284-
[color]="demoColor"
285-
[disableRipple]="demoDisableRipple"
286-
[id]="demoId"
287-
[name]="demoName"
288-
[value]="demoValue"
289-
[disabled]="demoDisabled"
290-
[indeterminate]="demoIndeterminate"
291-
[(ngModel)]="demoChecked"
292-
(indeterminateChange)="demoIndeterminate = $event">
293-
@if (!demoHideLabel) {
294-
Checkbox w/ [(ngModel)]
295-
}
296-
</mat-checkbox>
297-
</div>
298258
</div>
299259

300260
<p>

src/dev-app/checkbox/checkbox-demo.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ANIMATION_MODULE_TYPE, ChangeDetectionStrategy, Component, Directive} from '@angular/core';
9+
import {ChangeDetectionStrategy, Component, Directive} from '@angular/core';
1010
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
1111
import {MAT_CHECKBOX_DEFAULT_OPTIONS, MatCheckboxModule} from '@angular/material/checkbox';
1212
import {MatPseudoCheckboxModule, ThemePalette} from '@angular/material/core';
@@ -32,12 +32,6 @@ export class ClickActionNoop {}
3232
})
3333
export class ClickActionCheck {}
3434

35-
@Directive({
36-
selector: '[animationsNoop]',
37-
providers: [{provide: ANIMATION_MODULE_TYPE, useValue: 'NoopAnimations'}],
38-
})
39-
export class AnimationsNoop {}
40-
4135
@Component({
4236
selector: 'mat-checkbox-demo-nested-checklist',
4337
styles: `
@@ -107,7 +101,6 @@ export class MatCheckboxDemoNestedChecklist {
107101
MatCheckboxDemoNestedChecklist,
108102
ClickActionNoop,
109103
ClickActionCheck,
110-
AnimationsNoop,
111104
MatTooltip,
112105
],
113106
changeDetection: ChangeDetectionStrategy.OnPush,

src/material/autocomplete/autocomplete.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import {
10-
ANIMATION_MODULE_TYPE,
1110
AfterContentInit,
1211
ChangeDetectionStrategy,
1312
ChangeDetectorRef,
@@ -27,6 +26,7 @@ import {
2726
inject,
2827
} from '@angular/core';
2928
import {
29+
_animationsDisabled,
3030
MAT_OPTGROUP,
3131
MAT_OPTION_PARENT_COMPONENT,
3232
MatOptgroup,
@@ -124,8 +124,7 @@ export class MatAutocomplete implements AfterContentInit, OnDestroy {
124124
private _changeDetectorRef = inject(ChangeDetectorRef);
125125
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
126126
protected _defaults = inject<MatAutocompleteDefaultOptions>(MAT_AUTOCOMPLETE_DEFAULT_OPTIONS);
127-
protected _animationsDisabled =
128-
inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations';
127+
protected _animationsDisabled = _animationsDisabled();
129128
private _activeOptionChanges = Subscription.EMPTY;
130129

131130
/** Manages active item in option list based on key events. */

src/material/badge/badge.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import {
2121
OnInit,
2222
Renderer2,
2323
ViewEncapsulation,
24-
ANIMATION_MODULE_TYPE,
2524
} from '@angular/core';
26-
import {ThemePalette} from '../core';
25+
import {_animationsDisabled, ThemePalette} from '../core';
2726
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
2827

2928
/** Allowed position options for matBadgePosition */
@@ -76,7 +75,7 @@ export class MatBadge implements OnInit, OnDestroy {
7675
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
7776
private _ariaDescriber = inject(AriaDescriber);
7877
private _renderer = inject(Renderer2);
79-
private _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});
78+
private _animationsDisabled = _animationsDisabled();
8079
private _idGenerator = inject(_IdGenerator);
8180

8281
/**
@@ -240,14 +239,14 @@ export class MatBadge implements OnInit, OnDestroy {
240239
badgeElement.setAttribute('aria-hidden', 'true');
241240
badgeElement.classList.add(BADGE_CONTENT_CLASS);
242241

243-
if (this._animationMode === 'NoopAnimations') {
242+
if (this._animationsDisabled) {
244243
badgeElement.classList.add('_mat-animation-noopable');
245244
}
246245

247246
this._elementRef.nativeElement.appendChild(badgeElement);
248247

249248
// animate in after insertion
250-
if (typeof requestAnimationFrame === 'function' && this._animationMode !== 'NoopAnimations') {
249+
if (typeof requestAnimationFrame === 'function' && !this._animationsDisabled) {
251250
this._ngZone.runOutsideAngular(() => {
252251
requestAnimationFrame(() => {
253252
badgeElement.classList.add(activeClass);

src/material/bottom-sheet/bottom-sheet-container.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import {CdkDialogContainer} from '@angular/cdk/dialog';
1010
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
1111
import {
12-
ANIMATION_MODULE_TYPE,
1312
ChangeDetectionStrategy,
1413
Component,
1514
EventEmitter,
@@ -19,6 +18,7 @@ import {
1918
} from '@angular/core';
2019
import {Subscription} from 'rxjs';
2120
import {CdkPortalOutlet} from '@angular/cdk/portal';
21+
import {_animationsDisabled} from '../core';
2222

2323
const ENTER_ANIMATION = '_mat-bottom-sheet-enter';
2424
const EXIT_ANIMATION = '_mat-bottom-sheet-exit';
@@ -54,8 +54,7 @@ const EXIT_ANIMATION = '_mat-bottom-sheet-exit';
5454
})
5555
export class MatBottomSheetContainer extends CdkDialogContainer implements OnDestroy {
5656
private _breakpointSubscription: Subscription;
57-
protected _animationsDisabled =
58-
inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations';
57+
protected _animationsDisabled = _animationsDisabled();
5958

6059
/** The state of the bottom sheet animations. */
6160
_animationState: 'void' | 'visible' | 'hidden' = 'void';

src/material/button-toggle/button-toggle.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
1414
import {
1515
AfterContentInit,
1616
AfterViewInit,
17-
ANIMATION_MODULE_TYPE,
1817
booleanAttribute,
1918
ChangeDetectionStrategy,
2019
ChangeDetectorRef,
@@ -36,7 +35,7 @@ import {
3635
ViewEncapsulation,
3736
} from '@angular/core';
3837
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
39-
import {_StructuralStylesLoader, MatPseudoCheckbox, MatRipple} from '../core';
38+
import {_animationsDisabled, _StructuralStylesLoader, MatPseudoCheckbox, MatRipple} from '../core';
4039

4140
/**
4241
* @deprecated No longer used.
@@ -577,7 +576,7 @@ export class MatButtonToggle implements OnInit, AfterViewInit, OnDestroy {
577576
private _elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
578577
private _focusMonitor = inject(FocusMonitor);
579578
private _idGenerator = inject(_IdGenerator);
580-
private _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});
579+
private _animationDisabled = _animationsDisabled();
581580
private _checked = false;
582581

583582
/**
@@ -721,7 +720,7 @@ export class MatButtonToggle implements OnInit, AfterViewInit, OnDestroy {
721720
// 1. We don't want the animation to fire on the first render for pre-checked toggles so we
722721
// delay adding the class until the view is rendered.
723722
// 2. We don't want animation if the `NoopAnimationsModule` is provided.
724-
if (this._animationMode !== 'NoopAnimations') {
723+
if (!this._animationDisabled) {
725724
this._elementRef.nativeElement.classList.add('mat-button-toggle-animations-enabled');
726725
}
727726

src/material/button/button-base.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
1010
import {
1111
AfterViewInit,
12-
ANIMATION_MODULE_TYPE,
1312
booleanAttribute,
1413
Directive,
1514
ElementRef,
@@ -21,7 +20,7 @@ import {
2120
OnDestroy,
2221
Renderer2,
2322
} from '@angular/core';
24-
import {_StructuralStylesLoader, MatRippleLoader, ThemePalette} from '../core';
23+
import {_animationsDisabled, _StructuralStylesLoader, MatRippleLoader, ThemePalette} from '../core';
2524
import {_CdkPrivateStyleLoader} from '@angular/cdk/private';
2625

2726
/**
@@ -62,13 +61,13 @@ function transformTabIndex(value: unknown): number | undefined {
6261
'[class.mat-mdc-button-disabled]': 'disabled',
6362
'[class.mat-mdc-button-disabled-interactive]': 'disabledInteractive',
6463
'[class.mat-unthemed]': '!color',
65-
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
64+
'[class._mat-animation-noopable]': '_animationsDisabled',
6665
},
6766
})
6867
export class MatButtonBase implements AfterViewInit, OnDestroy {
6968
_elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
7069
protected _ngZone = inject(NgZone);
71-
protected _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});
70+
protected _animationsDisabled = _animationsDisabled();
7271

7372
protected readonly _config = inject(MAT_BUTTON_CONFIG, {optional: true});
7473
private readonly _focusMonitor = inject(FocusMonitor);

src/material/checkbox/checkbox.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import {_IdGenerator, FocusableOption} from '@angular/cdk/a11y';
1010
import {
11-
ANIMATION_MODULE_TYPE,
1211
AfterViewInit,
1312
ChangeDetectionStrategy,
1413
ChangeDetectorRef,
@@ -36,7 +35,12 @@ import {
3635
ValidationErrors,
3736
Validator,
3837
} from '@angular/forms';
39-
import {MatRipple, _MatInternalFormField, _StructuralStylesLoader} from '../core';
38+
import {
39+
MatRipple,
40+
_MatInternalFormField,
41+
_StructuralStylesLoader,
42+
_animationsDisabled,
43+
} from '../core';
4044
import {
4145
MAT_CHECKBOX_DEFAULT_OPTIONS,
4246
MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY,
@@ -79,7 +83,7 @@ const defaults = MAT_CHECKBOX_DEFAULT_OPTIONS_FACTORY();
7983
'[attr.tabindex]': 'null',
8084
'[attr.aria-label]': 'null',
8185
'[attr.aria-labelledby]': 'null',
82-
'[class._mat-animation-noopable]': `_animationMode === 'NoopAnimations'`,
86+
'[class._mat-animation-noopable]': '_animationsDisabled',
8387
'[class.mdc-checkbox--disabled]': 'disabled',
8488
'[id]': 'id',
8589
// Add classes that users can use to more easily target disabled or checked checkboxes.
@@ -111,7 +115,7 @@ export class MatCheckbox
111115
_elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
112116
private _changeDetectorRef = inject(ChangeDetectorRef);
113117
private _ngZone = inject(NgZone);
114-
_animationMode? = inject(ANIMATION_MODULE_TYPE, {optional: true});
118+
private _animationsDisabled = _animationsDisabled();
115119
private _options = inject<MatCheckboxDefaultOptions>(MAT_CHECKBOX_DEFAULT_OPTIONS, {
116120
optional: true,
117121
});
@@ -464,7 +468,7 @@ export class MatCheckbox
464468
newState: TransitionCheckState,
465469
): string {
466470
// Don't transition if animations are disabled.
467-
if (this._animationMode === 'NoopAnimations') {
471+
if (this._animationsDisabled) {
468472
return '';
469473
}
470474

src/material/chips/chip.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {BACKSPACE, DELETE} from '@angular/cdk/keycodes';
1111
import {_CdkPrivateStyleLoader, _VisuallyHiddenLoader} from '@angular/cdk/private';
1212
import {DOCUMENT} from '@angular/common';
1313
import {
14-
ANIMATION_MODULE_TYPE,
1514
AfterContentInit,
1615
AfterViewInit,
1716
ChangeDetectionStrategy,
@@ -39,6 +38,7 @@ import {
3938
MatRippleLoader,
4039
RippleGlobalOptions,
4140
_StructuralStylesLoader,
41+
_animationsDisabled,
4242
} from '../core';
4343
import {Subject, Subscription, merge} from 'rxjs';
4444
import {MatChipAction} from './chip-action';
@@ -120,7 +120,7 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
120120
private _actionChanges: Subscription | undefined;
121121

122122
/** Whether animations for the chip are enabled. */
123-
_animationsDisabled: boolean;
123+
_animationsDisabled = _animationsDisabled();
124124

125125
/** All avatars present in the chip. */
126126
@ContentChildren(MAT_CHIP_AVATAR, {descendants: true})
@@ -245,8 +245,6 @@ export class MatChip implements OnInit, AfterViewInit, AfterContentInit, DoCheck
245245
const styleLoader = inject(_CdkPrivateStyleLoader);
246246
styleLoader.load(_StructuralStylesLoader);
247247
styleLoader.load(_VisuallyHiddenLoader);
248-
const animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true});
249-
this._animationsDisabled = animationMode === 'NoopAnimations';
250248
this._monitorFocus();
251249

252250
this._rippleLoader?.configureRipple(this._elementRef.nativeElement, {

0 commit comments

Comments
 (0)