notification2.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { defineComponent, ref, computed, onMounted, openBlock, createBlock, Transition, unref, withCtx, withDirectives, createElementVNode, normalizeClass, normalizeStyle, resolveDynamicComponent, createCommentVNode, toDisplayString, renderSlot, createElementBlock, Fragment, vShow, withModifiers } from 'vue';
  2. import { useEventListener, useTimeoutFn } from '@vueuse/core';
  3. import { ElIcon } from '../../icon/index.mjs';
  4. import { notificationProps, notificationEmits } from './notification.mjs';
  5. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  6. import { useGlobalComponentSettings } from '../../config-provider/src/hooks/use-global-config.mjs';
  7. import { TypeComponentsMap } from '../../../utils/vue/icon.mjs';
  8. import { EVENT_CODE } from '../../../constants/aria.mjs';
  9. const __default__ = defineComponent({
  10. name: "ElNotification"
  11. });
  12. const _sfc_main = /* @__PURE__ */ defineComponent({
  13. ...__default__,
  14. props: notificationProps,
  15. emits: notificationEmits,
  16. setup(__props, { expose }) {
  17. const props = __props;
  18. const { ns, zIndex } = useGlobalComponentSettings("notification");
  19. const { nextZIndex, currentZIndex } = zIndex;
  20. const visible = ref(false);
  21. let timer = void 0;
  22. const typeClass = computed(() => {
  23. const type = props.type;
  24. return type && TypeComponentsMap[props.type] ? ns.m(type) : "";
  25. });
  26. const iconComponent = computed(() => {
  27. if (!props.type)
  28. return props.icon;
  29. return TypeComponentsMap[props.type] || props.icon;
  30. });
  31. const horizontalClass = computed(() => props.position.endsWith("right") ? "right" : "left");
  32. const verticalProperty = computed(() => props.position.startsWith("top") ? "top" : "bottom");
  33. const positionStyle = computed(() => {
  34. var _a;
  35. return {
  36. [verticalProperty.value]: `${props.offset}px`,
  37. zIndex: (_a = props.zIndex) != null ? _a : currentZIndex.value
  38. };
  39. });
  40. function startTimer() {
  41. if (props.duration > 0) {
  42. ({ stop: timer } = useTimeoutFn(() => {
  43. if (visible.value)
  44. close();
  45. }, props.duration));
  46. }
  47. }
  48. function clearTimer() {
  49. timer == null ? void 0 : timer();
  50. }
  51. function close() {
  52. visible.value = false;
  53. }
  54. function onKeydown({ code }) {
  55. if (code === EVENT_CODE.delete || code === EVENT_CODE.backspace) {
  56. clearTimer();
  57. } else if (code === EVENT_CODE.esc) {
  58. if (visible.value) {
  59. close();
  60. }
  61. } else {
  62. startTimer();
  63. }
  64. }
  65. onMounted(() => {
  66. startTimer();
  67. nextZIndex();
  68. visible.value = true;
  69. });
  70. useEventListener(document, "keydown", onKeydown);
  71. expose({
  72. visible,
  73. close
  74. });
  75. return (_ctx, _cache) => {
  76. return openBlock(), createBlock(Transition, {
  77. name: unref(ns).b("fade"),
  78. onBeforeLeave: _ctx.onClose,
  79. onAfterLeave: ($event) => _ctx.$emit("destroy"),
  80. persisted: ""
  81. }, {
  82. default: withCtx(() => [
  83. withDirectives(createElementVNode("div", {
  84. id: _ctx.id,
  85. class: normalizeClass([unref(ns).b(), _ctx.customClass, unref(horizontalClass)]),
  86. style: normalizeStyle(unref(positionStyle)),
  87. role: "alert",
  88. onMouseenter: clearTimer,
  89. onMouseleave: startTimer,
  90. onClick: _ctx.onClick
  91. }, [
  92. unref(iconComponent) ? (openBlock(), createBlock(unref(ElIcon), {
  93. key: 0,
  94. class: normalizeClass([unref(ns).e("icon"), unref(typeClass)])
  95. }, {
  96. default: withCtx(() => [
  97. (openBlock(), createBlock(resolveDynamicComponent(unref(iconComponent))))
  98. ]),
  99. _: 1
  100. }, 8, ["class"])) : createCommentVNode("v-if", true),
  101. createElementVNode("div", {
  102. class: normalizeClass(unref(ns).e("group"))
  103. }, [
  104. createElementVNode("h2", {
  105. class: normalizeClass(unref(ns).e("title")),
  106. textContent: toDisplayString(_ctx.title)
  107. }, null, 10, ["textContent"]),
  108. withDirectives(createElementVNode("div", {
  109. class: normalizeClass(unref(ns).e("content")),
  110. style: normalizeStyle(!!_ctx.title ? void 0 : { margin: 0 })
  111. }, [
  112. renderSlot(_ctx.$slots, "default", {}, () => [
  113. !_ctx.dangerouslyUseHTMLString ? (openBlock(), createElementBlock("p", { key: 0 }, toDisplayString(_ctx.message), 1)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
  114. createCommentVNode(" Caution here, message could've been compromised, never use user's input as message "),
  115. createElementVNode("p", { innerHTML: _ctx.message }, null, 8, ["innerHTML"])
  116. ], 2112))
  117. ])
  118. ], 6), [
  119. [vShow, _ctx.message]
  120. ]),
  121. _ctx.showClose ? (openBlock(), createBlock(unref(ElIcon), {
  122. key: 0,
  123. class: normalizeClass(unref(ns).e("closeBtn")),
  124. onClick: withModifiers(close, ["stop"])
  125. }, {
  126. default: withCtx(() => [
  127. (openBlock(), createBlock(resolveDynamicComponent(_ctx.closeIcon)))
  128. ]),
  129. _: 1
  130. }, 8, ["class", "onClick"])) : createCommentVNode("v-if", true)
  131. ], 2)
  132. ], 46, ["id", "onClick"]), [
  133. [vShow, visible.value]
  134. ])
  135. ]),
  136. _: 3
  137. }, 8, ["name", "onBeforeLeave", "onAfterLeave"]);
  138. };
  139. }
  140. });
  141. var NotificationConstructor = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "notification.vue"]]);
  142. export { NotificationConstructor as default };
  143. //# sourceMappingURL=notification2.mjs.map