-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfinite-scroll.tsx
More file actions
81 lines (71 loc) · 2.45 KB
/
Copy pathinfinite-scroll.tsx
File metadata and controls
81 lines (71 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { PropsWithChildren, ReactNode } from "react"
import { useEffect, useRef } from "react"
import { useTab } from "@/tab-provider"
import { IconLoader2 } from "@tabler/icons-react"
import type { UseInfiniteQueryResult } from "@tanstack/react-query"
import { cn } from "@/lib/utils"
import { ScrollArea } from "@/components/ui/scroll-area"
export const InfiniteScroll = ({
query,
children,
// When false, scrolling / viewport-fill won't auto-fetch the next page. Used to
// pause runaway fetching (e.g. a type filter that matches nothing) and hand
// control to an explicit continue action rendered via `endSlot`.
autoFetch = true,
endSlot,
...props
}: PropsWithChildren<{
query: UseInfiniteQueryResult
autoFetch?: boolean
endSlot?: ReactNode
}> &
React.ComponentProps<typeof ScrollArea>) => {
const { active } = useTab()
const scrollRef = useRef<HTMLDivElement>(null)
const contentRef = useRef<HTMLDivElement>(null)
// Fetch more on scroll
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => {
if (!autoFetch) return
const { scrollTop, clientHeight, scrollHeight } = e.currentTarget
if (scrollTop + clientHeight > scrollHeight - 100) {
if (query.isFetching || !query.hasNextPage) {
return
}
query.fetchNextPage()
}
}
// Check if viewport is filled and fetch more if needed
const checkAndFetchMore = () => {
if (!autoFetch) return
if (!scrollRef.current || !contentRef.current) return
const viewportHeight = scrollRef.current.clientHeight
const contentHeight = contentRef.current.clientHeight
// Fetch until it overflows a bit
const overflowThreshold = viewportHeight + 100
if (contentHeight < overflowThreshold && query.hasNextPage && !query.isFetching) {
query.fetchNextPage()
}
}
useEffect(() => {
if (!active) return
// Timeout for dom update
const timer = setTimeout(checkAndFetchMore, 100)
return () => clearTimeout(timer)
}, [active, query.data])
return (
<ScrollArea
type="always"
onScroll={handleScroll}
{...props}
className={cn("block h-full min-h-0 w-full overflow-hidden transition-all", props.className)}
ref={scrollRef}
>
<div ref={contentRef}>
{children}
<div className="flex h-[100px] justify-center py-2 text-zinc-300">
{query.isFetching ? <IconLoader2 className="animate-spin" size={16} /> : endSlot}
</div>
</div>
</ScrollArea>
)
}