@os-design/use-swipe
Advanced tools
Comparing version
@@ -7,40 +7,30 @@ "use strict"; | ||
exports["default"] = void 0; | ||
var _react = require("react"); | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var useSwipe = function useSwipe() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var _options$direction = options.direction, | ||
direction = _options$direction === void 0 ? 'left' : _options$direction, | ||
_options$threshold = options.threshold, | ||
threshold = _options$threshold === void 0 ? 30 : _options$threshold; | ||
direction = _options$direction === void 0 ? 'left' : _options$direction, | ||
_options$threshold = options.threshold, | ||
threshold = _options$threshold === void 0 ? 30 : _options$threshold; | ||
var thresholdRef = (0, _react.useRef)(threshold); | ||
var startCoordsRef = (0, _react.useRef)(null); | ||
var _useState = (0, _react.useState)(false), | ||
_useState2 = _slicedToArray(_useState, 2), | ||
opened = _useState2[0], | ||
setOpened = _useState2[1]; | ||
_useState2 = _slicedToArray(_useState, 2), | ||
opened = _useState2[0], | ||
setOpened = _useState2[1]; | ||
(0, _react.useEffect)(function () { | ||
thresholdRef.current = threshold; | ||
}, [threshold]); // Set start coordinates | ||
}, [threshold]); | ||
// Set start coordinates | ||
var onTouchStart = (0, _react.useCallback)(function (e) { | ||
var _e$changedTouches$ite = e.changedTouches.item(0), | ||
clientX = _e$changedTouches$ite.clientX, | ||
clientY = _e$changedTouches$ite.clientY; | ||
clientX = _e$changedTouches$ite.clientX, | ||
clientY = _e$changedTouches$ite.clientY; | ||
startCoordsRef.current = { | ||
@@ -50,24 +40,20 @@ x: clientX, | ||
}; | ||
}, []); // If the difference between the start and current coordinates exceeds the | ||
}, []); | ||
// If the difference between the start and current coordinates exceeds the | ||
// threshold value, update the opened status and resave the start coordinates. | ||
var onTouchMove = (0, _react.useCallback)(function (e) { | ||
var current = startCoordsRef.current; | ||
if (!current) return; | ||
var _e$changedTouches$ite2 = e.changedTouches.item(0), | ||
clientX = _e$changedTouches$ite2.clientX, | ||
clientY = _e$changedTouches$ite2.clientY; | ||
clientX = _e$changedTouches$ite2.clientX, | ||
clientY = _e$changedTouches$ite2.clientY; | ||
var diffX = Math.abs(clientX - current.x); | ||
var diffY = Math.abs(clientY - current.y); | ||
if (['left', 'right'].includes(direction) && diffX < thresholdRef.current) { | ||
return; | ||
} | ||
if (['top', 'bottom'].includes(direction) && diffY < thresholdRef.current) { | ||
return; | ||
} | ||
if (direction === 'left') setOpened(clientX < current.x); | ||
@@ -81,4 +67,5 @@ if (direction === 'right') setOpened(clientX > current.x); | ||
}; | ||
}, [direction]); // Reset start coordinates | ||
}, [direction]); | ||
// Reset start coordinates | ||
var onTouchEnd = (0, _react.useCallback)(function () { | ||
@@ -96,5 +83,4 @@ startCoordsRef.current = null; | ||
}; | ||
var _default = useSwipe; | ||
exports["default"] = _default; | ||
//# sourceMappingURL=index.js.map |
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
const useSwipe = (options = {}) => { | ||
@@ -13,4 +12,5 @@ const { | ||
thresholdRef.current = threshold; | ||
}, [threshold]); // Set start coordinates | ||
}, [threshold]); | ||
// Set start coordinates | ||
const onTouchStart = useCallback(e => { | ||
@@ -25,5 +25,6 @@ const { | ||
}; | ||
}, []); // If the difference between the start and current coordinates exceeds the | ||
}, []); | ||
// If the difference between the start and current coordinates exceeds the | ||
// threshold value, update the opened status and resave the start coordinates. | ||
const onTouchMove = useCallback(e => { | ||
@@ -40,11 +41,8 @@ const { | ||
const diffY = Math.abs(clientY - current.y); | ||
if (['left', 'right'].includes(direction) && diffX < thresholdRef.current) { | ||
return; | ||
} | ||
if (['top', 'bottom'].includes(direction) && diffY < thresholdRef.current) { | ||
return; | ||
} | ||
if (direction === 'left') setOpened(clientX < current.x); | ||
@@ -58,4 +56,5 @@ if (direction === 'right') setOpened(clientX > current.x); | ||
}; | ||
}, [direction]); // Reset start coordinates | ||
}, [direction]); | ||
// Reset start coordinates | ||
const onTouchEnd = useCallback(() => { | ||
@@ -73,4 +72,3 @@ startCoordsRef.current = null; | ||
}; | ||
export default useSwipe; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@os-design/use-swipe", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"license": "UNLICENSED", | ||
@@ -34,3 +34,3 @@ "repository": "git@gitlab.com:os-team/libs/os-design.git", | ||
}, | ||
"gitHead": "174987fc6c9d55db201be10abde3a4cf5a790573" | ||
"gitHead": "8cb28f6719d699c014fbce91d832a9ff06abe515" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Explicitly Unlicensed Item
License(Experimental) Something was found which is explicitly marked as unlicensed.
Found 1 instance in 1 package
Explicitly Unlicensed Item
License(Experimental) Something was found which is explicitly marked as unlicensed.
Found 1 instance in 1 package
160
3.9%18475
-0.81%