@blenx-dev/datatable 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ import { type RefObject, useCallback, useEffect, useRef } from "react";
2
+
3
+ interface UseInfiniteScrollOptions {
4
+ /** Whether there are more pages to load */
5
+ hasNextPage: boolean;
6
+ /** Whether a fetch is currently in progress */
7
+ isFetchingNextPage: boolean;
8
+ /** Function to fetch the next page */
9
+ fetchNextPage: () => void;
10
+ /** IntersectionObserver root margin. Default: "200px" */
11
+ rootMargin?: string;
12
+ /** IntersectionObserver threshold. Default: 0 */
13
+ threshold?: number;
14
+ /** If true, the observer is disabled */
15
+ enabled?: boolean;
16
+ }
17
+
18
+ interface UseInfiniteScrollReturn {
19
+ /** Ref to attach to the sentinel element */
20
+ sentinelRef: RefObject<HTMLDivElement | null>;
21
+ }
22
+
23
+ /**
24
+ * Hook to observe a sentinel element and trigger fetchNextPage when visible.
25
+ *
26
+ * Uses IntersectionObserver with a configurable root margin and threshold.
27
+ * Respects hasNextPage, isFetchingNextPage, and enabled state to prevent
28
+ * duplicate requests.
29
+ */
30
+ export function useInfiniteScroll({
31
+ hasNextPage,
32
+ isFetchingNextPage,
33
+ fetchNextPage,
34
+ rootMargin = "200px",
35
+ threshold = 0,
36
+ enabled = true,
37
+ }: UseInfiniteScrollOptions): UseInfiniteScrollReturn {
38
+ const sentinelRef = useRef<HTMLDivElement | null>(null);
39
+
40
+ const handleIntersection = useCallback(
41
+ (entries: IntersectionObserverEntry[]) => {
42
+ const [entry] = entries;
43
+ if (entry?.isIntersecting && hasNextPage && !isFetchingNextPage) {
44
+ fetchNextPage();
45
+ }
46
+ },
47
+ [hasNextPage, isFetchingNextPage, fetchNextPage],
48
+ );
49
+
50
+ useEffect(() => {
51
+ const sentinel = sentinelRef.current;
52
+ if (!sentinel || !enabled) return;
53
+
54
+ const observer = new IntersectionObserver(handleIntersection, {
55
+ rootMargin,
56
+ threshold,
57
+ });
58
+
59
+ observer.observe(sentinel);
60
+
61
+ return () => {
62
+ observer.disconnect();
63
+ };
64
+ }, [enabled, handleIntersection, rootMargin, threshold]);
65
+
66
+ return { sentinelRef };
67
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./DataTable";
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@blenx-dev/config/tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "jsx": "react-jsx",
5
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
6
+ "types": []
7
+ },
8
+ "include": ["src/**/*.ts", "src/**/*.tsx"],
9
+ "exclude": ["node_modules"]
10
+ }