Skip to content

Commit aa008de

Browse files
committed
fix(material-experimental/mdc-slider): skip resizing while the user is dragging (#25318)
Fixes an internal bug caused by #25288 where a slider was resizing while the user is dragging, causing the thumb to jump around. (cherry picked from commit 8ad2ef6)
1 parent cf7a971 commit aa008de

File tree

1 file changed

+18
-6
lines changed
  • src/material-experimental/mdc-slider

1 file changed

+18
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class MatSliderVisualThumb implements AfterViewInit, OnDestroy {
127127
private _activeRippleRef: RippleRef | undefined;
128128

129129
/** Whether the slider thumb is currently being pressed. */
130-
private _isActive: boolean = false;
130+
readonly _isActive = false;
131131

132132
/** Whether the slider thumb is currently being hovered. */
133133
private _isHovered: boolean = false;
@@ -201,14 +201,14 @@ export class MatSliderVisualThumb implements AfterViewInit, OnDestroy {
201201

202202
private _onDragStart(event: MatSliderDragEvent): void {
203203
if (event.source._thumbPosition === this.thumbPosition) {
204-
this._isActive = true;
204+
(this as {_isActive: boolean})._isActive = true;
205205
this._showActiveRipple();
206206
}
207207
}
208208

209209
private _onDragEnd(event: MatSliderDragEvent): void {
210210
if (event.source._thumbPosition === this.thumbPosition) {
211-
this._isActive = false;
211+
(this as {_isActive: boolean})._isActive = false;
212212
this._activeRippleRef?.fadeOut();
213213
// Happens when the user starts dragging a thumb, tabs away, and then stops dragging.
214214
if (!this._sliderInput._isFocused()) {
@@ -960,19 +960,31 @@ export class MatSlider
960960
// observer to ensure that the layout is correct (see #24590 and #25286).
961961
this._ngZone.runOutsideAngular(() => {
962962
this._resizeObserver = new ResizeObserver(entries => {
963+
// Triggering a layout while the user is dragging can throw off the alignment.
964+
if (this._isActive()) {
965+
return;
966+
}
967+
963968
clearTimeout(this._resizeTimer);
964969
this._resizeTimer = setTimeout(() => {
965970
// The `layout` call is going to call `getBoundingClientRect` to update the dimensions
966971
// of the host. Since the `ResizeObserver` already calculated them, we can save some
967972
// work by returning them instead of having to check the DOM again.
968-
this._cachedHostRect = entries[0]?.contentRect;
969-
this._layout();
970-
this._cachedHostRect = null;
973+
if (!this._isActive()) {
974+
this._cachedHostRect = entries[0]?.contentRect;
975+
this._layout();
976+
this._cachedHostRect = null;
977+
}
971978
}, 50);
972979
});
973980
this._resizeObserver.observe(this._elementRef.nativeElement);
974981
});
975982
}
983+
984+
/** Whether any of the thumbs are currently active. */
985+
private _isActive(): boolean {
986+
return this._getThumb(Thumb.START)._isActive || this._getThumb(Thumb.END)._isActive;
987+
}
976988
}
977989

978990
/** The MDCSliderAdapter implementation. */

0 commit comments

Comments
 (0)