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';

0 commit comments

Comments
 (0)