"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTippy = void 0; const lodash_castarray_1 = __importDefault(require("lodash.castarray")); const tippy_js_1 = __importDefault(require("tippy.js")); const vue_1 = require("vue"); /** * HACK: This hacky evaluation to tippy at runtime should just be * ```js * import tippy from 'tippy.js' * ``` * * However, there is a strange bug for some user apps, that will load * vue-use-tippy as ESM but tippy.js as CJS. If that happens, they * will see a error akin to "tippy is not a function". * * If tippy is not already (correctly) a function, we guess it might * be on `.default` */ const tippy = typeof tippy_js_1.default === 'function' ? tippy_js_1.default : tippy_js_1.default.default; const applyForEvery = (instance, callback) => { if (instance.value !== null) for (const tippyInstance of (0, lodash_castarray_1.default)(instance.value)) callback(tippyInstance); }; const unwrapAndThrowOnNull = (target) => { if (target.value === null) throw new Error('useTippy: Unexpected null target'); return target.value; }; /** * @see {@link https://atomiks.github.io/tippyjs/v6/all-props} for options */ const useTippy = (targets, options) => { const instance = (0, vue_1.ref)(null); const onMountCallbacks = []; (0, vue_1.onMounted)(() => { if (typeof targets === 'string') { instance.value = tippy(targets, options.value); } else if (Array.isArray(targets)) { const unwrappedTargets = targets.map((target) => unwrapAndThrowOnNull(target)); instance.value = tippy(unwrappedTargets, options.value); } else { instance.value = tippy(unwrapAndThrowOnNull(targets), options.value); } onMountCallbacks.forEach((callback) => { callback(instance.value); }); }); const onUnmountCallbacks = []; (0, vue_1.onUnmounted)(() => { applyForEvery(instance, (tippyInstance) => { tippyInstance.destroy(); }); onUnmountCallbacks.forEach((callback) => { callback(instance.value); }); }); (0, vue_1.watch)(options, () => { applyForEvery(instance, (tippyInstance) => { tippyInstance.setProps(options.value); }); }, { immediate: true, flush: 'post' }); return { onMount: (callback) => onMountCallbacks.push(callback), onUnmount: (callback) => onUnmountCallbacks.push(callback), tippy: instance, }; }; exports.useTippy = useTippy; //# sourceMappingURL=index.js.map