react-bottom-scroll-listener
Version:
A simple React component that lets you listen for when you have scrolled to the bottom.
23 lines (22 loc) • 1.33 kB
TypeScript
import lodashDebounce from 'lodash.debounce';
import { type RefObject } from 'react';
export type DebounceOptions = Parameters<typeof lodashDebounce>[2];
/**
* @description
* A react hook that invokes a callback when user scrolls to the bottom
*
* @param onBottom Required callback that will be invoked when scrolled to bottom
* @param {Object} options - Optional parameters
* @param {number} [options.offset=0] - Offset from bottom of page in pixels. E.g. 300 will trigger onBottom 300px from the bottom of the page
* @param {number} [options.debounce=200] - Optional debounce in milliseconds, defaults to 200ms
* @param {DebounceOptions} [options.debounceOptions={leading=true}] - Options passed to lodash.debounce, see https://lodash.com/docs/4.17.15#debounce
* @param {boolean} [options.triggerOnNoScroll=false] - Triggers the onBottom callback when the page has no scrollbar
* @returns {RefObject} ref - If passed to a element as a ref, e.g. a div it will register scrolling to the bottom of that div instead of document viewport
*/
declare function useBottomScrollListener<T extends HTMLElement>(onBottom: () => void, options?: {
offset?: number;
debounce?: number;
debounceOptions?: DebounceOptions;
triggerOnNoScroll?: boolean;
}): RefObject<T | null>;
export default useBottomScrollListener;