diff --git a/app/components/ScrollToTop.client.vue b/app/components/ScrollToTop.client.vue index e1e70ec5f..93e6d03e5 100644 --- a/app/components/ScrollToTop.client.vue +++ b/app/components/ScrollToTop.client.vue @@ -3,44 +3,36 @@ const route = useRoute() // Pages where scroll-to-top should NOT be shown const excludedRoutes = new Set(['index', 'code']) +const isPackagePage = computed(() => route.path.includes('/package/')) -const isActive = computed(() => !excludedRoutes.has(route.name as string)) +const isActive = computed(() => !excludedRoutes.has(route.name as string) && !isPackagePage.value) + +const SCROLL_TO_TOP_DURATION = 500 const isMounted = useMounted() -const isVisible = shallowRef(false) -const scrollThreshold = 300 +const { scrollToTop, isTouchDeviceClient } = useScrollToTop({ duration: SCROLL_TO_TOP_DURATION }) + +const { y: scrollTop } = useScroll(window) +const isVisible = computed(() => { + if (supportsScrollStateQueries.value) return false + return scrollTop.value > SCROLL_TO_TOP_THRESHOLD +}) const { isSupported: supportsScrollStateQueries } = useCssSupports( 'container-type', 'scroll-state', { ssrValue: false }, ) - -function onScroll() { - if (!supportsScrollStateQueries.value) { - return - } - isVisible.value = window.scrollY > scrollThreshold -} - -function scrollToTop() { - window.scrollTo({ top: 0, behavior: 'smooth' }) -} - -useEventListener('scroll', onScroll, { passive: true }) - -onMounted(() => { - onScroll() -}) +const shouldShowButton = computed(() => isActive.value && isTouchDeviceClient.value)