Skip to content

fix(material-experimental): mdc-slider should not throw when rendering on the server #16993

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
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
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-slider/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ng_module(
assets = [":slider_scss"] + glob(["**/*.html"]),
module_name = "@angular/material-experimental/mdc-slider",
deps = [
"//src/cdk/bidi",
"//src/cdk/coercion",
"//src/cdk/platform",
"//src/material/core",
"@npm//@angular/forms",
"@npm//@material/slider",
Expand Down
46 changes: 31 additions & 15 deletions src/material-experimental/mdc-slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

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

ngAfterViewInit() {
this._isInitialized = true;
this._foundation.init();

// The standard Angular Material slider is always using discrete values. We always
// want to enable discrete values and support ticks, but want to still provide
// non-discrete slider visual looks if thumb label is disabled.
// TODO(devversion): check if we can get a public API for this.
// Tracked with: https://github.com/material-components/material-components-web/issues/5020
(this._foundation as any).isDiscrete_ = true;
if (this._platform.isBrowser) {
// The MDC slider foundation accesses DOM globals, so we cannot initialize the
// foundation on the server. The foundation would be needed to move the thumb
// to the proper position and to render the ticks.
this._foundation.init();
Copy link
Member Author

@devversion devversion Sep 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmalerba @jelbourn Unfortunately there is an issue with MDC slider (and I guess it can be for other MDC foundations too). The slider thumb is moved in the MDC foundation through calculated pixels. This requires the foundation to perform element measurements using getBoundingClientRect.

In the server though, getBoundingClientRect is not available and therefore the foundation cannot move the thumb to the proper location. This is an issue for server-side rendering because the pre-rendered page will have sliders that do not reflect the initial value (until the page is hydrated)

Ideally, the slider foundation would not depend on the measurements of the slider element for moving the thumb. It should only depend on the measurements for determining the value from the click position. should I create an issue on their repo?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I would create an issue on their repo. They've said that they want all of their components to be SSR compatible, so this sounds like a bug to me

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: here is the issue: material-components/material-components-web#5058


// The standard Angular Material slider is always using discrete values. We always
// want to enable discrete values and support ticks, but want to still provide
// non-discrete slider visual looks if thumb label is disabled.
// TODO(devversion): check if we can get a public API for this.
// Tracked with: https://github.com/material-components/material-components-web/issues/5020
(this._foundation as any).isDiscrete_ = true;

// These bindings cannot be synced in the foundation, as the foundation is not
// initialized and they cause DOM globals to be accessed (to move the thumb)
this._syncStep();
this._syncValue();
this._syncMax();
this._syncMin();
}

this._syncStep();
this._syncValue();
this._syncMax();
this._syncMin();
this._syncDisabled();
}

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

ngOnDestroy() {
this._dirChangeSubscription.unsubscribe();
this._foundation.destroy();
// The foundation cannot be destroyed on the server, as the foundation
// has not be initialized on the server.
if (this._platform.isBrowser) {
this._foundation.destroy();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just check if (this._foundation)? That way if we add another condition to the initialization logic it won't break.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foundation is always created, so the check would always be true. If we would only create the foundation on the browser, I guess we'd need to make it "null"-able and handle it in more places.

}
}

/** Focuses the slider. */
Expand Down
6 changes: 6 additions & 0 deletions src/universal-app/kitchen-sink-mdc/kitchen-sink-mdc.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ <h2>MDC slide-toggle</h2>
<mat-slide-toggle checked></mat-slide-toggle>
<mat-slide-toggle checked disabled></mat-slide-toggle>
<mat-slide-toggle>with a label</mat-slide-toggle>

<h2>Slider</h2>
<mat-slider></mat-slider>
<mat-slider value="50"></mat-slider>
<mat-slider tickInterval="1" min="1" max="10" value="5" thumbLabel></mat-slider>
<mat-slider disabled></mat-slider>
2 changes: 2 additions & 0 deletions src/universal-app/kitchen-sink-mdc/kitchen-sink-mdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {MatChipsModule} from '@angular/material-experimental/mdc-chips';
import {MatMenuModule} from '@angular/material-experimental/mdc-menu';
import {MatRadioModule} from '@angular/material-experimental/mdc-radio';
import {MatSlideToggleModule} from '@angular/material-experimental/mdc-slide-toggle';
import {MatSliderModule} from '@angular/material-experimental/mdc-slider';
import {MatTabsModule} from '@angular/material-experimental/mdc-tabs';

@Component({
Expand All @@ -24,6 +25,7 @@ export class KitchenSinkMdc {
MatMenuModule,
MatRadioModule,
MatSlideToggleModule,
MatSliderModule,
MatTabsModule,
],
declarations: [KitchenSinkMdc],
Expand Down
1 change: 1 addition & 0 deletions src/universal-app/kitchen-sink/kitchen-sink.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ <h2>Slider</h2>
<mat-slider></mat-slider>
<mat-slider value="50"></mat-slider>
<mat-slider tickInterval="1" min="1" max="10" value="5" thumbLabel></mat-slider>
<mat-slider disabled></mat-slider>

<h2>Tabs</h2>
<mat-tab-group>
Expand Down