Skip to content

Commit 1718d78

Browse files
authored
fix(slider): not stopping drag when released outside of viewport (#18905)
We're binding the slider events to the `document.body` which means that we won't pick up the ones that happen outside the viewport. These changes move the events to the `document`. Fixes #18888.
1 parent 4e02a95 commit 1718d78

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/material/slider/slider.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ function dispatchSlideEvent(sliderElement: HTMLElement, percent: number): void {
16631663
const dimensions = trackElement.getBoundingClientRect();
16641664
const x = dimensions.left + (dimensions.width * percent);
16651665
const y = dimensions.top + (dimensions.height * percent);
1666-
dispatchMouseEvent(document.body, 'mousemove', x, y);
1666+
dispatchMouseEvent(document, 'mousemove', x, y);
16671667
}
16681668

16691669
/**
@@ -1690,7 +1690,7 @@ function dispatchSlideEndEvent(sliderElement: HTMLElement, percent: number): voi
16901690
const dimensions = trackElement.getBoundingClientRect();
16911691
const x = dimensions.left + (dimensions.width * percent);
16921692
const y = dimensions.top + (dimensions.height * percent);
1693-
dispatchMouseEvent(document.body, 'mouseup', x, y);
1693+
dispatchMouseEvent(document, 'mouseup', x, y);
16941694
}
16951695

16961696
/**

src/material/slider/slider.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,16 +709,19 @@ export class MatSlider extends _MatSliderMixinBase
709709
* as they're swiping across the screen.
710710
*/
711711
private _bindGlobalEvents(triggerEvent: TouchEvent | MouseEvent) {
712-
if (typeof this._document !== 'undefined' && this._document) {
713-
const body = this._document.body;
712+
// Note that we bind the events to the `document`, because it allows us to capture
713+
// drag cancel events where the user's pointer is outside the browser window.
714+
const document = this._document;
715+
716+
if (typeof document !== 'undefined' && document) {
714717
const isTouch = isTouchEvent(triggerEvent);
715718
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
716719
const endEventName = isTouch ? 'touchend' : 'mouseup';
717-
body.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
718-
body.addEventListener(endEventName, this._pointerUp, activeEventOptions);
720+
document.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
721+
document.addEventListener(endEventName, this._pointerUp, activeEventOptions);
719722

720723
if (isTouch) {
721-
body.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
724+
document.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
722725
}
723726
}
724727

@@ -731,13 +734,14 @@ export class MatSlider extends _MatSliderMixinBase
731734

732735
/** Removes any global event listeners that we may have added. */
733736
private _removeGlobalEvents() {
734-
if (typeof this._document !== 'undefined' && this._document) {
735-
const body = this._document.body;
736-
body.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
737-
body.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
738-
body.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
739-
body.removeEventListener('touchend', this._pointerUp, activeEventOptions);
740-
body.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
737+
const document = this._document;
738+
739+
if (typeof document !== 'undefined' && document) {
740+
document.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
741+
document.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
742+
document.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
743+
document.removeEventListener('touchend', this._pointerUp, activeEventOptions);
744+
document.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
741745
}
742746

743747
const window = this._getWindow();

0 commit comments

Comments
 (0)