"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useMobileTouchMove; var React = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var SMOOTH_PTG = 14 / 15; function useMobileTouchMove(inVirtual, listRef, callback) { var touchedRef = (0, React.useRef)(false); var touchYRef = (0, React.useRef)(0); var elementRef = (0, React.useRef)(null); // Smooth scroll var intervalRef = (0, React.useRef)(null); /* eslint-disable prefer-const */ var cleanUpEvents; var onTouchMove = function onTouchMove(e) { if (touchedRef.current) { var currentY = Math.ceil(e.touches[0].pageY); var offsetY = touchYRef.current - currentY; touchYRef.current = currentY; if (callback(offsetY)) { e.preventDefault(); } // Smooth interval clearInterval(intervalRef.current); intervalRef.current = setInterval(function () { offsetY *= SMOOTH_PTG; if (!callback(offsetY, true) || Math.abs(offsetY) <= 0.1) { clearInterval(intervalRef.current); } }, 16); } }; var onTouchEnd = function onTouchEnd() { touchedRef.current = false; cleanUpEvents(); }; var onTouchStart = function onTouchStart(e) { cleanUpEvents(); if (e.touches.length === 1 && !touchedRef.current) { touchedRef.current = true; touchYRef.current = Math.ceil(e.touches[0].pageY); elementRef.current = e.target; elementRef.current.addEventListener('touchmove', onTouchMove); elementRef.current.addEventListener('touchend', onTouchEnd); } }; cleanUpEvents = function cleanUpEvents() { if (elementRef.current) { elementRef.current.removeEventListener('touchmove', onTouchMove); elementRef.current.removeEventListener('touchend', onTouchEnd); } }; React.useLayoutEffect(function () { if (inVirtual) { listRef.current.addEventListener('touchstart', onTouchStart); } return function () { listRef.current.removeEventListener('touchstart', onTouchStart); cleanUpEvents(); clearInterval(intervalRef.current); }; }, [inVirtual]); }