Skip to content

Commit 99e66b8

Browse files
devversionjelbourn
authored andcommitted
fix(material-experimental): mdc-slider should not throw when rendering on the server (#16993)
Follow-up to the initial mdc-slider prototype implementation. This commit adds the mdc-slider to the universal-app test and also fixes issues that prevented the slider to be rendered on the server.
1 parent a1e5863 commit 99e66b8

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

src/material-experimental/mdc-slider/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ ng_module(
1313
assets = [":slider_scss"] + glob(["**/*.html"]),
1414
module_name = "@angular/material-experimental/mdc-slider",
1515
deps = [
16+
"//src/cdk/bidi",
17+
"//src/cdk/coercion",
18+
"//src/cdk/platform",
1619
"//src/material/core",
1720
"@npm//@angular/forms",
1821
"@npm//@material/slider",

src/material-experimental/mdc-slider/slider.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Directionality} from '@angular/cdk/bidi';
1010
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
11-
import {normalizePassiveListenerOptions} from '@angular/cdk/platform';
11+
import {normalizePassiveListenerOptions, Platform} from '@angular/cdk/platform';
1212
import {
1313
AfterViewInit,
1414
Attribute,
@@ -322,8 +322,11 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
322322
@ViewChild('trackMarker', {static: false}) _trackMarker: ElementRef<HTMLElement>;
323323

324324
constructor(
325-
private _elementRef: ElementRef<HTMLElement>, private _changeDetectorRef: ChangeDetectorRef,
326-
private _ngZone: NgZone, @Optional() private _dir: Directionality,
325+
private _elementRef: ElementRef<HTMLElement>,
326+
private _changeDetectorRef: ChangeDetectorRef,
327+
private _ngZone: NgZone,
328+
private _platform: Platform,
329+
@Optional() private _dir: Directionality,
327330
@Attribute('tabindex') tabIndex: string,
328331
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
329332
this.tabIndex = parseInt(tabIndex) || 0;
@@ -342,19 +345,28 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
342345

343346
ngAfterViewInit() {
344347
this._isInitialized = true;
345-
this._foundation.init();
346348

347-
// The standard Angular Material slider is always using discrete values. We always
348-
// want to enable discrete values and support ticks, but want to still provide
349-
// non-discrete slider visual looks if thumb label is disabled.
350-
// TODO(devversion): check if we can get a public API for this.
351-
// Tracked with: https://github.com/material-components/material-components-web/issues/5020
352-
(this._foundation as any).isDiscrete_ = true;
349+
if (this._platform.isBrowser) {
350+
// The MDC slider foundation accesses DOM globals, so we cannot initialize the
351+
// foundation on the server. The foundation would be needed to move the thumb
352+
// to the proper position and to render the ticks.
353+
this._foundation.init();
354+
355+
// The standard Angular Material slider is always using discrete values. We always
356+
// want to enable discrete values and support ticks, but want to still provide
357+
// non-discrete slider visual looks if thumb label is disabled.
358+
// TODO(devversion): check if we can get a public API for this.
359+
// Tracked with: https://github.com/material-components/material-components-web/issues/5020
360+
(this._foundation as any).isDiscrete_ = true;
361+
362+
// These bindings cannot be synced in the foundation, as the foundation is not
363+
// initialized and they cause DOM globals to be accessed (to move the thumb)
364+
this._syncStep();
365+
this._syncValue();
366+
this._syncMax();
367+
this._syncMin();
368+
}
353369

354-
this._syncStep();
355-
this._syncValue();
356-
this._syncMax();
357-
this._syncMin();
358370
this._syncDisabled();
359371
}
360372

@@ -385,7 +397,11 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
385397

386398
ngOnDestroy() {
387399
this._dirChangeSubscription.unsubscribe();
388-
this._foundation.destroy();
400+
// The foundation cannot be destroyed on the server, as the foundation
401+
// has not be initialized on the server.
402+
if (this._platform.isBrowser) {
403+
this._foundation.destroy();
404+
}
389405
}
390406

391407
/** Focuses the slider. */

src/universal-app/kitchen-sink-mdc/kitchen-sink-mdc.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,9 @@ <h2>MDC slide-toggle</h2>
4949
<mat-slide-toggle checked></mat-slide-toggle>
5050
<mat-slide-toggle checked disabled></mat-slide-toggle>
5151
<mat-slide-toggle>with a label</mat-slide-toggle>
52+
53+
<h2>Slider</h2>
54+
<mat-slider></mat-slider>
55+
<mat-slider value="50"></mat-slider>
56+
<mat-slider tickInterval="1" min="1" max="10" value="5" thumbLabel></mat-slider>
57+
<mat-slider disabled></mat-slider>

src/universal-app/kitchen-sink-mdc/kitchen-sink-mdc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {MatChipsModule} from '@angular/material-experimental/mdc-chips';
66
import {MatMenuModule} from '@angular/material-experimental/mdc-menu';
77
import {MatRadioModule} from '@angular/material-experimental/mdc-radio';
88
import {MatSlideToggleModule} from '@angular/material-experimental/mdc-slide-toggle';
9+
import {MatSliderModule} from '@angular/material-experimental/mdc-slider';
910
import {MatTabsModule} from '@angular/material-experimental/mdc-tabs';
1011
import {MatIconModule} from '@angular/material/icon';
1112

@@ -26,6 +27,7 @@ export class KitchenSinkMdc {
2627
MatMenuModule,
2728
MatRadioModule,
2829
MatSlideToggleModule,
30+
MatSliderModule,
2931
MatTabsModule,
3032
],
3133
declarations: [KitchenSinkMdc],

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ <h2>Slider</h2>
208208
<mat-slider></mat-slider>
209209
<mat-slider value="50"></mat-slider>
210210
<mat-slider tickInterval="1" min="1" max="10" value="5" thumbLabel></mat-slider>
211+
<mat-slider disabled></mat-slider>
211212

212213
<h2>Tabs</h2>
213214
<mat-tab-group>

0 commit comments

Comments
 (0)