Skip to content

Commit d8643ed

Browse files
committed
fix(slide-toggle): disabled theme and dragging
* It seems like as per the new theming feature, view encapsulation turned off and now the checked theming overwrites the disabled theme (too high specificity) * If a slide-toggle is disabled, users are still able to drag the thumb (which is invalid) * Fix invalid `user-select` property, and now dragging works without clamps.
1 parent bb61928 commit d8643ed

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44

55
@mixin _md-slide-toggle-checked($palette) {
6-
&.md-checked {
6+
// Do not apply the checked colors if the toggle is disabled, because the specificity would be to high for
7+
// the disabled styles.
8+
&.md-checked:not(.md-disabled) {
79
.md-slide-toggle-thumb {
810
background-color: md-color($palette);
911
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ $md-slide-toggle-margin: 16px !default;
2424
line-height: $md-slide-toggle-height;
2525

2626
white-space: nowrap;
27+
28+
// Disable user selection to ensure that dragging is smooth without grabbing some elements
29+
// accidentally. Manually prefixing here, because the un-prefixed property is not supported yet.
30+
-webkit-user-select: none;
31+
-moz-user-select: none;
32+
-ms-user-select: none;
2733
user-select: none;
2834

2935
outline: none;
@@ -68,7 +74,6 @@ $md-slide-toggle-margin: 16px !default;
6874
height: $md-slide-toggle-height;
6975

7076
position: relative;
71-
user-select: none;
7277

7378
margin-right: 8px;
7479
}

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,24 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
215215

216216
/** TODO: internal */
217217
_onDragStart() {
218-
this._slideRenderer.startThumbDrag(this.checked);
218+
if (!this.disabled) {
219+
this._slideRenderer.startThumbDrag(this.checked);
220+
}
219221
}
220222

221223
/** TODO: internal */
222224
_onDrag(event: HammerInput) {
223-
this._slideRenderer.updateThumbPosition(event.deltaX);
225+
if (this._slideRenderer.isDragging()) {
226+
this._slideRenderer.updateThumbPosition(event.deltaX);
227+
}
224228
}
225229

226230
/** TODO: internal */
227231
_onDragEnd() {
232+
if (!this._slideRenderer.isDragging()) {
233+
return;
234+
}
235+
228236
// Notice that we have to stop outside of the current event handler,
229237
// because otherwise the click event will be fired and will reset the new checked variable.
230238
setTimeout(() => {
@@ -258,7 +266,7 @@ class SlideToggleRenderer {
258266

259267
/** Initializes the drag of the slide-toggle. */
260268
startThumbDrag(checked: boolean) {
261-
if (!this._thumbBarWidth) {
269+
if (!this.isDragging()) {
262270
this._thumbBarWidth = this._thumbBarEl.clientWidth - this._thumbEl.clientWidth;
263271
this._checked = checked;
264272
this._thumbEl.classList.add('md-dragging');
@@ -267,7 +275,7 @@ class SlideToggleRenderer {
267275

268276
/** Stops the current drag and returns the new checked value. */
269277
stopThumbDrag(): boolean {
270-
if (this._thumbBarWidth) {
278+
if (this.isDragging()) {
271279
this._thumbBarWidth = null;
272280
this._thumbEl.classList.remove('md-dragging');
273281

@@ -279,10 +287,8 @@ class SlideToggleRenderer {
279287

280288
/** Updates the thumb containers position from the specified distance. */
281289
updateThumbPosition(distance: number) {
282-
if (this._thumbBarWidth) {
283-
this._percentage = this._getThumbPercentage(distance);
284-
applyCssTransform(this._thumbEl, `translate3d(${this._percentage}%, 0, 0)`);
285-
}
290+
this._percentage = this._getThumbPercentage(distance);
291+
applyCssTransform(this._thumbEl, `translate3d(${this._percentage}%, 0, 0)`);
286292
}
287293

288294
/** Retrieves the percentage of thumb from the moved distance. */

0 commit comments

Comments
 (0)