@yrobot/auto-scroll
Version:
This is a tool which makes scroll-container auto scroll to the bottom easy.
46 lines (45 loc) • 1.96 kB
TypeScript
type unObserverCallback = () => void;
type OnUnmount = (elm: Element) => void;
export type Context = {
escapeHook?: (elm: Element) => boolean;
onMount?: (elm: Element) => void | OnUnmount;
onUnmount?: OnUnmount;
};
export type Plugin<T = unknown> = (config?: T) => Context;
/**
* the auto-scroll plugin for escaping auto scroll down when user scroll up.
*
* @param {Object} config - The configs.
* @param {number} [config.threshold=24] - The threshold value for scroll up distance (default: 24).
* @param {number} [config.throttleTime=100] - The throttle time for scroll event (default: 100).
*
* @returns {Context} The generated state object. For autoScroll.param.plugins
*/
export declare const escapeWhenUpPlugin: Plugin<{
threshold?: number;
throttleTime?: number;
}>;
/**
* @description auto scroll the selector dom to the bottom, when the size of the selector dom has been updated.
*
* @param {Object} options - The config options for the autoScroll function.
* @param {string} options.selector - The selector for the container element. (example: '#container')
* @param {Context[]} [options.plugins] - The plugins for the life cycle hooks of the autoScroll function. [escapeHook,onMount,onUnmount]
* @param {number} [options.throttleTime=100] - The throttle time in milliseconds. default is 100. 0 for no throttle.
* @param {number} [options.offset=0] - The offset for the scroll position based on the container.scrollHeight.
*
* @return {function} The unObserverCallback function.
*
* @example autoScroll({ selector: "#scroll-container-id" })
* @example autoScroll({ selector: "#scroll-container-id", plugins: [escapeWhenUpPlugin()] })
*/
export default function autoScroll({ throttleTime, plugins, offset, ...res }: ({
selector: string;
} | {
container: HTMLElement | null;
}) & {
throttleTime?: number;
plugins?: ReturnType<Plugin>[];
offset?: number;
}): unObserverCallback;
export {};