1- import { watch , onMounted , ref , Ref , ComputedRef , computed } from 'vue' ;
1+ import { watch , onMounted , ref , computed , type Ref , type ComputedRef } from 'vue' ;
22import { isSSR , useMediaRef } from './utils' ;
33
44type UseListenersOptions = {
5- isHTML : ComputedRef < boolean > ;
5+ isWindow : ComputedRef < boolean > ;
66 root : Ref < HTMLElement | null > ;
77 matchMedia : Ref < boolean > ;
88 _setActive : ( prevY : number , isCancel ?: { isCancel : boolean } ) => void ;
99} ;
1010
1111const ONCE = { once : true } ;
1212
13- export function useScroll ( { isHTML , root, _setActive, matchMedia } : UseListenersOptions ) {
13+ export function useScroll ( { isWindow , root, _setActive, matchMedia } : UseListenersOptions ) {
1414 const isClick = useMediaRef ( matchMedia , false ) ;
1515 const isReady = ref ( false ) ;
1616 const clickY = computed ( ( ) => ( isClick . value ? getNextY ( ) : 0 ) ) ;
1717
1818 let prevY : number ;
1919
2020 function getNextY ( ) {
21- return isHTML . value ? window . scrollY : root . value ! . scrollTop ;
21+ return isWindow . value ? window . scrollY : root . value ? .scrollTop || 0 ;
2222 }
2323
2424 function setReady ( maxFrames : number ) {
25+ let rafId : DOMHighResTimeStamp | undefined = undefined ;
2526 let rafPrevY : number ;
26- let rafId : DOMHighResTimeStamp ;
2727 let frameCount = 0 ;
2828
2929 function scrollEnd ( ) {
3030 const rafNextY = getNextY ( ) ;
3131 if ( typeof rafPrevY === 'undefined' || rafPrevY !== rafNextY ) {
3232 frameCount = 0 ;
3333 rafPrevY = rafNextY ;
34- // console.log('Scrolling...');
3534 return requestAnimationFrame ( scrollEnd ) ;
3635 }
3736 // When equal, wait for n frames after scroll to make sure is idle
3837 frameCount ++ ;
3938 if ( frameCount === maxFrames ) {
4039 isReady . value = true ;
4140 isClick . value = false ;
42- console . log ( 'Scroll end.' ) ;
43- cancelAnimationFrame ( rafId ) ;
41+ cancelAnimationFrame ( rafId as DOMHighResTimeStamp ) ;
4442 } else {
4543 requestAnimationFrame ( scrollEnd ) ;
4644 }
@@ -78,7 +76,7 @@ export function useScroll({ isHTML, root, _setActive, matchMedia }: UseListeners
7876 if ( ! isLink && ! hasLink ) {
7977 reScroll ( ) ;
8078 // ...and force set if canceling scroll
81- _setActive ( clickY . value ! , { isCancel : true } ) ;
79+ _setActive ( clickY . value , { isCancel : true } ) ;
8280 }
8381 }
8482
@@ -93,24 +91,22 @@ export function useScroll({ isHTML, root, _setActive, matchMedia }: UseListeners
9391
9492 watch (
9593 [ isReady , matchMedia , root ] ,
96- ( [ _isReady , _matchMedia , _root ] , [ ] , onCleanup ) => {
94+ ( [ _isReady , _matchMedia , _root ] , _ , onCleanup ) => {
9795 if ( isSSR ) {
9896 return ;
9997 }
10098
101- const rootEl = isHTML . value ? document : _root ;
99+ const rootEl = isWindow . value ? document : _root ;
102100 const isActive = rootEl && _isReady && _matchMedia ;
103101
104102 if ( isActive ) {
105- console . log ( 'Adding main listener...' ) ;
106103 rootEl . addEventListener ( 'scroll' , onScroll , {
107104 passive : true ,
108105 } ) ;
109106 }
110107
111108 onCleanup ( ( ) => {
112109 if ( isActive ) {
113- console . log ( 'Removing main listener...' ) ;
114110 rootEl . removeEventListener ( 'scroll' , onScroll ) ;
115111 }
116112 } ) ;
@@ -125,10 +121,9 @@ export function useScroll({ isHTML, root, _setActive, matchMedia }: UseListeners
125121 watch (
126122 isClick ,
127123 ( _isClick , _ , onCleanup ) => {
128- const rootEl = isHTML . value ? document : root . value ! ;
124+ const rootEl = isWindow . value ? document : root . value ;
129125
130- if ( _isClick ) {
131- console . log ( 'Adding additional listeners...' ) ;
126+ if ( _isClick && rootEl ) {
132127 rootEl . addEventListener ( 'scroll' , resetReady , ONCE ) ;
133128 rootEl . addEventListener ( 'wheel' , reScroll , ONCE ) ;
134129 rootEl . addEventListener ( 'keydown' , onSpaceBar as EventListener , ONCE ) ;
@@ -137,8 +132,7 @@ export function useScroll({ isHTML, root, _setActive, matchMedia }: UseListeners
137132 }
138133
139134 onCleanup ( ( ) => {
140- if ( _isClick ) {
141- console . log ( 'Removing additional listeners...' ) ;
135+ if ( _isClick && rootEl ) {
142136 rootEl . removeEventListener ( 'scroll' , resetReady ) ;
143137 rootEl . removeEventListener ( 'wheel' , reScroll ) ;
144138 rootEl . removeEventListener ( 'keydown' , onSpaceBar as EventListener ) ;
0 commit comments