'use strict'; var react = require('react'); function isBrowser() { return typeof window === 'object'; } function getScrollTop(target) { if (target) return target.scrollTop; return window.scrollY || document.body.scrollTop || document.documentElement.scrollTop || 0; } function getScrollLeft(target) { if (target) return target.scrollLeft; return window.scrollX || document.body.scrollLeft || document.documentElement.scrollLeft || 0; } function addScrollListener(listener, target) { if (target === void 0) { target = document; } return target.addEventListener('scroll', listener); } function removeScrollListener(listener, target) { if (target === void 0) { target = document; } return target.removeEventListener('scroll', listener); } function useScrollDir(target) { var _a = react.useState(), targetFromProps = _a[0], setTargetFromProps = _a[1]; var _b = react.useState(null), lastScrollDir = _b[0], setLastScrollDir = _b[1]; var _c = react.useState(null), scrollDir = _c[0], setScrollDir = _c[1]; react.useEffect(function () { return setTargetFromProps(target); }, [target]); var isScrolling = scrollDir !== null; var isScrollingY = scrollDir === 'UP' || scrollDir === 'DOWN'; var isScrollingX = scrollDir === 'LEFT' || scrollDir === 'RIGHT'; var isScrollingUp = scrollDir === 'UP'; var isScrollingLeft = scrollDir === 'LEFT'; var isScrollingDown = scrollDir === 'DOWN'; var isScrollingRight = scrollDir === 'RIGHT'; react.useEffect(function () { if (isBrowser()) { var scrollTimeout_1; var lastScrollTop_1 = getScrollTop(targetFromProps); var lastScrollLeft_1 = getScrollLeft(targetFromProps); var handleScroll_1 = function () { window.clearTimeout(scrollTimeout_1); scrollTimeout_1 = window.setTimeout(function () { setScrollDir(null); }, 100); var scrollTop = getScrollTop(targetFromProps); if (scrollTop < lastScrollTop_1) { setLastScrollDir('UP'); setScrollDir('UP'); } else if (scrollTop > lastScrollTop_1) { setLastScrollDir('DOWN'); setScrollDir('DOWN'); } lastScrollTop_1 = scrollTop; var scrollLeft = getScrollLeft(targetFromProps); if (scrollLeft < lastScrollLeft_1) { setLastScrollDir('LEFT'); setScrollDir('LEFT'); } else if (scrollLeft > lastScrollLeft_1) { setLastScrollDir('RIGHT'); setScrollDir('RIGHT'); } lastScrollLeft_1 = scrollLeft; }; addScrollListener(handleScroll_1, targetFromProps); return function () { return removeScrollListener(handleScroll_1, targetFromProps); }; } return undefined; }, [targetFromProps]); return { lastScrollDir: lastScrollDir, scrollDir: scrollDir, isScrolling: isScrolling, isScrollingY: isScrollingY, isScrollingX: isScrollingX, isScrollingUp: isScrollingUp, isScrollingLeft: isScrollingLeft, isScrollingDown: isScrollingDown, isScrollingRight: isScrollingRight, }; } exports.useScrollDir = useScrollDir;