@elastic/eui
Version:
Elastic UI Component Library
48 lines (47 loc) • 2.1 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { useEffect } from 'react';
import { throttle } from '../../services/throttle';
export function isMouseEvent(event) {
return _typeof(event) === 'object' && 'pageX' in event && 'pageY' in event;
}
export function useMouseMove(handleChange) {
var interactionConditional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
useEffect(function () {
return unbindEventListeners;
}, []); // eslint-disable-line react-hooks/exhaustive-deps
var handleInteraction = function handleInteraction(e, isFirstInteraction) {
if (e) {
if (interactionConditional) {
var x = isMouseEvent(e) ? e.pageX : e.touches[0].pageX;
var y = isMouseEvent(e) ? e.pageY : e.touches[0].pageY;
handleChange({
x: x,
y: y
}, isFirstInteraction);
}
}
};
var handleMouseMove = throttle(function (e) {
handleChange({
x: e.pageX,
y: e.pageY
}, false);
});
var handleMouseDown = function handleMouseDown(e) {
handleInteraction(e, true);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', unbindEventListeners);
};
var unbindEventListeners = function unbindEventListeners() {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', unbindEventListeners);
};
return [handleMouseDown, handleInteraction];
}