@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'
44import * as u from '../urx'
55import { approximatelyEqual } from '../utils/approximatelyEqual'
66import { correctItemSize } from '../utils/correctItemSize'
7+ import { clearHorizontalScrollDirectionCache , getLogicalScrollLeft , getPhysicalScrollLeft } from '../utils/horizontalScroll'
78
89import type { ScrollContainerState } from '../interfaces'
910
@@ -39,14 +40,14 @@ export default function useScrollTop(
3940
4041 if ( isDocument ( el ) || isWindow ( el ) ) {
4142 const theWindow = isWindow ( el ) ? el : ( el as unknown as Document ) . defaultView !
42- scrollTop = horizontalDirection === true ? theWindow . scrollX : theWindow . scrollY
43+ scrollTop = horizontalDirection === true ? getLogicalScrollLeft ( theWindow , theWindow . scrollX ) : theWindow . scrollY
4344
4445 scrollHeight =
4546 horizontalDirection === true ? theWindow . document . documentElement . scrollWidth : theWindow . document . documentElement . scrollHeight
4647
4748 viewportHeight = horizontalDirection === true ? theWindow . innerWidth : theWindow . innerHeight
4849 } else {
49- scrollTop = horizontalDirection === true ? el . scrollLeft : el . scrollTop
50+ scrollTop = horizontalDirection === true ? getLogicalScrollLeft ( el , el . scrollLeft ) : el . scrollTop
5051
5152 scrollHeight = horizontalDirection === true ? el . scrollWidth : el . scrollHeight
5253
@@ -83,11 +84,13 @@ export default function useScrollTop(
8384 React . useEffect ( ( ) => {
8485 const localRef = customScrollParent ? customScrollParent : scrollerRef . current !
8586
87+ clearHorizontalScrollDirectionCache ( localRef )
8688 scrollerRefCallback ( customScrollParent ? customScrollParent : scrollerRef . current )
8789 handler ( { suppressFlushSync : true , target : localRef } as unknown as Event )
8890 localRef . addEventListener ( 'scroll' , handler , { passive : true } )
8991
9092 return ( ) => {
93+ clearHorizontalScrollDirectionCache ( localRef )
9194 scrollerRefCallback ( null )
9295 localRef . removeEventListener ( 'scroll' , handler )
9396 }
@@ -119,20 +122,27 @@ export default function useScrollTop(
119122 : scrollerElement . document . documentElement . scrollHeight
120123 )
121124 offsetHeight = horizontalDirection === true ? scrollerElement . innerWidth : scrollerElement . innerHeight
122- scrollTop = horizontalDirection === true ? window . scrollX : window . scrollY
125+ scrollTop = horizontalDirection === true ? getLogicalScrollLeft ( scrollerElement , scrollerElement . scrollX ) : scrollerElement . scrollY
123126 } else {
124127 scrollHeight = scrollerElement [ horizontalDirection === true ? 'scrollWidth' : 'scrollHeight' ]
125128 offsetHeight = correctItemSize ( scrollerElement , horizontalDirection === true ? 'width' : 'height' )
126- scrollTop = scrollerElement [ horizontalDirection === true ? 'scrollLeft' : 'scrollTop' ]
129+ scrollTop =
130+ horizontalDirection === true ? getLogicalScrollLeft ( scrollerElement , scrollerElement . scrollLeft ) : scrollerElement . scrollTop
127131 }
128132
129133 const maxScrollTop = scrollHeight - offsetHeight
130- location . top = Math . ceil ( Math . max ( Math . min ( maxScrollTop , location . top ! ) , 0 ) )
134+ if ( location . top === undefined ) {
135+ scrollerElement . scrollTo ( location )
136+ return
137+ }
138+
139+ const top = Math . ceil ( Math . max ( Math . min ( maxScrollTop , location . top ) , 0 ) )
140+ location . top = top
131141
132142 // avoid system hanging because the DOM never called back
133143 // with the scrollTop
134144 // scroller is already at this location
135- if ( approximatelyEqual ( offsetHeight , scrollHeight ) || location . top === scrollTop ) {
145+ if ( approximatelyEqual ( offsetHeight , scrollHeight ) || top === scrollTop ) {
136146 scrollContainerStateCallback ( { scrollHeight, scrollTop, viewportHeight : offsetHeight } )
137147 if ( isSmooth ) {
138148 smoothScrollTargetReached ( true )
@@ -141,7 +151,7 @@ export default function useScrollTop(
141151 }
142152
143153 if ( isSmooth ) {
144- scrollTopTarget . current = location . top
154+ scrollTopTarget . current = top
145155 if ( timeoutRef . current ) {
146156 clearTimeout ( timeoutRef . current )
147157 }
@@ -156,7 +166,10 @@ export default function useScrollTop(
156166 }
157167
158168 if ( horizontalDirection === true ) {
159- location = { ...( location . behavior !== undefined ? { behavior : location . behavior } : { } ) , left : location . top }
169+ location = {
170+ ...( location . behavior !== undefined ? { behavior : location . behavior } : { } ) ,
171+ left : getPhysicalScrollLeft ( scrollerElement , top ) ,
172+ }
160173 }
161174
162175 scrollerElement . scrollTo ( location )
@@ -166,7 +179,7 @@ export default function useScrollTop(
166179 if ( horizontalDirection === true ) {
167180 location = {
168181 ...( location . behavior !== undefined ? { behavior : location . behavior } : { } ) ,
169- ...( location . top !== undefined ? { left : location . top } : { } ) ,
182+ ...( location . top !== undefined ? { left : getPhysicalScrollLeft ( scrollerRef . current ! , location . top ) } : { } ) ,
170183 }
171184 }
172185 scrollerRef . current ! . scrollBy ( location )
0 commit comments