Skip to content

fix(material-experimental/mdc-slider): use passive event listeners #24675

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 29, 2022
Merged
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
29 changes: 24 additions & 5 deletions src/material-experimental/mdc-slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coerceNumberProperty,
NumberInput,
} from '@angular/cdk/coercion';
import {Platform} from '@angular/cdk/platform';
import {Platform, normalizePassiveListenerOptions} from '@angular/cdk/platform';
import {DOCUMENT} from '@angular/common';
import {
AfterViewInit,
Expand Down Expand Up @@ -55,6 +55,9 @@ import {MDCSliderAdapter, MDCSliderFoundation, Thumb, TickMark} from '@material/
import {Subscription} from 'rxjs';
import {GlobalChangeAndInputListener} from './global-change-and-input-listener';

/** Options used to bind passive event listeners. */
const passiveEventListenerOptions = normalizePassiveListenerOptions({passive: true});

/** Represents a drag event emitted by the MatSlider component. */
export interface MatSliderDragEvent {
/** The MatSliderThumb that was interacted with. */
Expand Down Expand Up @@ -778,20 +781,36 @@ export class MatSlider
// would prefer to use "mousedown" as the default, for some reason it does not work (the
// callback is never triggered).
if (this._SUPPORTS_POINTER_EVENTS) {
this._elementRef.nativeElement.addEventListener('pointerdown', this._layout);
this._elementRef.nativeElement.addEventListener(
'pointerdown',
this._layout,
passiveEventListenerOptions,
);
} else {
this._elementRef.nativeElement.addEventListener('mouseenter', this._layout);
this._elementRef.nativeElement.addEventListener('touchstart', this._layout);
this._elementRef.nativeElement.addEventListener(
'touchstart',
this._layout,
passiveEventListenerOptions,
);
}
}

/** Removes the event listener that keeps sync the slider UI and the foundation in sync. */
_removeUISyncEventListener(): void {
if (this._SUPPORTS_POINTER_EVENTS) {
this._elementRef.nativeElement.removeEventListener('pointerdown', this._layout);
this._elementRef.nativeElement.removeEventListener(
'pointerdown',
this._layout,
passiveEventListenerOptions,
);
} else {
this._elementRef.nativeElement.removeEventListener('mouseenter', this._layout);
this._elementRef.nativeElement.removeEventListener('touchstart', this._layout);
this._elementRef.nativeElement.removeEventListener(
'touchstart',
this._layout,
passiveEventListenerOptions,
);
}
}

Expand Down