Skip to content

Commit 8bda1e0

Browse files
committed
refactor: hard-code duration + use shared composable
1 parent efc36de commit 8bda1e0

File tree

3 files changed

+6
-16
lines changed

3 files changed

+6
-16
lines changed

app/components/ScrollToTop.client.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ const isPackagePage = computed(() => route.name === 'package' || route.name ===
77
88
const isActive = computed(() => !excludedRoutes.has(route.name as string) && !isPackagePage.value)
99
10-
const SCROLL_TO_TOP_DURATION = 500
11-
1210
const isMounted = useMounted()
13-
const { scrollToTop, isTouchDeviceClient } = useScrollToTop({ duration: SCROLL_TO_TOP_DURATION })
11+
const { scrollToTop, isTouchDeviceClient } = useScrollToTop()
1412
1513
const { y: scrollTop } = useScroll(window)
1614
const isVisible = computed(() => {

app/composables/useScrollToTop.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
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
92
const easeOutQuad = (t: number) => t * (2 - t)
103

114
export 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+
})

app/pages/package/[[org]]/[name].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ const { copied: copiedVersion, copy: copyVersion } = useClipboard({
251251
copiedDuring: 2000,
252252
})
253253
254-
const { scrollToTop, isTouchDeviceClient } = useScrollToTop({ duration: 500 })
254+
const { scrollToTop, isTouchDeviceClient } = useScrollToTop()
255255
256256
const { y: scrollY } = useScroll(window)
257257
const showScrollToTop = computed(

0 commit comments

Comments
 (0)