Skip to content

Commit 62be7b3

Browse files
authored
fix(material/slide-toggle): disable ripples when animations are disabled (#22713)
Fixes that the ripples of the slide toggle weren't being disabled when all animations are disabled.
1 parent e7a64d3 commit 62be7b3

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

src/material-experimental/mdc-slide-toggle/slide-toggle.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ const RIPPLE_ANIMATION_CONFIG: RippleAnimationConfig = {
4949
exitDuration: numbers.FG_DEACTIVATION_MS,
5050
};
5151

52+
/** Configuration for ripples when animations are disabled. */
53+
const NOOP_RIPPLE_ANIMATION_CONFIG: RippleAnimationConfig = {
54+
enterDuration: 0,
55+
exitDuration: 0
56+
};
57+
5258
/** @docs-private */
5359
export const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR: any = {
5460
provide: NG_VALUE_ACCESSOR,
@@ -81,7 +87,7 @@ export class MatSlideToggleChange {
8187
'[class.mat-warn]': 'color === "warn"',
8288
'[class.mat-mdc-slide-toggle-focused]': '_focused',
8389
'[class.mat-mdc-slide-toggle-checked]': 'checked',
84-
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
90+
'[class._mat-animation-noopable]': '_noopAnimations',
8591
},
8692
exportAs: 'matSlideToggle',
8793
encapsulation: ViewEncapsulation.None,
@@ -111,7 +117,10 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
111117
_focused: boolean;
112118

113119
/** Configuration for the underlying ripple. */
114-
_rippleAnimation: RippleAnimationConfig = RIPPLE_ANIMATION_CONFIG;
120+
_rippleAnimation: RippleAnimationConfig;
121+
122+
/** Whether noop animations are enabled. */
123+
_noopAnimations: boolean;
115124

116125
/** The color palette for this slide toggle. */
117126
@Input() color: ThemePalette;
@@ -201,9 +210,12 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
201210
@Attribute('tabindex') tabIndex: string,
202211
@Inject(MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS)
203212
public defaults: MatSlideToggleDefaultOptions,
204-
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
213+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
205214
this.tabIndex = parseInt(tabIndex) || 0;
206215
this.color = defaults.color || 'accent';
216+
this._noopAnimations = animationMode === 'NoopAnimations';
217+
this._rippleAnimation = this._noopAnimations ?
218+
NOOP_RIPPLE_ANIMATION_CONFIG : RIPPLE_ANIMATION_CONFIG;
207219
}
208220

209221
ngAfterViewInit() {

src/material/slide-toggle/slide-toggle.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
[matRippleDisabled]="disableRipple || disabled"
2424
[matRippleCentered]="true"
2525
[matRippleRadius]="20"
26-
[matRippleAnimation]="{enterDuration: 150}">
26+
[matRippleAnimation]="{enterDuration: _noopAnimations ? 0 : 150}">
2727

2828
<div class="mat-ripple-element mat-slide-toggle-persistent-ripple"></div>
2929
</div>

src/material/slide-toggle/slide-toggle.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const _MatSlideToggleMixinBase:
8888
'[class.mat-checked]': 'checked',
8989
'[class.mat-disabled]': 'disabled',
9090
'[class.mat-slide-toggle-label-before]': 'labelPosition == "before"',
91-
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
91+
'[class._mat-animation-noopable]': '_noopAnimations',
9292
},
9393
templateUrl: 'slide-toggle.html',
9494
styleUrls: ['slide-toggle.css'],
@@ -109,6 +109,9 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
109109
private _required: boolean = false;
110110
private _checked: boolean = false;
111111

112+
/** Whether noop animations are enabled. */
113+
_noopAnimations: boolean;
114+
112115
/** Reference to the thumb HTMLElement. */
113116
@ViewChild('thumbContainer') _thumbEl: ElementRef;
114117

@@ -165,10 +168,11 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
165168
@Attribute('tabindex') tabIndex: string,
166169
@Inject(MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS)
167170
public defaults: MatSlideToggleDefaultOptions,
168-
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
171+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string) {
169172
super(elementRef);
170173
this.tabIndex = parseInt(tabIndex) || 0;
171174
this.color = this.defaultColor = defaults.color || 'accent';
175+
this._noopAnimations = animationMode === 'NoopAnimations';
172176
}
173177

174178
ngAfterContentInit() {

tools/public_api_guard/material/slide-toggle.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export declare const MAT_SLIDE_TOGGLE_REQUIRED_VALIDATOR: Provider;
1111
export declare const MAT_SLIDE_TOGGLE_VALUE_ACCESSOR: any;
1212

1313
export declare class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestroy, AfterContentInit, ControlValueAccessor, CanDisable, CanColor, HasTabIndex, CanDisableRipple {
14-
_animationMode?: string | undefined;
1514
_inputElement: ElementRef<HTMLInputElement>;
15+
_noopAnimations: boolean;
1616
_thumbBarEl: ElementRef;
1717
_thumbEl: ElementRef;
1818
ariaLabel: string | null;
@@ -28,7 +28,7 @@ export declare class MatSlideToggle extends _MatSlideToggleMixinBase implements
2828
get required(): boolean;
2929
set required(value: boolean);
3030
readonly toggleChange: EventEmitter<void>;
31-
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, tabIndex: string, defaults: MatSlideToggleDefaultOptions, _animationMode?: string | undefined);
31+
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, tabIndex: string, defaults: MatSlideToggleDefaultOptions, animationMode?: string);
3232
_onChangeEvent(event: Event): void;
3333
_onInputClick(event: Event): void;
3434
_onLabelTextChange(): void;

0 commit comments

Comments
 (0)