Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions src/components/InfiniteScrollPaginator/InfiniteScroll.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PropsWithChildren } from 'react';
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import type { PaginatorProps } from '../../types/types';
import { deprecationAndReplacementWarning } from '../../utils/deprecationWarning';
import { DEFAULT_LOAD_PAGE_SCROLL_THRESHOLD } from '../../constants/limits';
Expand Down Expand Up @@ -59,7 +59,7 @@ export type InfiniteScrollProps = PaginatorProps & {
export const InfiniteScroll = (props: PropsWithChildren<InfiniteScrollProps>) => {
const {
children,
element = 'div',
element: Component = 'div',
hasMore,
hasMoreNewer,
hasNextPage,
Expand All @@ -83,13 +83,13 @@ export const InfiniteScroll = (props: PropsWithChildren<InfiniteScrollProps>) =>
const hasNextPageFlag = hasNextPage || hasMoreNewer;
const hasPreviousPageFlag = hasPreviousPage || hasMore;

const scrollComponent = useRef<HTMLElement>(undefined);
const [scrollComponent, setScrollComponent] = useState<HTMLElement | null>(null);
const previousOffset = useRef<number | undefined>(undefined);
const previousReverseOffset = useRef<number | undefined>(undefined);

const scrollListenerRef = useRef<() => void>(undefined);
scrollListenerRef.current = () => {
const element = scrollComponent.current;
const element = scrollComponent;

if (!element || element.offsetParent === null) {
return;
Expand All @@ -107,6 +107,7 @@ export const InfiniteScroll = (props: PropsWithChildren<InfiniteScrollProps>) =>
}

if (isLoading) return;

if (
previousOffset.current === offset &&
previousReverseOffset.current === reverseOffset
Expand Down Expand Up @@ -147,7 +148,8 @@ export const InfiniteScroll = (props: PropsWithChildren<InfiniteScrollProps>) =>
}, []);

useEffect(() => {
const scrollElement = scrollComponent.current?.parentNode;
const scrollElement = scrollComponent?.parentNode;

if (!scrollElement) return;

const scrollListener = () => scrollListenerRef.current?.();
Expand All @@ -160,32 +162,25 @@ export const InfiniteScroll = (props: PropsWithChildren<InfiniteScrollProps>) =>
scrollElement.removeEventListener('scroll', scrollListener, useCapture);
scrollElement.removeEventListener('resize', scrollListener, useCapture);
};
}, [initialLoad, useCapture]);
}, [initialLoad, scrollComponent, useCapture]);

useEffect(() => {
const scrollElement = scrollComponent.current?.parentNode;
if (scrollElement) {
scrollElement.addEventListener('wheel', mousewheelListener, { passive: false });
}
return () => {
if (scrollElement) {
scrollElement.removeEventListener('wheel', mousewheelListener, useCapture);
}
};
}, [useCapture]);
const scrollElement = scrollComponent?.parentNode;

const attributes = {
...elementProps,
ref: (element: HTMLElement) => {
scrollComponent.current = element;
},
};

const childrenArray = [loader, children];
if (!scrollElement) return;

if (head) {
childrenArray.unshift(head);
}
scrollElement.addEventListener('wheel', mousewheelListener, { passive: false });

return React.createElement(element, attributes, childrenArray);
return () => {
scrollElement.removeEventListener('wheel', mousewheelListener, useCapture);
};
}, [scrollComponent, useCapture]);

return (
<Component {...elementProps} ref={setScrollComponent}>
{head}
{loader}
{children}
</Component>
Comment on lines +180 to +184
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we switch tsconfig.json#jsx option to react-jsx this will be translated to the _jsx function and React won't have to be imported to this module.

);
};
Loading