@orca-fe/hooks
Version:
React Hooks Collections
29 lines • 989 B
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useEventListener } from 'ahooks';
import { useRef } from 'react';
import useMemorizedFn from "./useMemorizedFn";
export default function useStaticClick(_callback, options = {}) {
var mousePointRef = useRef([0, 0]);
var _options$distance = options.distance,
distance = _options$distance === void 0 ? 2 : _options$distance,
target = options.target;
var callback = useMemorizedFn(_callback);
useEventListener('mousedown', e => {
mousePointRef.current = [e.clientX, e.clientY];
}, {
target
});
useEventListener('click', e => {
if (mousePointRef.current) {
var _mousePointRef$curren = _slicedToArray(mousePointRef.current, 2),
x = _mousePointRef$curren[0],
y = _mousePointRef$curren[1];
var d = Math.pow(x - e.clientX, 2) + Math.pow(y - e.clientY, 2);
if (d <= Math.pow(distance, 2)) {
callback(e);
}
}
}, {
target
});
}