Skip to content

fix(slider): not stopping drag when released outside of viewport #18905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ function dispatchSlideEvent(sliderElement: HTMLElement, percent: number): void {
const dimensions = trackElement.getBoundingClientRect();
const x = dimensions.left + (dimensions.width * percent);
const y = dimensions.top + (dimensions.height * percent);
dispatchMouseEvent(document.body, 'mousemove', x, y);
dispatchMouseEvent(document, 'mousemove', x, y);
}

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

/**
Expand Down
28 changes: 16 additions & 12 deletions src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,16 +709,19 @@ export class MatSlider extends _MatSliderMixinBase
* as they're swiping across the screen.
*/
private _bindGlobalEvents(triggerEvent: TouchEvent | MouseEvent) {
if (typeof this._document !== 'undefined' && this._document) {
const body = this._document.body;
// Note that we bind the events to the `document`, because it allows us to capture
// drag cancel events where the user's pointer is outside the browser window.
const document = this._document;

if (typeof document !== 'undefined' && document) {
const isTouch = isTouchEvent(triggerEvent);
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
const endEventName = isTouch ? 'touchend' : 'mouseup';
body.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
body.addEventListener(endEventName, this._pointerUp, activeEventOptions);
document.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
document.addEventListener(endEventName, this._pointerUp, activeEventOptions);

if (isTouch) {
body.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
document.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
}
}

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

/** Removes any global event listeners that we may have added. */
private _removeGlobalEvents() {
if (typeof this._document !== 'undefined' && this._document) {
const body = this._document.body;
body.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
body.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
body.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
body.removeEventListener('touchend', this._pointerUp, activeEventOptions);
body.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
const document = this._document;

if (typeof document !== 'undefined' && document) {
document.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
document.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
document.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
document.removeEventListener('touchend', this._pointerUp, activeEventOptions);
document.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
}

const window = this._getWindow();
Expand Down