@@ -693,6 +693,12 @@ export class MatSlider
693
693
/** Subscription to changes to the directionality (LTR / RTL) context for the application. */
694
694
private _dirChangeSubscription : Subscription ;
695
695
696
+ /** Observer used to monitor size changes in the slider. */
697
+ private _resizeObserver : ResizeObserver | null ;
698
+
699
+ /** Timeout used to debounce resize listeners. */
700
+ private _resizeTimer : number ;
701
+
696
702
constructor (
697
703
readonly _ngZone : NgZone ,
698
704
readonly _cdr : ChangeDetectorRef ,
@@ -727,6 +733,7 @@ export class MatSlider
727
733
this . _foundation . init ( ) ;
728
734
this . _foundation . layout ( ) ;
729
735
this . _initialized = true ;
736
+ this . _observeHostResize ( ) ;
730
737
}
731
738
// The MDC foundation requires access to the view and content children of the MatSlider. In
732
739
// order to access the view and content children of MatSlider we need to wait until change
@@ -746,6 +753,9 @@ export class MatSlider
746
753
this . _foundation . destroy ( ) ;
747
754
}
748
755
this . _dirChangeSubscription . unsubscribe ( ) ;
756
+ this . _resizeObserver ?. disconnect ( ) ;
757
+ this . _resizeObserver = null ;
758
+ clearTimeout ( this . _resizeTimer ) ;
749
759
this . _removeUISyncEventListener ( ) ;
750
760
}
751
761
@@ -919,6 +929,31 @@ export class MatSlider
919
929
_isRippleDisabled ( ) : boolean {
920
930
return this . disabled || this . disableRipple || ! ! this . _globalRippleOptions ?. disabled ;
921
931
}
932
+
933
+ /** Starts observing and updating the slider if the host changes its size. */
934
+ private _observeHostResize ( ) {
935
+ if ( typeof ResizeObserver === 'undefined' || ! ResizeObserver ) {
936
+ return ;
937
+ }
938
+
939
+ // MDC only updates the slider when the window is resized which
940
+ // doesn't capture changes of the container itself. We use a resize
941
+ // observer to ensure that the layout is correct (see #24590).
942
+ this . _ngZone . runOutsideAngular ( ( ) => {
943
+ // The callback will fire as soon as an element is observed and
944
+ // we only want to know after the initial layout.
945
+ let hasResized = false ;
946
+ this . _resizeObserver = new ResizeObserver ( ( ) => {
947
+ if ( hasResized ) {
948
+ // Debounce the layouts since they can happen frequently.
949
+ clearTimeout ( this . _resizeTimer ) ;
950
+ this . _resizeTimer = setTimeout ( this . _layout , 50 ) ;
951
+ }
952
+ hasResized = true ;
953
+ } ) ;
954
+ this . _resizeObserver . observe ( this . _elementRef . nativeElement ) ;
955
+ } ) ;
956
+ }
922
957
}
923
958
924
959
/** The MDCSliderAdapter implementation. */
0 commit comments