akanjs 2.3.5-rc.2 → 2.3.5-rc.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "2.3.5-rc.2",
3
+ "version": "2.3.5-rc.3",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -22,6 +22,7 @@ export const InfiniteScroll = ({
22
22
  reverse,
23
23
  }: InfiniteScrollProps) => {
24
24
  const [isFetching, setIsFetching] = useState(false);
25
+ const isFetchingRef = useRef(false);
25
26
  const target = useRef<HTMLDivElement>(null);
26
27
  const page = useRef<number>(currentPage);
27
28
  const totalPages = Math.ceil(total / (itemsPerPage || 1));
@@ -38,14 +39,39 @@ export const InfiniteScroll = ({
38
39
  }, []);
39
40
 
40
41
  const fetchMoreItems = async () => {
41
- if (isFetching) return;
42
+ if (isFetchingRef.current) return;
42
43
  const nextPage = page.current + 1;
43
44
  if (nextPage > totalPages) return;
45
+
46
+ const scroller = reverse ? document.scrollingElement : null;
47
+ const prevScrollHeight = scroller?.scrollHeight ?? 0;
48
+ const prevScrollTop = scroller?.scrollTop ?? 0;
49
+
50
+ isFetchingRef.current = true;
44
51
  setIsFetching(true);
45
- await onAddPage(nextPage);
46
- onPageSelect(nextPage);
47
- setIsFetching(false);
48
- page.current = nextPage;
52
+ try {
53
+ await onAddPage(nextPage);
54
+ onPageSelect(nextPage);
55
+ page.current = nextPage;
56
+
57
+ const restoreScroll = () => {
58
+ if (scroller) {
59
+ scroller.scrollTop = prevScrollTop + (scroller.scrollHeight - prevScrollHeight);
60
+ }
61
+ isFetchingRef.current = false;
62
+ setIsFetching(false);
63
+ };
64
+
65
+ if (typeof requestAnimationFrame === "function") {
66
+ requestAnimationFrame(restoreScroll);
67
+ } else {
68
+ restoreScroll();
69
+ }
70
+ } catch (error) {
71
+ isFetchingRef.current = false;
72
+ setIsFetching(false);
73
+ throw error;
74
+ }
49
75
  };
50
76
 
51
77
  return (
package/ui/More.tsx CHANGED
@@ -1,8 +1,10 @@
1
1
  "use client";
2
2
  import { clsx, isMobileDevice } from "akanjs/client";
3
- import { InfiniteScroll } from "./InfiniteScroll";
3
+ import { lazy } from "akanjs/webkit";
4
4
  import { Pagination } from "./Pagination";
5
5
 
6
+ const InfiniteScroll = lazy(() => import("./InfiniteScroll").then((mod) => mod.InfiniteScroll), { ssr: false });
7
+
6
8
  interface MoreProps {
7
9
  total: number;
8
10
  itemsPerPage: number;