UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 11 kB
{"version":3,"file":"notify.mjs","sources":["../../../../../../packages/components/notification/src/notify.ts"],"sourcesContent":["import { createVNode, isVNode, render } from 'vue'\nimport {\n debugWarn,\n isClient,\n isElement,\n isFunction,\n isString,\n isUndefined,\n} from '@element-plus/utils'\nimport NotificationConstructor from './notification.vue'\nimport { notificationTypes } from './notification'\n\nimport type { Ref, VNode } from 'vue'\nimport type {\n NotificationOptions,\n NotificationProps,\n NotificationQueue,\n Notify,\n NotifyFn,\n} from './notification'\n\n// This should be a queue but considering there were `non-autoclosable` notifications.\nconst notifications: Record<\n NotificationOptions['position'],\n NotificationQueue\n> = {\n 'top-left': [],\n 'top-right': [],\n 'bottom-left': [],\n 'bottom-right': [],\n}\n\n// the gap size between each notification\nconst GAP_SIZE = 16\nlet seed = 1\n\nconst notify: NotifyFn & Partial<Notify> = function (options = {}, context) {\n if (!isClient) return { close: () => undefined }\n\n if (isString(options) || isVNode(options)) {\n options = { message: options }\n }\n\n const position = options.position || 'top-right'\n\n let verticalOffset = options.offset || 0\n notifications[position].forEach(({ vm }) => {\n verticalOffset += (vm.el?.offsetHeight || 0) + GAP_SIZE\n })\n verticalOffset += GAP_SIZE\n\n const id = `notification_${seed++}`\n const userOnClose = options.onClose\n const props: Partial<NotificationProps> = {\n ...options,\n offset: verticalOffset,\n id,\n onClose: () => {\n close(id, position, userOnClose)\n },\n }\n\n let appendTo: HTMLElement | null = document.body\n if (isElement(options.appendTo)) {\n appendTo = options.appendTo\n } else if (isString(options.appendTo)) {\n appendTo = document.querySelector(options.appendTo)\n }\n\n // should fallback to default value with a warning\n if (!isElement(appendTo)) {\n debugWarn(\n 'ElNotification',\n 'the appendTo option is not an HTMLElement. Falling back to document.body.'\n )\n appendTo = document.body\n }\n\n const container = document.createElement('div')\n\n const vm = createVNode(\n NotificationConstructor,\n props,\n isFunction(props.message)\n ? props.message\n : isVNode(props.message)\n ? () => props.message\n : null\n )\n vm.appContext = isUndefined(context) ? notify._context : context\n\n // clean notification element preventing mem leak\n vm.props!.onDestroy = () => {\n render(null, container)\n }\n\n // instances will remove this item when close function gets called. So we do not need to worry about it.\n render(vm, container)\n notifications[position].push({ vm })\n appendTo.appendChild(container.firstElementChild!)\n\n return {\n // instead of calling the onClose function directly, setting this value so that we can have the full lifecycle\n // for out component, so that all closing steps will not be skipped.\n close: () => {\n ;(vm.component!.exposed as { visible: Ref<boolean> }).visible.value =\n false\n },\n }\n}\nnotificationTypes.forEach((type) => {\n notify[type] = (options = {}, appContext) => {\n if (isString(options) || isVNode(options)) {\n options = {\n message: options,\n }\n }\n return notify({ ...options, type }, appContext)\n }\n})\n\n/**\n * This function gets called when user click `x` button or press `esc` or the time reached its limitation.\n * Emitted by transition@before-leave event so that we can fetch the current notification.offsetHeight, if this was called\n * by @after-leave the DOM element will be removed from the page thus we can no longer fetch the offsetHeight.\n * @param {String} id notification id to be closed\n * @param {Position} position the positioning strategy\n * @param {Function} userOnClose the callback called when close passed by user\n */\nexport function close(\n id: string,\n position: NotificationOptions['position'],\n userOnClose?: (vm: VNode) => void\n): void {\n // maybe we can store the index when inserting the vm to notification list.\n const orientedNotifications = notifications[position]\n const idx = orientedNotifications.findIndex(\n ({ vm }) => vm.component?.props.id === id\n )\n if (idx === -1) return\n const { vm } = orientedNotifications[idx]\n if (!vm) return\n // calling user's on close function before notification gets removed from DOM.\n userOnClose?.(vm)\n\n // note that this is called @before-leave, that's why we were able to fetch this property.\n const removedHeight = vm.el!.offsetHeight\n const verticalPos = position.split('-')[0]\n orientedNotifications.splice(idx, 1)\n const len = orientedNotifications.length\n if (len < 1) return\n // starting from the removing item.\n for (let i = idx; i < len; i++) {\n // new position equals the current offsetTop minus removed height plus 16px(the gap size between each item)\n const { el, component } = orientedNotifications[i].vm\n const pos =\n Number.parseInt(el!.style[verticalPos], 10) - removedHeight - GAP_SIZE\n component!.props.offset = pos\n }\n}\n\nexport function closeAll(): void {\n // loop through all directions, close them at once.\n for (const orientedNotifications of Object.values(notifications)) {\n orientedNotifications.forEach(({ vm }) => {\n // same as the previous close method, we'd like to make sure lifecycle gets handle properly.\n ;(vm.component!.exposed as { visible: Ref<boolean> }).visible.value =\n false\n })\n }\n}\n\nexport function updateOffsets(\n position: NotificationOptions['position'] = 'top-right'\n) {\n let verticalOffset =\n notifications[position][0]?.vm.component?.props?.offset || 0\n\n for (const { vm } of notifications[position]) {\n vm.component!.props.offset = verticalOffset\n verticalOffset += (vm.el?.offsetHeight || 0) + GAP_SIZE\n }\n}\n\nnotify.closeAll = closeAll\nnotify.updateOffsets = updateOffsets\nnotify._context = null\n\nexport default notify as Notify\n"],"names":["vm"],"mappings":";;;;;;;;AAsBA,MAAM,aAGF,GAAA;AAAA,EACF,YAAY,EAAC;AAAA,EACb,aAAa,EAAC;AAAA,EACd,eAAe,EAAC;AAAA,EAChB,gBAAgB,EAAC;AACnB,CAAA,CAAA;AAGA,MAAM,QAAW,GAAA,EAAA,CAAA;AACjB,IAAI,IAAO,GAAA,CAAA,CAAA;AAEX,MAAM,MAAqC,GAAA,SAAU,OAAU,GAAA,IAAI,OAAS,EAAA;AAC1E,EAAA,IAAI,CAAC,QAAA;AAAU,IAAO,OAAA,EAAE,KAAO,EAAA,MAAM,KAAU,CAAA,EAAA,CAAA;AAE/C,EAAA,IAAI,QAAS,CAAA,OAAO,CAAK,IAAA,OAAA,CAAQ,OAAO,CAAG,EAAA;AACzC,IAAU,OAAA,GAAA,EAAE,SAAS,OAAQ,EAAA,CAAA;AAAA,GAC/B;AAEA,EAAM,MAAA,QAAA,GAAW,QAAQ,QAAY,IAAA,WAAA,CAAA;AAErC,EAAI,IAAA,cAAA,GAAiB,QAAQ,MAAU,IAAA,CAAA,CAAA;AACvC,EAAA,aAAA,CAAc,UAAU,OAAQ,CAAA,CAAC,EAAE,EAAA,EAAAA,KAAS,KAAA;AA9C9C,IAAA,IAAA,EAAA,CAAA;AA+CI,IAAA,cAAA,IAAA,CAAA,CAAA,CAAmB,EAAAA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