Skip to content

Commit d8b726d

Browse files
timsawyerjelbourn
authored andcommitted
fix(material/slider): inject document token (#18275)
Update Angular Material `slider` component to inject reference to document instead of accessing global variable. This allows for rendering the slider in contexts where the document is dynamically provided. This change follows the pattern used in Cdk OverlayContainer. (cherry picked from commit 3bae800)
1 parent 25ace54 commit d8b726d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/material/slider/slider.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
} from '@angular/material/core';
5858
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
5959
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
60+
import {DOCUMENT} from '@angular/common';
6061
import {Subscription} from 'rxjs';
6162

6263
const activeEventOptions = normalizePassiveListenerOptions({passive: false});
@@ -483,6 +484,9 @@ export class MatSlider extends _MatSliderMixinBase
483484
/** Keeps track of the last pointer event that was captured by the slider. */
484485
private _lastPointerEvent: MouseEvent | TouchEvent | null;
485486

487+
/** Used to subscribe to global move and end events */
488+
protected _document?: Document;
489+
486490
constructor(elementRef: ElementRef,
487491
private _focusMonitor: FocusMonitor,
488492
private _changeDetectorRef: ChangeDetectorRef,
@@ -491,9 +495,13 @@ export class MatSlider extends _MatSliderMixinBase
491495
// @breaking-change 8.0.0 `_animationMode` parameter to be made required.
492496
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
493497
// @breaking-change 9.0.0 `_ngZone` parameter to be made required.
494-
private _ngZone?: NgZone) {
498+
private _ngZone?: NgZone,
499+
/** @breaking-change 11.0.0 make document required */
500+
@Optional() @Inject(DOCUMENT) document?: any) {
495501
super(elementRef);
496502

503+
this._document = document;
504+
497505
this.tabIndex = parseInt(tabIndex) || 0;
498506

499507
this._runOutsizeZone(() => {
@@ -691,8 +699,8 @@ export class MatSlider extends _MatSliderMixinBase
691699
* as they're swiping across the screen.
692700
*/
693701
private _bindGlobalEvents(triggerEvent: TouchEvent | MouseEvent) {
694-
if (typeof document !== 'undefined' && document) {
695-
const body = document.body;
702+
if (typeof this._document !== 'undefined' && this._document) {
703+
const body = this._document.body;
696704
const isTouch = isTouchEvent(triggerEvent);
697705
const moveEventName = isTouch ? 'touchmove' : 'mousemove';
698706
const endEventName = isTouch ? 'touchend' : 'mouseup';
@@ -710,8 +718,8 @@ export class MatSlider extends _MatSliderMixinBase
710718

711719
/** Removes any global event listeners that we may have added. */
712720
private _removeGlobalEvents() {
713-
if (typeof document !== 'undefined' && document) {
714-
const body = document.body;
721+
if (typeof this._document !== 'undefined' && this._document) {
722+
const body = this._document.body;
715723
body.removeEventListener('mousemove', this._pointerMove, activeEventOptions);
716724
body.removeEventListener('mouseup', this._pointerUp, activeEventOptions);
717725
body.removeEventListener('touchmove', this._pointerMove, activeEventOptions);

tools/public_api_guard/material/slider.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +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;
56
get _invertAxis(): boolean;
67
_isActive: boolean;
78
get _isMinValue(): boolean;
@@ -45,7 +46,8 @@ export declare class MatSlider extends _MatSliderMixinBase implements ControlVal
4546
readonly valueChange: EventEmitter<number | null>;
4647
get vertical(): boolean;
4748
set vertical(value: boolean);
48-
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, _dir: Directionality, tabIndex: string, _animationMode?: string | undefined, _ngZone?: NgZone | undefined);
49+
constructor(elementRef: ElementRef, _focusMonitor: FocusMonitor, _changeDetectorRef: ChangeDetectorRef, _dir: Directionality, tabIndex: string, _animationMode?: string | undefined, _ngZone?: NgZone | undefined,
50+
document?: any);
4951
_onBlur(): void;
5052
_onFocus(): void;
5153
_onKeydown(event: KeyboardEvent): void;

0 commit comments

Comments
 (0)