1- interface UseScrollToTopOptions {
2- /**
3- * Duration of the scroll animation in milliseconds.
4- */
5- duration ?: number
6- }
7-
81// Easing function for the scroll animation
92const easeOutQuad = ( t : number ) => t * ( 2 - t )
103
114export const SCROLL_TO_TOP_THRESHOLD = 300
5+ const SCROLL_TO_TOP_DURATION = 500
126
137/**
148 * Scroll to the top of the page with a smooth animation.
159 * @param options - Configuration options for the scroll animation.
1610 * @returns An object containing the scrollToTop function and a cancel function.
1711 */
18- export function useScrollToTop ( options : UseScrollToTopOptions ) {
19- const { duration = 500 } = options
20-
12+ export const useScrollToTop = createSharedComposable ( function useScrollToTop ( ) {
2113 // Check if prefers-reduced-motion is enabled
2214 const preferReducedMotion = useMediaQuery ( '(prefers-reduced-motion: reduce)' )
2315
@@ -71,7 +63,7 @@ export function useScrollToTop(options: UseScrollToTopOptions) {
7163 // Start the frame-by-frame scroll animation.
7264 function animate ( ) {
7365 const elapsed = performance . now ( ) - startTime
74- const t = Math . min ( elapsed / duration , 1 )
66+ const t = Math . min ( elapsed / SCROLL_TO_TOP_DURATION , 1 )
7567 const y = start + change * easeOutQuad ( t )
7668
7769 window . scrollTo ( { top : y } )
@@ -98,4 +90,4 @@ export function useScrollToTop(options: UseScrollToTopOptions) {
9890 cancel,
9991 isTouchDeviceClient,
10092 }
101- }
93+ } )
0 commit comments