yuang-framework-ui-pc
Version:
yuang-framework-ui-pc Library
109 lines (108 loc) • 2.71 kB
JavaScript
import { defineComponent, ref, watch, onMounted, onBeforeUnmount, unref, openBlock, createElementBlock } from "vue";
import { CountUp } from "countup.js";
import { countUpProps, countUpEmits } from "./props";
const isFunction = (value) => typeof value === "function";
const _sfc_main = defineComponent({
name: "EleCountUp",
props: countUpProps,
emits: countUpEmits,
setup(props, { emit }) {
let instance;
const rootRef = ref(null);
const create = () => {
const elem = unref(rootRef);
if (instance || !elem) {
return;
}
const ins = new CountUp(elem, props.endVal ?? 0, props.options);
if (ins.error) {
console.warn(ins);
return;
}
instance = ins;
};
const printValue = (value) => {
if (instance && isFunction(instance.printValue)) {
return instance.printValue(value);
}
};
const start = (callback) => {
if (instance && isFunction(instance.start)) {
return instance.start(callback);
}
};
const pauseResume = () => {
if (instance && isFunction(instance.pauseResume)) {
return instance.pauseResume();
}
};
const reset = () => {
if (instance && isFunction(instance.reset)) {
return instance.reset();
}
};
const update = (newEndVal) => {
if (instance && isFunction(instance.update)) {
return instance.update(newEndVal ?? 0);
}
};
const destroy = () => {
reset();
instance = null;
};
watch(
() => props.endVal,
(value) => {
update(value);
}
);
watch(
() => props.options,
() => {
destroy();
create();
emit("ready", instance);
}
);
onMounted(() => {
create();
if (props.delay < 0) {
emit("ready", instance);
return;
}
setTimeout(() => {
if (instance && isFunction(instance.start)) {
instance.start(() => {
emit("ready", instance);
});
}
}, props.delay);
});
onBeforeUnmount(() => {
destroy();
});
return {
rootRef,
printValue,
start,
pauseResume,
reset,
update
};
}
});
const _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
const _hoisted_1 = { ref: "rootRef" };
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return openBlock(), createElementBlock("span", _hoisted_1, null, 512);
}
const index = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export {
index as default
};