UNPKG

tdesign-mobile-vue

Version:
72 lines (68 loc) 2.71 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import { useEventListener, unrefElement } from '@vueuse/core'; import isArray from 'lodash/isArray'; import { isBrowser } from '../util.js'; import '@babel/runtime/helpers/slicedToArray'; import 'lodash/isNumber'; import '../../config.js'; function useClickAway(target, fn) { var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; if (!isBrowser) return; var _options$eventName = options.eventName, eventName = _options$eventName === void 0 ? "touchstart" : _options$eventName, _options$capture = options.capture, capture = _options$capture === void 0 ? true : _options$capture, _options$ignore = options.ignore, ignore = _options$ignore === void 0 ? [] : _options$ignore, _options$detectIframe = options.detectIframe, detectIframe = _options$detectIframe === void 0 ? false : _options$detectIframe; var listenerOptions = { passive: true, capture: capture }; var checkElInEvtTarget = function checkElInEvtTarget(el, event) { return el && (event.target === el || event.composedPath().includes(el)); }; var shouldIgnore = function shouldIgnore(event) { return ignore.some(function (ignoreTarget) { return checkElInEvtTarget(unrefElement(ignoreTarget), event); }); }; var eventHandler = function eventHandler(event) { var targets = isArray(target) ? _toConsumableArray(target) : [target]; var shouldTrigger = targets.every(function (targetItem) { return !checkElInEvtTarget(unrefElement(targetItem), event); }); if (!shouldTrigger) return; shouldTrigger = !shouldIgnore(event); if (!shouldTrigger) return; fn(event); }; var cleanup = [useEventListener(window, eventName, eventHandler, listenerOptions)]; if (detectIframe) { cleanup.push(useEventListener(window, "blur", function (event) { setTimeout(function () { var targets = isArray(target) ? _toConsumableArray(target) : [target]; if (targets.every(function (targetItem) { var _window$document$acti; var el = unrefElement(targetItem); return ((_window$document$acti = window.document.activeElement) === null || _window$document$acti === void 0 ? void 0 : _window$document$acti.tagName) === "IFRAME" && !(el !== null && el !== void 0 && el.contains(window.document.activeElement)); })) { fn(event); } }, 0); })); } return function () { return cleanup.forEach(function (clean) { return clean(); }); }; } export { useClickAway }; //# sourceMappingURL=index.js.map