Skip to content

Commit 320f387

Browse files
crisbetommalerba
authored andcommitted
fix(slider): handle touchcancel event (#17520)
Stops sliding on `touchcancel` events in the `mat-slider`, in addition to `touchend`.
1 parent 52918ea commit 320f387

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Diff for: src/material/slider/slider.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -672,21 +672,28 @@ export class MatSlider extends _MatSliderMixinBase
672672
*/
673673
private _bindGlobalEvents(triggerEvent: TouchEvent | MouseEvent) {
674674
if (typeof document !== 'undefined' && document) {
675+
const body = document.body;
675676
const isTouch = isTouchEvent(triggerEvent);
676677
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
677678
const endEventName = isTouch ? 'touchend' : 'mouseup';
678-
document.body.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
679-
document.body.addEventListener(endEventName, this._pointerUp, activeEventOptions);
679+
body.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
680+
body.addEventListener(endEventName, this._pointerUp, activeEventOptions);
681+
682+
if (isTouch) {
683+
body.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
684+
}
680685
}
681686
}
682687

683688
/** Removes any global event listeners that we may have added. */
684689
private _removeGlobalEvents() {
685690
if (typeof document !== 'undefined' && document) {
686-
document.body.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
687-
document.body.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
688-
document.body.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
689-
document.body.removeEventListener('touchend', this._pointerUp, activeEventOptions);
691+
const body = document.body;
692+
body.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
693+
body.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
694+
body.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
695+
body.removeEventListener('touchend', this._pointerUp, activeEventOptions);
696+
body.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
690697
}
691698
}
692699

0 commit comments

Comments
 (0)