diff --git a/app/components/ScrollToTop.client.vue b/app/components/ScrollToTop.client.vue index e1e70ec5f..f09c13f2d 100644 --- a/app/components/ScrollToTop.client.vue +++ b/app/components/ScrollToTop.client.vue @@ -3,42 +3,32 @@ const route = useRoute() // Pages where scroll-to-top should NOT be shown const excludedRoutes = new Set(['index', 'code']) +const isPackagePage = computed(() => route.name === 'package' || route.name === 'package-version') -const isActive = computed(() => !excludedRoutes.has(route.name as string)) +const isActive = computed(() => !excludedRoutes.has(route.name as string) && !isPackagePage.value) const isMounted = useMounted() -const isVisible = shallowRef(false) -const scrollThreshold = 300 +const { scrollToTop, isTouchDeviceClient } = useScrollToTop() + +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)