UNPKG

@n0n3br/react-use-scroll-direction

Version:

A robust React hook to detect vertical scroll direction ('up', 'down', 'static') for any DOM element or the window.

97 lines (95 loc) 3.51 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { useScrollDirection: () => useScrollDirection }); module.exports = __toCommonJS(src_exports); var import_react = require("react"); function useScrollDirection(ref, options) { const { threshold = 0, throttleDelay = 100 } = options || {}; const [scrollDirection, setScrollDirection] = (0, import_react.useState)("static"); const lastScrollPosition = (0, import_react.useRef)(0); const throttleTimeoutId = (0, import_react.useRef)(null); const getScrollY = (0, import_react.useCallback)(() => { if (ref && ref.current) { return ref.current.scrollTop; } return window.scrollY; }, [ref]); (0, import_react.useEffect)(() => { const targetElement = ref && ref.current ? ref.current : window; if (!targetElement) return; lastScrollPosition.current = getScrollY(); const handleScroll = () => { const currentScrollPosition = getScrollY(); const scrollDifference = currentScrollPosition - lastScrollPosition.current; if (Math.abs(scrollDifference) <= threshold) { return; } if (scrollDifference > 0) { setScrollDirection("down"); } else { setScrollDirection("up"); } lastScrollPosition.current = currentScrollPosition; }; const throttledHandleScroll = () => { if (throttleTimeoutId.current === null) { throttleTimeoutId.current = window.requestAnimationFrame(() => { handleScroll(); throttleTimeoutId.current = null; }); } }; const throttledHandleScrollWithDelay = () => { if (throttleTimeoutId.current === null) { throttleTimeoutId.current = window.setTimeout(() => { handleScroll(); throttleTimeoutId.current = null; }, throttleDelay); } }; const effectiveThrottledScrollHandler = throttleDelay > 0 && throttleDelay !== 100 ? throttledHandleScrollWithDelay : throttledHandleScroll; targetElement.addEventListener("scroll", effectiveThrottledScrollHandler, { passive: true }); return () => { targetElement.removeEventListener( "scroll", effectiveThrottledScrollHandler ); if (throttleTimeoutId.current !== null) { if (throttleDelay > 0 && throttleDelay !== 100) { clearTimeout(throttleTimeoutId.current); } else { cancelAnimationFrame(throttleTimeoutId.current); } throttleTimeoutId.current = null; } }; }, [ref, threshold, throttleDelay, getScrollY]); return scrollDirection; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useScrollDirection });