@@ -69,7 +69,7 @@ const RCTSliderWebComponent = React.forwardRef(
6969 const containerSize = React . useRef ( { width : 0 , height : 0 } ) ;
7070 const containerPositionX = React . useRef ( 0 ) ;
7171 const containerRef = forwardedRef || React . createRef ( ) ;
72- const hasBeenResized = React . useRef ( false ) ;
72+ const containerPositionInvalidated = React . useRef ( false ) ;
7373 const [ value , setValue ] = React . useState ( initialValue || minimumValue ) ;
7474 const lastInitialValue = React . useRef < number > ( ) ;
7575
@@ -145,18 +145,36 @@ const RCTSliderWebComponent = React.forwardRef(
145145 }
146146 } , [ initialValue , updateValue , animatedValue ] ) ;
147147
148- const onResize = ( ) => {
149- hasBeenResized . current = true ;
150- } ;
151148 React . useEffect ( ( ) => {
149+ const invalidateContainerPosition = ( ) => {
150+ containerPositionInvalidated . current = true ;
151+ } ;
152+ const onDocumentScroll = ( e : Event ) => {
153+ if (
154+ // Avoid all other checks if already invalidated
155+ ! containerPositionInvalidated . current &&
156+ containerRef . current &&
157+ // @ts -ignore
158+ e . target . contains ( containerRef . current )
159+ ) {
160+ invalidateContainerPosition ( ) ;
161+ }
162+ } ;
152163 //@ts -ignore
153- window . addEventListener ( 'resize' , onResize ) ;
164+ window . addEventListener ( 'resize' , invalidateContainerPosition ) ;
165+ //@ts -ignore
166+ document . addEventListener ( 'scroll' , onDocumentScroll , { capture : true } ) ;
154167
155168 return ( ) => {
156169 //@ts -ignore
157- window . removeEventListener ( 'resize' , onResize ) ;
170+ window . removeEventListener ( 'resize' , invalidateContainerPosition ) ;
171+
172+ //@ts -ignore
173+ document . removeEventListener ( 'scroll' , onDocumentScroll , {
174+ capture : true ,
175+ } ) ;
158176 } ;
159- } , [ ] ) ;
177+ } , [ containerRef ] ) ;
160178
161179 const containerStyle = StyleSheet . compose (
162180 {
@@ -221,8 +239,8 @@ const RCTSliderWebComponent = React.forwardRef(
221239 const getValueFromNativeEvent = ( pageX : number ) => {
222240 const { width = 1 } = containerSize . current ;
223241
224- if ( hasBeenResized . current ) {
225- hasBeenResized . current = false ;
242+ if ( containerPositionInvalidated . current ) {
243+ containerPositionInvalidated . current = false ;
226244 updateContainerPositionX ( ) ;
227245 }
228246 const containerX = containerPositionX . current ;
0 commit comments