Skip to content
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

fix(material-experimental/mdc-slider): update layout when container resizes #24648

Merged
merged 1 commit into from
Mar 27, 2022
Merged
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
35 changes: 35 additions & 0 deletions src/material-experimental/mdc-slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,12 @@ export class MatSlider
/** Subscription to changes to the directionality (LTR / RTL) context for the application. */
private _dirChangeSubscription: Subscription;

/** Observer used to monitor size changes in the slider. */
private _resizeObserver: ResizeObserver | null;

/** Timeout used to debounce resize listeners. */
private _resizeTimer: number;

constructor(
readonly _ngZone: NgZone,
readonly _cdr: ChangeDetectorRef,
Expand Down Expand Up @@ -727,6 +733,7 @@ export class MatSlider
this._foundation.init();
this._foundation.layout();
this._initialized = true;
this._observeHostResize();
}
// The MDC foundation requires access to the view and content children of the MatSlider. In
// order to access the view and content children of MatSlider we need to wait until change
Expand All @@ -746,6 +753,9 @@ export class MatSlider
this._foundation.destroy();
}
this._dirChangeSubscription.unsubscribe();
this._resizeObserver?.disconnect();
this._resizeObserver = null;
clearTimeout(this._resizeTimer);
this._removeUISyncEventListener();
}

Expand Down Expand Up @@ -919,6 +929,31 @@ export class MatSlider
_isRippleDisabled(): boolean {
return this.disabled || this.disableRipple || !!this._globalRippleOptions?.disabled;
}

/** Starts observing and updating the slider if the host changes its size. */
private _observeHostResize() {
if (typeof ResizeObserver === 'undefined' || !ResizeObserver) {
return;
}

// MDC only updates the slider when the window is resized which
// doesn't capture changes of the container itself. We use a resize
// observer to ensure that the layout is correct (see #24590).
this._ngZone.runOutsideAngular(() => {
// The callback will fire as soon as an element is observed and
// we only want to know after the initial layout.
Copy link

Choose a reason for hiding this comment

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

this is actually what ResizeObserver already does. The callback is not executed synchronously, but after initial layout is done, see https://medium.com/@paul_irish/requestanimationframe-scheduling-for-nerds-9c57f7438ef4

if you actually want to ignore the first layout (i.e. the first callback invocation), the comment is misleading

Copy link
Member Author

Choose a reason for hiding this comment

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

I was playing around with resizing the browser and it definitely felt like it was emitting for each pixel.

Copy link
Member

Choose a reason for hiding this comment

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

let hasResized = false;
this._resizeObserver = new ResizeObserver(() => {
if (hasResized) {
// Debounce the layouts since they can happen frequently.
clearTimeout(this._resizeTimer);
this._resizeTimer = setTimeout(this._layout, 50);
}
hasResized = true;
});
this._resizeObserver.observe(this._elementRef.nativeElement);
});
}
}

/** The MDCSliderAdapter implementation. */
Expand Down