Skip to content

Commit e30c74a

Browse files
committed
add ssr condition nuxt
1 parent 5f2f8d9 commit e30c74a

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/useScroll.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { watch, onMounted, ref, computed, type Ref, type ComputedRef } from 'vue';
2-
import { isFirefox, useMediaRef } from './utils';
2+
import { isFirefox, isSSR, useMediaRef } from './utils';
33

44
type UseScrollOptions = {
55
isWindow: ComputedRef<boolean>;
@@ -24,10 +24,10 @@ export function useScroll({
2424
const isIdle = ref(false);
2525
const clickY = computed(() => (isClick.value ? getY() : 0));
2626

27-
let prevY = getY();
27+
let prevY = isSSR ? 0 : getY();
2828

2929
function getY() {
30-
return isWindow.value ? window.scrollY : root.value?.scrollTop || 0; // SSR safe
30+
return isWindow.value ? window.scrollY : root.value?.scrollTop ?? 0;
3131
}
3232

3333
function setIdle(maxFrames = 20) {

src/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { customRef, Ref } from 'vue';
22

33
export const isSSR = typeof window === 'undefined';
44

5-
export const FIXED_TO_TOP_OFFSET = 10;
65
export const FIXED_OFFSET = 5;
76

87
export function isFirefox() {
@@ -32,7 +31,7 @@ export function getEdges(root: HTMLElement) {
3231
// Mobile devices
3332
const clientHeight = root === document.documentElement ? window.innerHeight : root.clientHeight;
3433

35-
const isTopReached = root.scrollTop <= FIXED_TO_TOP_OFFSET;
34+
const isTopReached = root.scrollTop <= FIXED_OFFSET * 2;
3635
const isBottomReached = Math.abs(root.scrollHeight - clientHeight - root.scrollTop) <= 1;
3736
const isOverscrollTop = root.scrollTop < 0;
3837
const isOverscrollBottom = root.scrollTop > root.scrollHeight - clientHeight;

0 commit comments

Comments
 (0)