forgeplus-react/node_modules/ahooks/lib/useClickAway/index.js

42 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var react_1 = require("react");
var dom_1 = require("../utils/dom"); // 鼠标点击事件click 不会监听右键
var defaultEvent = 'click';
function useClickAway(onClickAway, target, eventName) {
if (eventName === void 0) {
eventName = defaultEvent;
}
var onClickAwayRef = react_1.useRef(onClickAway);
onClickAwayRef.current = onClickAway;
react_1.useEffect(function () {
var handler = function handler(event) {
var targets = Array.isArray(target) ? target : [target];
if (targets.some(function (targetItem) {
var targetElement = dom_1.getTargetElement(targetItem);
return !targetElement || (targetElement === null || targetElement === void 0 ? void 0 : targetElement.contains(event.target));
})) {
return;
}
onClickAwayRef.current(event);
};
document.addEventListener(eventName, handler);
return function () {
document.removeEventListener(eventName, handler);
};
}, [target, eventName]);
}
exports["default"] = useClickAway;