UNPKG

@opensig/opendesign

Version:

78 lines (77 loc) 2.28 kB
import { defineComponent, ref, computed, onMounted, onUnmounted, createElementBlock, createCommentVNode, openBlock, renderSlot, createTextVNode, toDisplayString } from "vue"; import { isUndefined, isFunction } from "../_utils/is.mjs"; import { toastProps } from "./types.mjs"; const _hoisted_1 = { key: 0, class: "o-toast" }; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "OToast", props: toastProps, emits: ["duration-end", "close", "update:visible"], setup(__props, { expose: __expose, emit: __emit }) { const props = __props; const innerIsVisible = ref(props.visible ?? props.defaultVisible); const isVisible = computed(() => props.visible ?? innerIsVisible.value); const emits = __emit; let timer = 0; const clearTimer = () => { if (timer) { window.clearTimeout(timer); timer = 0; } }; const startTimer = () => { if (isUndefined(props.duration) || props.duration <= 0) { return; } timer = window.setTimeout(() => { emits("duration-end"); innerIsVisible.value = false; emits("update:visible", innerIsVisible.value); clearTimer(); }, props.duration); }; const onClose = async (ev) => { ev == null ? void 0 : ev.stopPropagation(); if (isFunction(props.beforeClose)) { const rlt = await props.beforeClose(); if (rlt) { innerIsVisible.value = false; emits("update:visible", innerIsVisible.value); emits("close", ev); return; } } innerIsVisible.value = false; emits("update:visible", innerIsVisible.value); emits("close", ev); }; onMounted(() => { startTimer(); }); onUnmounted(() => { clearTimer(); }); __expose({ /** * 关闭即时反馈 */ close: onClose }); return (_ctx, _cache) => { return isVisible.value ? (openBlock(), createElementBlock("div", _hoisted_1, [ renderSlot(_ctx.$slots, "default", {}, () => [ createTextVNode( toDisplayString(_ctx.message), 1 /* TEXT */ ) ]) ])) : createCommentVNode("v-if", true); }; } }); export { _sfc_main as default };