Skip to content

Commit df795a8

Browse files
authored
refactor(slider): change deprecated APIs for version 10 (#19379)
Changes the APIs that were marked as deprecated for v10 in the slider. BREAKING CHANGES: * The `_ngZone` and `_document` parameters in the `MatSlider` constructor are now required.
1 parent d309ee2 commit df795a8

File tree

3 files changed

+28
-48
lines changed

3 files changed

+28
-48
lines changed

src/material/schematics/ng-update/data/constructor-checks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const constructorChecks: VersionChanges<ConstructorChecksUpgradeData> = {
1818
{
1919
pr: 'https://github.com/angular/components/pull/19307',
2020
changes: ['MatSlideToggle']
21+
},
22+
{
23+
pr: 'https://github.com/angular/components/pull/19379',
24+
changes: ['MatSlider']
2125
}
2226
],
2327
[TargetVersion.V9]: [

src/material/slider/slider.ts

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -490,26 +490,21 @@ export class MatSlider extends _MatSliderMixinBase
490490
private _lastPointerEvent: MouseEvent | TouchEvent | null;
491491

492492
/** Used to subscribe to global move and end events */
493-
protected _document?: Document;
493+
protected _document: Document;
494494

495495
constructor(elementRef: ElementRef,
496496
private _focusMonitor: FocusMonitor,
497497
private _changeDetectorRef: ChangeDetectorRef,
498498
@Optional() private _dir: Directionality,
499499
@Attribute('tabindex') tabIndex: string,
500-
// @breaking-change 8.0.0 `_animationMode` parameter to be made required.
501-
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
502-
// @breaking-change 9.0.0 `_ngZone` parameter to be made required.
503-
private _ngZone?: NgZone,
504-
/** @breaking-change 11.0.0 make document required */
505-
@Optional() @Inject(DOCUMENT) document?: any) {
500+
private _ngZone: NgZone,
501+
@Inject(DOCUMENT) _document: any,
502+
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
506503
super(elementRef);
507-
508-
this._document = document;
509-
504+
this._document = _document;
510505
this.tabIndex = parseInt(tabIndex) || 0;
511506

512-
this._runOutsizeZone(() => {
507+
_ngZone.runOutsideAngular(() => {
513508
const element = elementRef.nativeElement;
514509
element.addEventListener('mousedown', this._pointerDown, activeEventOptions);
515510
element.addEventListener('touchstart', this._pointerDown, activeEventOptions);
@@ -629,7 +624,7 @@ export class MatSlider extends _MatSliderMixinBase
629624
return;
630625
}
631626

632-
this._runInsideZone(() => {
627+
this._ngZone.run(() => {
633628
const oldValue = this.value;
634629
const pointerPosition = getPointerPositionOnPage(event);
635630
this._isSliding = true;
@@ -700,7 +695,7 @@ export class MatSlider extends _MatSliderMixinBase
700695

701696
/** Use defaultView of injected document if available or fallback to global window reference */
702697
private _getWindow(): Window {
703-
return this._document?.defaultView || window;
698+
return this._document.defaultView || window;
704699
}
705700

706701
/**
@@ -712,17 +707,14 @@ export class MatSlider extends _MatSliderMixinBase
712707
// Note that we bind the events to the `document`, because it allows us to capture
713708
// drag cancel events where the user's pointer is outside the browser window.
714709
const document = this._document;
715-
716-
if (typeof document !== 'undefined' && document) {
717-
const isTouch = isTouchEvent(triggerEvent);
718-
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
719-
const endEventName = isTouch ? 'touchend' : 'mouseup';
720-
document.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
721-
document.addEventListener(endEventName, this._pointerUp, activeEventOptions);
722-
723-
if (isTouch) {
724-
document.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
725-
}
710+
const isTouch = isTouchEvent(triggerEvent);
711+
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
712+
const endEventName = isTouch ? 'touchend' : 'mouseup';
713+
document.addEventListener(moveEventName, this._pointerMove, activeEventOptions);
714+
document.addEventListener(endEventName, this._pointerUp, activeEventOptions);
715+
716+
if (isTouch) {
717+
document.addEventListener('touchcancel', this._pointerUp, activeEventOptions);
726718
}
727719

728720
const window = this._getWindow();
@@ -735,14 +727,11 @@ export class MatSlider extends _MatSliderMixinBase
735727
/** Removes any global event listeners that we may have added. */
736728
private _removeGlobalEvents() {
737729
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);
745-
}
730+
document.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
731+
document.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
732+
document.removeEventListener('touchmove', this._pointerMove, activeEventOptions);
733+
document.removeEventListener('touchend', this._pointerUp, activeEventOptions);
734+
document.removeEventListener('touchcancel', this._pointerUp, activeEventOptions);
746735

747736
const window = this._getWindow();
748737

@@ -869,18 +858,6 @@ export class MatSlider extends _MatSliderMixinBase
869858
this._elementRef.nativeElement.blur();
870859
}
871860

872-
/** Runs a callback inside of the NgZone, if possible. */
873-
private _runInsideZone(fn: () => any) {
874-
// @breaking-change 9.0.0 Remove this function once `_ngZone` is a required parameter.
875-
this._ngZone ? this._ngZone.run(fn) : fn();
876-
}
877-
878-
/** Runs a callback outside of the NgZone, if possible. */
879-
private _runOutsizeZone(fn: () => any) {
880-
// @breaking-change 9.0.0 Remove this function once `_ngZone` is a required parameter.
881-
this._ngZone ? this._ngZone.runOutsideAngular(fn) : fn();
882-
}
883-
884861
/**
885862
* Sets the model value. Implemented as part of ControlValueAccessor.
886863
* @param value

tools/public_api_guard/material/slider.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export declare const MAT_SLIDER_VALUE_ACCESSOR: any;
22

33
export declare class MatSlider extends _MatSliderMixinBase implements ControlValueAccessor, OnDestroy, CanDisable, CanColor, OnInit, HasTabIndex {
44
_animationMode?: string | undefined;
5-
protected _document?: Document;
5+
protected _document: Document;
66
get _invertAxis(): boolean;
77
_isActive: boolean;
88
get _isMinValue(): boolean;
@@ -46,8 +46,7 @@ export declare class MatSlider extends _MatSliderMixinBase implements ControlVal
4646
readonly valueChange: EventEmitter<number | null>;
4747
get vertical(): boolean;
4848
set vertical(value: boolean);
49-
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, _dir: Directionality, tabIndex: string, _animationMode?: string | undefined, _ngZone?: NgZone | undefined,
50-
document?: any);
49+
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, _dir: Directionality, tabIndex: string, _ngZone: NgZone, _document: any, _animationMode?: string | undefined);
5150
_onBlur(): void;
5251
_onFocus(): void;
5352
_onKeydown(event: KeyboardEvent): void;
@@ -72,7 +71,7 @@ export declare class MatSlider extends _MatSliderMixinBase implements ControlVal
7271
static ngAcceptInputType_value: NumberInput;
7372
static ngAcceptInputType_vertical: BooleanInput;
7473
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatSlider, "mat-slider", ["matSlider"], { "disabled": "disabled"; "color": "color"; "tabIndex": "tabIndex"; "invert": "invert"; "max": "max"; "min": "min"; "step": "step"; "thumbLabel": "thumbLabel"; "tickInterval": "tickInterval"; "value": "value"; "displayWith": "displayWith"; "vertical": "vertical"; }, { "change": "change"; "input": "input"; "valueChange": "valueChange"; }, never, never>;
75-
static ɵfac: i0.ɵɵFactoryDef<MatSlider, [null, null, null, { optional: true; }, { attribute: "tabindex"; }, { optional: true; }, null, { optional: true; }]>;
74+
static ɵfac: i0.ɵɵFactoryDef<MatSlider, [null, null, null, { optional: true; }, { attribute: "tabindex"; }, null, null, { optional: true; }]>;
7675
}
7776

7877
export declare class MatSliderChange {

0 commit comments

Comments
 (0)