UNPKG

@extclp/vexip-ui

Version:

A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good

1 lines 9.99 kB
{"version":3,"file":"index.mjs","sources":["../../../components/toast/index.ts"],"sourcesContent":["import { createApp, createVNode, markRaw, render } from 'vue'\r\n\r\nimport Component from './toast.vue'\r\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\r\nimport { destroyObject, isClient, noop, toNumber } from '@vexip-ui/utils'\r\n\r\nimport type { App, MaybeRef } from 'vue'\r\nimport type { MaybeInstance } from '@vexip-ui/hooks'\r\nimport type { ToastInstance, ToastOptions, ToastType } from './symbol'\r\n\r\nexport { toastProps } from './props'\r\n\r\nexport type { ToastProps, ToastCProps } from './props'\r\nexport type { ToastType, ToastOptions }\r\n\r\ntype FuzzyOptions = string | ToastOptions\r\n\r\ninterface AipMethod {\r\n (options: ToastOptions): () => void,\r\n (content: string, duration?: number): () => void,\r\n /** @internal */\r\n (options: FuzzyOptions, duration?: number): () => void,\r\n}\r\n\r\nconst conveniences: Record<ToastType, Record<string, any>> = {\r\n success: {},\r\n warning: {},\r\n error: {},\r\n loading: {\r\n showMask: true,\r\n },\r\n}\r\n\r\nexport class ToastManager {\r\n name: string\r\n defaults: Record<string, unknown>\r\n\r\n open: AipMethod\r\n success: AipMethod\r\n warning: AipMethod\r\n error: AipMethod\r\n loading: AipMethod\r\n\r\n private _mountedApp: App<unknown> | null\r\n private _instance: ToastInstance | null\r\n private _innerApp: App<unknown> | null\r\n private _container: HTMLElement | null\r\n private _timer: ReturnType<typeof setTimeout> | null\r\n private _wrapper: HTMLElement | SVGElement | null\r\n private _mountedEl: HTMLElement | null\r\n\r\n constructor(options: Partial<ToastOptions> = {}) {\r\n options = {\r\n ...options,\r\n duration: options.duration ? toNumber(options.duration) : 2000,\r\n }\r\n\r\n this._mountedApp = null\r\n this._instance = null\r\n this._innerApp = null\r\n this._container = null\r\n this._timer = null\r\n this._wrapper = null\r\n this._mountedEl = null\r\n this.name = 'Toast'\r\n this.defaults = {}\r\n\r\n this.config(options)\r\n\r\n this.open = (content: FuzzyOptions, duration?: number) => {\r\n return this._open(null, content, duration)\r\n }\r\n\r\n this.success = (content: FuzzyOptions, duration?: number) => {\r\n return this._open('success', content, duration)\r\n }\r\n\r\n this.warning = (content: FuzzyOptions, duration?: number) => {\r\n return this._open('warning', content, duration)\r\n }\r\n\r\n this.error = (content: FuzzyOptions, duration?: number) => {\r\n return this._open('error', content, duration)\r\n }\r\n\r\n this.loading = (content: FuzzyOptions, duration?: number) => {\r\n return this._open('loading', content, duration)\r\n }\r\n }\r\n\r\n close() {\r\n this._timer && clearTimeout(this._timer)\r\n this._getInstance()?.closeToast()\r\n }\r\n\r\n config(options: Record<string, unknown>) {\r\n this.defaults = { ...this.defaults, ...options }\r\n }\r\n\r\n clone() {\r\n const manager = new ToastManager(this.defaults)\r\n\r\n manager._mountedApp = this._mountedApp\r\n\r\n return manager\r\n }\r\n\r\n destroy() {\r\n this._mountedEl && this._wrapper?.removeChild(this._mountedEl)\r\n this._innerApp?.unmount()\r\n this._container && render(null, this._container)\r\n destroyObject(this)\r\n }\r\n\r\n isDestroyed() {\r\n return false\r\n }\r\n\r\n install(app: App, options: Partial<ToastOptions> & { property?: string } = {}) {\r\n const { property, ...others } = options\r\n\r\n this.config(others)\r\n this._mountedApp = app\r\n\r\n if (property || !app.config.globalProperties.$toast) {\r\n app.config.globalProperties[property || '$toast'] = this\r\n }\r\n }\r\n\r\n transferTo(target: MaybeRef<string | MaybeInstance>) {\r\n if (!isClient) return\r\n\r\n const el = unrefElement(target)\r\n\r\n if (el) {\r\n this._wrapper = el\r\n\r\n if (this._instance) {\r\n this._mountedEl && this._wrapper.appendChild(this._mountedEl)\r\n } else {\r\n this._getInstance()\r\n }\r\n }\r\n }\r\n\r\n private _getInstance() {\r\n if (!this._instance && isClient) {\r\n if (!this._mountedApp) {\r\n console.warn('[vexip-ui:Toast]: App missing, the plugin maybe not installed.')\r\n\r\n this._container = document.createElement('div')\r\n this._innerApp = createApp(Component)\r\n this._instance = this._innerApp.mount(this._container) as ToastInstance\r\n } else {\r\n const vnode = createVNode(Component, null, null)\r\n\r\n this._container = document.createElement('div')\r\n vnode.appContext = this._mountedApp._context\r\n\r\n render(vnode, this._container)\r\n\r\n this._instance = proxyExposed<ToastInstance>(vnode)\r\n }\r\n\r\n this._mountedEl = this._container.firstElementChild as HTMLElement\r\n ;(this._wrapper || document.body).appendChild(this._mountedEl)\r\n }\r\n\r\n return this._instance\r\n }\r\n\r\n private _open(type: null | ToastType, content: FuzzyOptions, _duration?: number) {\r\n if (!isClient) {\r\n return noop\r\n }\r\n\r\n this._timer && clearTimeout(this._timer)\r\n\r\n const options = typeof content === 'string' ? { content, duration: _duration } : content\r\n const convenienceOptions = type ? (conveniences[type] ?? {}) : {}\r\n\r\n const userCloseFn = options.onClose\r\n const onClose = () => {\r\n this._timer && clearTimeout(this._timer)\r\n\r\n if (typeof userCloseFn === 'function') {\r\n return userCloseFn()\r\n }\r\n }\r\n\r\n const toast = this._getInstance()!\r\n const item: ToastOptions = {\r\n ...this.defaults,\r\n ...convenienceOptions,\r\n ...options,\r\n type: type ?? options.type,\r\n onClose,\r\n }\r\n\r\n if (item.icon && typeof item.icon !== 'function') {\r\n item.icon = markRaw(item.icon)\r\n }\r\n\r\n toast.openToast(item)\r\n\r\n const duration = typeof item.duration === 'number' ? item.duration : 2000\r\n\r\n if (duration >= 500) {\r\n this._timer = setTimeout(() => {\r\n toast.closeToast()\r\n }, duration)\r\n }\r\n\r\n return () => {\r\n this._timer && clearTimeout(this._timer)\r\n toast.closeToast()\r\n }\r\n }\r\n}\r\n\r\nexport const Toast = new ToastManager()\r\n"],"names":["conveniences","ToastManager","options","__publicField","toNumber","content","duration","_a","manager","_b","render","destroyObject","app","property","others","target","isClient","el","unrefElement","createApp","Component","vnode","createVNode","proxyExposed","type","_duration","noop","convenienceOptions","userCloseFn","onClose","toast","item","markRaw","Toast"],"mappings":";;;;;;;;;AAwBA,MAAMA,IAAuD;AA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