Skip to content

Commit 2d1911d

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 0da7b68 commit 2d1911d

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
@@ -214,16 +214,24 @@ export class MdSlideToggle implements AfterContentInit, ControlValueAccessor {
214214

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

220222
/** TODO: internal */
221223
_onDrag(event: HammerInput) {
222-
this._slideRenderer.updateThumbPosition(event.deltaX);
224+
if (this._slideRenderer.isDragging()) {
225+
this._slideRenderer.updateThumbPosition(event.deltaX);
226+
}
223227
}
224228

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

258266
/** Initializes the drag of the slide-toggle. */
259267
startThumbDrag(checked: boolean) {
260-
if (!this._thumbBarWidth) {
268+
if (!this.isDragging()) {
261269
this._thumbBarWidth = this._thumbBarEl.clientWidth - this._thumbEl.clientWidth;
262270
this._checked = checked;
263271
this._thumbEl.classList.add('md-dragging');
@@ -266,7 +274,7 @@ class SlideToggleRenderer {
266274

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

@@ -278,10 +286,8 @@ class SlideToggleRenderer {
278286

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

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

0 commit comments

Comments
 (0)