• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

glideapps / glide-data-grid / 7435539780

07 Jan 2024 02:33AM CUT coverage: 90.255% (+3.8%) from 86.42%
7435539780

Pull #810

github

web-flow
Merge c2dbff45d into 3068d54a9
Pull Request #810: 6.0.0

2629 of 3279 branches covered (0.0%)

16144 of 17887 relevant lines covered (90.26%)

2994.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.67
/packages/core/src/internal/scrolling-data-grid/use-kinetic-scroll.ts
1
import { useEffect, useRef } from "react";
1✔
2

1✔
3
const useKineticScroll = (
1✔
4
    isEnabled: boolean,
671✔
5
    callback: () => void,
671✔
6
    targetScroller: React.MutableRefObject<HTMLDivElement | null>
671✔
7
) => {
671✔
8
    const rafId = useRef<number | null>(null);
671✔
9
    const isTouching = useRef<boolean | null>(null);
671✔
10
    const lastScrollPosition = useRef<number | null>(null);
671✔
11
    const sameCount = useRef(0);
671✔
12

671✔
13
    const callbackRef = useRef(callback);
671✔
14
    callbackRef.current = callback;
671✔
15

671✔
16
    const scrollEl = targetScroller.current;
671✔
17

671✔
18
    useEffect(() => {
671✔
19
        const handleScroll = () => {
274✔
20
            if (isTouching.current === false) {
×
21
                const currentScrollPosition = scrollEl?.scrollTop ?? 0;
×
22
                if (lastScrollPosition.current === currentScrollPosition) {
×
23
                    if (sameCount.current > 3) {
×
24
                        // Scroll position hasn't changed, stop the animation frame
×
25
                        lastScrollPosition.current = null;
×
26
                        isTouching.current = null;
×
27
                        return;
×
28
                    } else {
×
29
                        sameCount.current++;
×
30
                    }
×
31
                } else {
×
32
                    sameCount.current = 0;
×
33
                }
×
34

×
35
                lastScrollPosition.current = currentScrollPosition;
×
36
                callbackRef.current();
×
37
                rafId.current = window.setTimeout(handleScroll, 1000 / 120);
×
38
            }
×
39
        };
×
40

274✔
41
        const startTouch = () => {
274✔
42
            isTouching.current = true;
×
43
            lastScrollPosition.current = null; // Reset last scroll position on touch start
×
44
            if (rafId.current !== null) {
×
45
                window.clearTimeout(rafId.current);
×
46
                rafId.current = null;
×
47
            }
×
48
        };
×
49

274✔
50
        const endTouch = (event: TouchEvent) => {
274✔
51
            if (event.touches.length === 0) {
×
52
                // All touches have ended
×
53
                isTouching.current = false;
×
54
                sameCount.current = 0;
×
55
                rafId.current = window.setTimeout(handleScroll, 1000 / 120);
×
56
            }
×
57
        };
×
58

274✔
59
        if (isEnabled && scrollEl !== null) {
274!
60
            const element = scrollEl;
×
61
            element.addEventListener("touchstart", startTouch);
×
62
            element.addEventListener("touchend", endTouch);
×
63

×
64
            return () => {
×
65
                element.removeEventListener("touchstart", startTouch);
×
66
                element.removeEventListener("touchend", endTouch);
×
67
                if (rafId.current !== null) {
×
68
                    window.clearTimeout(rafId.current);
×
69
                }
×
70
            };
×
71
        }
×
72
    }, [isEnabled, scrollEl]);
671✔
73
};
671✔
74

1✔
75
export default useKineticScroll;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc