Skip to content

Commit 5f2e077

Browse files
crisbetommalerba
authored andcommitted
feat(slide-toggle): align with 2018 material design spec (#12419)
Aligns the slide toggle component with the latest Material design spec. The component was mostly on spec already, but these changes adjust the size of the ripple and add the new behavior/opacity when hovering and on focus.
1 parent 4cfdb20 commit 5f2e077

File tree

5 files changed

+53
-66
lines changed

5 files changed

+53
-66
lines changed

src/lib/slide-toggle/_slide-toggle-theme.scss

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@
4545

4646
// Ripple colors are based on the current palette and the state of the slide-toggle.
4747
// See https://material.google.com/components/selection-controls.html#selection-controls-switch
48-
$ripple-checked-opacity: 0.12;
49-
$ripple-unchecked-color: mat-color($foreground, base, if($is-dark, 0.12, 0.06));
50-
$ripple-primary-color: mat-color($primary, $thumb-checked-hue, $ripple-checked-opacity);
51-
$ripple-accent-color: mat-color($accent, $thumb-checked-hue, $ripple-checked-opacity);
52-
$ripple-warn-color: mat-color($warn, $thumb-checked-hue, $ripple-checked-opacity);
48+
$ripple-unchecked-color: mat-color($foreground, base);
49+
$ripple-primary-color: mat-color($primary, $thumb-checked-hue);
50+
$ripple-accent-color: mat-color($accent, $thumb-checked-hue);
51+
$ripple-warn-color: mat-color($warn, $thumb-checked-hue);
5352

5453
.mat-slide-toggle {
5554
@include _mat-slide-toggle-checked($accent, $thumb-checked-hue);

src/lib/slide-toggle/slide-toggle.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
[matRippleTrigger]="label"
2828
[matRippleDisabled]="disableRipple || disabled"
2929
[matRippleCentered]="true"
30-
[matRippleRadius]="23"
30+
[matRippleRadius]="20"
3131
[matRippleAnimation]="{enterDuration: 150}">
32+
33+
<div class="mat-ripple-element mat-slide-toggle-persistent-ripple"></div>
3234
</div>
3335

3436
</div>

src/lib/slide-toggle/slide-toggle.scss

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $mat-slide-toggle-thumb-size: 20px !default;
99
$mat-slide-toggle-bar-border-radius: 8px !default;
1010
$mat-slide-toggle-height: 24px !default;
1111
$mat-slide-toggle-spacing: 8px !default;
12-
$mat-slide-toggle-ripple-radius: 23px !default;
12+
$mat-slide-toggle-ripple-radius: 20px !default;
1313
$mat-slide-toggle-bar-width: 36px !default;
1414
$mat-slide-toggle-bar-height: 14px !default;
1515
$mat-slide-toggle-bar-track-width: $mat-slide-toggle-bar-width - $mat-slide-toggle-thumb-size;
@@ -189,6 +189,31 @@ $mat-slide-toggle-bar-track-width: $mat-slide-toggle-bar-width - $mat-slide-togg
189189
width: $mat-slide-toggle-ripple-radius * 2;
190190
z-index: 1;
191191
pointer-events: none;
192+
193+
.mat-ripple-element:not(.mat-slide-toggle-persistent-ripple) {
194+
opacity: 0.16;
195+
}
196+
}
197+
198+
.mat-slide-toggle-persistent-ripple {
199+
width: 100%;
200+
height: 100%;
201+
transform: none;
202+
203+
.mat-slide-toggle-bar:hover & {
204+
opacity: 0.04;
205+
}
206+
207+
.mat-slide-toggle.cdk-focused & {
208+
opacity: 0.12;
209+
}
210+
211+
// We do this here, rather than having a `:not(.mat-slide-toggle-disabled)`
212+
// above in the `:hover`, because the `:not` will bump the specificity
213+
// a lot and will cause it to overide the focus styles.
214+
&, .mat-slide-toggle.mat-disabled .mat-slide-toggle-bar:hover & {
215+
opacity: 0;
216+
}
192217
}
193218

194219
/** Custom styling to make the slide-toggle usable in high contrast mode. */

src/lib/slide-toggle/slide-toggle.spec.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
tick,
1111
} from '@angular/core/testing';
1212
import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/forms';
13-
import {defaultRippleAnimationConfig} from '@angular/material/core';
1413
import {By, HAMMER_GESTURE_CONFIG} from '@angular/platform-browser';
1514
import {BidiModule, Direction} from '@angular/cdk/bidi';
1615
import {TestGestureConfig} from '../slider/test-gesture-config';
@@ -271,26 +270,6 @@ describe('MatSlideToggle without forms', () => {
271270
subscription.unsubscribe();
272271
}));
273272

274-
it('should show a ripple when focused by a keyboard action', fakeAsync(() => {
275-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
276-
.toBe(0, 'Expected no ripples to be present.');
277-
278-
dispatchFakeEvent(inputElement, 'keydown');
279-
dispatchFakeEvent(inputElement, 'focus');
280-
281-
tick(defaultRippleAnimationConfig.enterDuration);
282-
283-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
284-
.toBe(1, 'Expected the focus ripple to be showing up.');
285-
286-
dispatchFakeEvent(inputElement, 'blur');
287-
288-
tick(defaultRippleAnimationConfig.exitDuration);
289-
290-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
291-
.toBe(0, 'Expected focus ripple to be removed.');
292-
}));
293-
294273
it('should forward the required attribute', () => {
295274
testComponent.isRequired = true;
296275
fixture.detectChanges();
@@ -322,24 +301,27 @@ describe('MatSlideToggle without forms', () => {
322301
});
323302

324303
it('should show ripples on label mousedown', () => {
325-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
304+
const rippleSelector = '.mat-ripple-element:not(.mat-slide-toggle-persistent-ripple)';
305+
306+
expect(slideToggleElement.querySelectorAll(rippleSelector).length).toBe(0);
326307

327308
dispatchFakeEvent(labelElement, 'mousedown');
328309
dispatchFakeEvent(labelElement, 'mouseup');
329310

330-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(1);
311+
expect(slideToggleElement.querySelectorAll(rippleSelector).length).toBe(1);
331312
});
332313

333314
it('should not show ripples when disableRipple is set', () => {
315+
const rippleSelector = '.mat-ripple-element:not(.mat-slide-toggle-persistent-ripple)';
334316
testComponent.disableRipple = true;
335317
fixture.detectChanges();
336318

337-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
319+
expect(slideToggleElement.querySelectorAll(rippleSelector).length).toBe(0);
338320

339321
dispatchFakeEvent(labelElement, 'mousedown');
340322
dispatchFakeEvent(labelElement, 'mouseup');
341323

342-
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
324+
expect(slideToggleElement.querySelectorAll(rippleSelector).length).toBe(0);
343325
});
344326
});
345327

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y';
9+
import {FocusMonitor} from '@angular/cdk/a11y';
1010
import {Directionality} from '@angular/cdk/bidi';
1111
import {coerceBooleanProperty} from '@angular/cdk/coercion';
1212
import {Platform} from '@angular/cdk/platform';
@@ -35,12 +35,10 @@ import {
3535
CanDisableRipple,
3636
HammerInput,
3737
HasTabIndex,
38-
MatRipple,
3938
mixinColor,
4039
mixinDisabled,
4140
mixinDisableRipple,
4241
mixinTabIndex,
43-
RippleRef,
4442
} from '@angular/material/core';
4543
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
4644
import {
@@ -104,9 +102,6 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
104102
private _required: boolean = false;
105103
private _checked: boolean = false;
106104

107-
/** Reference to the focus state ripple. */
108-
private _focusRipple: RippleRef | null;
109-
110105
/** Whether the thumb is currently being dragged. */
111106
private _dragging = false;
112107

@@ -179,9 +174,6 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
179174
/** Reference to the underlying input element. */
180175
@ViewChild('input') _inputElement: ElementRef;
181176

182-
/** Reference to the ripple directive on the thumb container. */
183-
@ViewChild(MatRipple) _ripple: MatRipple;
184-
185177
constructor(elementRef: ElementRef,
186178
/**
187179
* @deprecated The `_platform` parameter to be removed.
@@ -202,12 +194,21 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
202194

203195
ngAfterContentInit() {
204196
this._focusMonitor
205-
.monitor(this._inputElement.nativeElement)
206-
.subscribe(focusOrigin => this._onInputFocusChange(focusOrigin));
197+
.monitor(this._elementRef.nativeElement, true)
198+
.subscribe(focusOrigin => {
199+
if (!focusOrigin) {
200+
// When a focused element becomes disabled, the browser *immediately* fires a blur event.
201+
// Angular does not expect events to be raised during change detection, so any state
202+
// change (such as a form control's 'ng-touched') will cause a changed-after-checked
203+
// error. See https://github.com/angular/angular/issues/17793. To work around this,
204+
// we defer telling the form control it has been touched until the next tick.
205+
Promise.resolve().then(() => this.onTouched());
206+
}
207+
});
207208
}
208209

209210
ngOnDestroy() {
210-
this._focusMonitor.stopMonitoring(this._inputElement.nativeElement);
211+
this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
211212
}
212213

213214
/** Method being called whenever the underlying input emits a change event. */
@@ -282,28 +283,6 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
282283
this.onChange(this.checked);
283284
}
284285

285-
/** Function is called whenever the focus changes for the input element. */
286-
private _onInputFocusChange(focusOrigin: FocusOrigin) {
287-
// TODO(paul): support `program`. See https://github.com/angular/material2/issues/9889
288-
if (!this._focusRipple && focusOrigin === 'keyboard') {
289-
// For keyboard focus show a persistent ripple as focus indicator.
290-
this._focusRipple = this._ripple.launch(0, 0, {persistent: true});
291-
} else if (!focusOrigin) {
292-
// When a focused element becomes disabled, the browser *immediately* fires a blur event.
293-
// Angular does not expect events to be raised during change detection, so any state change
294-
// (such as a form control's 'ng-touched') will cause a changed-after-checked error.
295-
// See https://github.com/angular/angular/issues/17793. To work around this, we defer telling
296-
// the form control it has been touched until the next tick.
297-
Promise.resolve().then(() => this.onTouched());
298-
299-
// Fade out and clear the focus ripple if one is currently present.
300-
if (this._focusRipple) {
301-
this._focusRipple.fadeOut();
302-
this._focusRipple = null;
303-
}
304-
}
305-
}
306-
307286
/**
308287
* Emits a change event on the `change` output. Also notifies the FormControl about the change.
309288
*/

0 commit comments

Comments
 (0)