UNPKG

element-plus

Version:

A Component Library for Vue 3

181 lines (178 loc) 6.76 kB
import { defineComponent, ref, computed, onMounted, openBlock, createBlock, Transition, unref, withCtx, withDirectives, createElementVNode, normalizeClass, normalizeStyle, resolveDynamicComponent, createCommentVNode, toDisplayString, renderSlot, createElementBlock, Fragment, vShow, withModifiers } from 'vue'; import { useEventListener, useTimeoutFn } from '@vueuse/core'; import { ElIcon } from '../../icon/index.mjs'; import { notificationProps, notificationEmits } from './notification.mjs'; import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs'; import { useGlobalComponentSettings } from '../../config-provider/src/hooks/use-global-config.mjs'; import { TypeComponentsMap } from '../../../utils/vue/icon.mjs'; import { getEventCode } from '../../../utils/dom/event.mjs'; import { EVENT_CODE } from '../../../constants/aria.mjs'; const _hoisted_1 = ["id"]; const _hoisted_2 = ["textContent"]; const _hoisted_3 = { key: 0 }; const _hoisted_4 = ["innerHTML"]; const _sfc_main = defineComponent({ ...{ name: "ElNotification" }, __name: "notification", props: notificationProps, emits: notificationEmits, setup(__props, { expose: __expose }) { const props = __props; const { ns, zIndex } = useGlobalComponentSettings("notification"); const { nextZIndex, currentZIndex } = zIndex; const visible = ref(false); let timer = void 0; const typeClass = computed(() => { const type = props.type; return type && TypeComponentsMap[props.type] ? ns.m(type) : ""; }); const iconComponent = computed(() => { if (!props.type) return props.icon; return TypeComponentsMap[props.type] || props.icon; }); const horizontalClass = computed( () => props.position.endsWith("right") ? "right" : "left" ); const verticalProperty = computed( () => props.position.startsWith("top") ? "top" : "bottom" ); const positionStyle = computed(() => { var _a; return { [verticalProperty.value]: `${props.offset}px`, zIndex: (_a = props.zIndex) != null ? _a : currentZIndex.value }; }); function startTimer() { if (props.duration > 0) { ({ stop: timer } = useTimeoutFn(() => { if (visible.value) close(); }, props.duration)); } } function clearTimer() { timer == null ? void 0 : timer(); } function close() { visible.value = false; } function onKeydown(event) { const code = getEventCode(event); switch (code) { case EVENT_CODE.delete: case EVENT_CODE.backspace: clearTimer(); break; case EVENT_CODE.esc: if (visible.value) { close(); } break; default: startTimer(); break; } } onMounted(() => { startTimer(); nextZIndex(); visible.value = true; }); useEventListener(document, "keydown", onKeydown); __expose({ visible, close }); return (_ctx, _cache) => { return openBlock(), createBlock(Transition, { name: unref(ns).b("fade"), onBeforeLeave: _ctx.onClose, onAfterLeave: _cache[1] || (_cache[1] = ($event) => _ctx.$emit("destroy")), persisted: "" }, { default: withCtx(() => [ withDirectives(createElementVNode("div", { id: _ctx.id, class: normalizeClass([unref(ns).b(), _ctx.customClass, horizontalClass.value]), style: normalizeStyle(positionStyle.value), role: "alert", onMouseenter: clearTimer, onMouseleave: startTimer, onClick: _cache[0] || (_cache[0] = (...args) => _ctx.onClick && _ctx.onClick(...args)) }, [ iconComponent.value ? (openBlock(), createBlock(unref(ElIcon), { key: 0, class: normalizeClass([unref(ns).e("icon"), typeClass.value]) }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(iconComponent.value))) ]), _: 1 }, 8, ["class"])) : createCommentVNode("v-if", true), createElementVNode( "div", { class: normalizeClass(unref(ns).e("group")) }, [ createElementVNode("h2", { class: normalizeClass(unref(ns).e("title")), textContent: toDisplayString(_ctx.title) }, null, 10, _hoisted_2), withDirectives(createElementVNode( "div", { class: normalizeClass(unref(ns).e("content")), style: normalizeStyle(!!_ctx.title ? void 0 : { margin: 0 }) }, [ renderSlot(_ctx.$slots, "default", {}, () => [ !_ctx.dangerouslyUseHTMLString ? (openBlock(), createElementBlock( "p", _hoisted_3, toDisplayString(_ctx.message), 1 )) : (openBlock(), createElementBlock( Fragment, { key: 1 }, [ createCommentVNode(" Caution here, message could've been compromised, never use user's input as message "), createElementVNode("p", { innerHTML: _ctx.message }, null, 8, _hoisted_4) ], 2112 )) ]) ], 6 ), [ [vShow, _ctx.message] ]), _ctx.showClose ? (openBlock(), createBlock(unref(ElIcon), { key: 0, class: normalizeClass(unref(ns).e("closeBtn")), onClick: withModifiers(close, ["stop"]) }, { default: withCtx(() => [ (openBlock(), createBlock(resolveDynamicComponent(_ctx.closeIcon))) ]), _: 1 }, 8, ["class"])) : createCommentVNode("v-if", true) ], 2 ) ], 46, _hoisted_1), [ [vShow, visible.value] ]) ]), _: 3 }, 8, ["name", "onBeforeLeave"]); }; } }); var NotificationConstructor = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/notification/src/notification.vue"]]); export { NotificationConstructor as default }; //# sourceMappingURL=notification2.mjs.map