-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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; | ||
|
@@ -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(); | ||
|
||
// 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(); | ||
} | ||
|
||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just check There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. */ | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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