Skip to content

call onScroll event on failed attempt to scroll down #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface State {
showLoader: boolean;
pullToRefreshThresholdBreached: boolean;
prevDataLength: number | undefined;
hasScrollbar: boolean;
}

export default class InfiniteScroll extends Component<Props, State> {
Expand All @@ -40,17 +41,23 @@ export default class InfiniteScroll extends Component<Props, State> {
showLoader: false,
pullToRefreshThresholdBreached: false,
prevDataLength: props.dataLength,
hasScrollbar: false,
};

this.throttledOnScrollListener = throttle(150, this.onScrollListener).bind(
this
);

this.throttledWheelListener = throttle(150, this.onWheelListener).bind(
this
);
this.onStart = this.onStart.bind(this);
this.onMove = this.onMove.bind(this);
this.onEnd = this.onEnd.bind(this);
}

private throttledOnScrollListener: (e: MouseEvent) => void;
private throttledWheelListener: (e: WheelEvent) => void;
private _scrollableNode: HTMLElement | undefined | null;
private el: HTMLElement | undefined | Window & typeof globalThis;
private _infScroll: HTMLDivElement | undefined;
Expand Down Expand Up @@ -83,6 +90,8 @@ export default class InfiniteScroll extends Component<Props, State> {
if (this.el) {
this.el.addEventListener('scroll', this
.throttledOnScrollListener as EventListenerOrEventListenerObject);
this.el.addEventListener('wheel', this
.throttledWheelListener as EventListenerOrEventListenerObject);
}

if (
Expand Down Expand Up @@ -295,6 +304,21 @@ export default class InfiniteScroll extends Component<Props, State> {
);
}

onWheelListener = (event: WheelEvent) => {
if (window.pageYOffset > 0) {
this.setState({
hasScrollbar: true,
});
} else {
this.setState({ hasScrollbar: false });
}

if (!this.state.hasScrollbar && event.deltaY > 0) {
// this means user is trying to scroll vertically down
this.onScrollListener(event);
}
};

onScrollListener = (event: MouseEvent) => {
if (typeof this.props.onScroll === 'function') {
// Execute this callback in next tick so that it does not affect the
Expand Down