11import {
22 onBeforeUnmount ,
3+ shallowRef ,
34 ref ,
45 unref ,
56 watch ,
6- nextTick ,
7- shallowReactive ,
87 computed ,
8+ readonly ,
99 type CSSProperties ,
1010} from 'vue'
1111
@@ -34,15 +34,15 @@ export function useFixedHeader(
3434
3535 // Internal state
3636
37- const styles = shallowReactive < CSSProperties > ( { } )
37+ const styles = shallowRef < CSSProperties > ( { } )
3838 const state = ref < State > ( State . READY )
3939
4040 function setStyles ( newStyles : CSSProperties ) {
41- Object . assign ( styles , newStyles )
41+ styles . value = newStyles
4242 }
4343
4444 function removeStyles ( ) {
45- Object . keys ( styles ) . forEach ( ( key ) => delete styles [ key as keyof CSSProperties ] )
45+ styles . value = { }
4646 }
4747
4848 function setState ( newState : State ) {
@@ -52,8 +52,6 @@ export function useFixedHeader(
5252 // Target utils
5353
5454 function getRoot ( ) {
55- if ( isSSR ) return null
56-
5755 const root = unref ( mergedOptions . root )
5856 if ( root != null ) return root
5957
@@ -114,7 +112,9 @@ export function useFixedHeader(
114112
115113 setStyles ( {
116114 ...mergedOptions . enterStyles ,
117- ...( mergedOptions . toggleVisibility ? { visibility : undefined } : { } ) ,
115+ ...( mergedOptions . toggleVisibility
116+ ? { visibility : '' as CSSProperties [ 'visibility' ] }
117+ : { } ) ,
118118 ...( isReducedMotion ( ) ? { transition : 'none' } : { } ) ,
119119 } )
120120
@@ -140,7 +140,7 @@ export function useFixedHeader(
140140
141141 if ( e . target !== unref ( target ) ) return
142142
143- setStyles ( { visibility : 'hidden' } )
143+ setStyles ( { ... mergedOptions . leaveStyles , visibility : 'hidden' } )
144144 }
145145
146146 function toggleTransitionListener ( isRemove = false ) {
@@ -227,7 +227,7 @@ export function useFixedHeader(
227227 const root = getRoot ( )
228228 if ( ! root ) return
229229
230- const scrollRoot = root === document . documentElement ? window : root
230+ const scrollRoot = root === document . documentElement ? document : root
231231 const method = isRemove ? 'removeEventListener' : 'addEventListener'
232232
233233 scrollRoot [ method ] ( 'scroll' , onScroll , { passive : true } )
@@ -237,20 +237,19 @@ export function useFixedHeader(
237237
238238 // Pointer
239239
240- const setHover = ( ) => ( isHovering = true )
241- const removeHover = ( ) => ( isHovering = false )
240+ function setPointer ( e : PointerEvent ) {
241+ isHovering = unref ( target ) ?. contains ( e . target as Node ) ?? false
242+ }
242243
243244 function togglePointer ( isRemove = false ) {
244245 const method = isRemove ? 'removeEventListener' : 'addEventListener'
245246
246- unref ( target ) ?. [ method ] ( 'pointermove' , setHover )
247- unref ( target ) ?. [ method ] ( 'pointerleave' , removeHover )
247+ document [ method ] ( 'pointermove' , setPointer as EventListener )
248248 }
249249
250250 // Listeners
251251
252252 function removeListeners ( ) {
253- toggleTransitionListener ( true )
254253 toggleScroll ( true )
255254 togglePointer ( true )
256255 }
@@ -262,7 +261,7 @@ export function useFixedHeader(
262261 // If the header is not anymore fixed or sticky
263262 if ( ! isValid ) {
264263 removeListeners ( )
265- nextTick ( removeStyles )
264+ removeStyles ( )
266265 }
267266 // If was not listening and now is fixed or sticky
268267 } else {
@@ -302,10 +301,10 @@ export function useFixedHeader(
302301 */
303302 watch (
304303 ( ) => [ unref ( target ) , unref ( mergedOptions . root ) ] ,
305- ( targetEl , _ , onCleanup ) => {
306- if ( isSSR ) return
304+ ( [ targetEl , rootEl ] , _ , onCleanup ) => {
305+ const shouldInit = ! isSSR && targetEl && ( rootEl || rootEl === null )
307306
308- if ( targetEl ) {
307+ if ( shouldInit ) {
309308 /**
310309 * Resize observer is added in any case as it is
311310 * in charge of toggling scroll/pointer listeners if the header
@@ -316,7 +315,7 @@ export function useFixedHeader(
316315
317316 /**
318317 * Immediately hides the header on page load, this has effect
319- * only if scroll restoration is not smooth and toggleVisibility
318+ * only if scroll restoration is not smooth and ' toggleVisibility'
320319 * is set to true.
321320 */
322321 onInstantScrollRestoration ( )
@@ -338,7 +337,7 @@ export function useFixedHeader(
338337 onBeforeUnmount ( _onCleanup )
339338
340339 return {
341- styles,
340+ styles : readonly ( styles ) ,
342341 isLeave : computed ( ( ) => state . value === State . LEAVE ) ,
343342 isEnter : computed ( ( ) => state . value === State . ENTER ) ,
344343 }
0 commit comments