@@ -71,6 +71,7 @@ export function ScrollControls({
7171 const [ fixed ] = React . useState ( ( ) => document . createElement ( 'div' ) )
7272 const target = gl . domElement . parentNode ! as HTMLElement
7373 const scroll = React . useRef ( 0 )
74+ const disableScrollUpdate = React . useRef ( false )
7475
7576 const state = React . useMemo ( ( ) => {
7677 const state = {
@@ -170,6 +171,12 @@ export function ScrollControls({
170171 const onScroll = ( ) => {
171172 // Prevent first scroll because it is indirectly caused by the one pixel offset
172173 if ( ! enabled || firstRun ) return
174+
175+ if ( disableScrollUpdate . current ) {
176+ disableScrollUpdate . current = false
177+ return
178+ }
179+
173180 invalidate ( )
174181 current = el [ horizontal ? 'scrollLeft' : 'scrollTop' ]
175182 scroll . current = current / scrollThreshold
@@ -194,29 +201,38 @@ export function ScrollControls({
194201 el . addEventListener ( 'scroll' , onScroll , { passive : true } )
195202 requestAnimationFrame ( ( ) => ( firstRun = false ) )
196203
197- const onWheel = e => {
204+ const onResize = ( ) => {
205+ if ( ! el ) return
206+
207+ const scrollLength = el [ horizontal ? 'scrollWidth' : 'scrollHeight' ]
208+
209+ disableScrollUpdate . current = true
210+ el . scrollLeft = state . offset * ( scrollLength - el . clientWidth )
211+ }
212+
213+ const onWheel = ( e ) => {
198214 if ( horizontal ) {
199- const previous = el . scrollLeft ;
200- el . scrollLeft = el . scrollLeft += e . deltaY / 2 ;
201- if ( infinite && previous === el . scrollLeft ) {
202- onScroll ( ) ;
215+ const previous = el . scrollLeft
216+ el . scrollLeft += e . deltaY / 2
217+ if ( infinite && previous == el . scrollLeft ) {
218+ onScroll ( )
203219 }
204220 } else if ( infinite ) {
205221 if ( el . scrollTop <= 1 && e . deltaY < 0 ) {
206- onScroll ( ) ;
207- } else if (
208- el . scrollTop >= el . scrollHeight - el . clientHeight - 1 &&
209- e . deltaY > 0
210- ) {
211- onScroll ( ) ;
222+ onScroll ( )
223+ } else if ( el . scrollTop >= el . scrollHeight - el . clientHeight - 1 && e . deltaY > 0 ) {
224+ onScroll ( )
212225 }
213226 }
214- } ;
227+ }
215228 if ( horizontal || infinite ) el . addEventListener ( 'wheel' , onWheel , { passive : true } )
216229
230+ if ( horizontal ) window . addEventListener ( 'resize' , onResize )
231+
217232 return ( ) => {
218233 el . removeEventListener ( 'scroll' , onScroll )
219234 if ( horizontal || infinite ) el . removeEventListener ( 'wheel' , onWheel )
235+ if ( horizontal ) window . removeEventListener ( 'resize' , onResize )
220236 }
221237 }
222238 } , [ el , events , size , infinite , state , invalidate , horizontal , enabled ] )
0 commit comments