@extclp/vexip-ui
Version:
A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good
1 lines • 9.55 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../../../components/toast/index.ts"],"sourcesContent":["import { createApp, createVNode, markRaw, render } from 'vue'\n\nimport Component from './toast.vue'\nimport { proxyExposed, unrefElement } from '@vexip-ui/hooks'\nimport { destroyObject, isClient, noop, toNumber } from '@vexip-ui/utils'\n\nimport type { App, MaybeRef } from 'vue'\nimport type { MaybeInstance } from '@vexip-ui/hooks'\nimport type { ToastInstance, ToastOptions, ToastType } from './symbol'\n\nexport { toastProps } from './props'\n\nexport type { ToastProps, ToastCProps } from './props'\nexport type { ToastType, ToastOptions }\n\ntype FuzzyOptions = string | ToastOptions\n\ninterface AipMethod {\n (options: ToastOptions): () => void,\n (content: string, duration?: number): () => void,\n /** @internal */\n (options: FuzzyOptions, duration?: number): () => void\n}\n\nconst conveniences: Record<ToastType, Record<string, any>> = {\n success: {},\n warning: {},\n error: {},\n loading: {\n showMask: true\n }\n}\n\nexport class ToastManager {\n name: string\n defaults: Record<string, unknown>\n\n open: AipMethod\n success: AipMethod\n warning: AipMethod\n error: AipMethod\n loading: AipMethod\n\n private _mountedApp: App<unknown> | null\n private _instance: ToastInstance | null\n private _innerApp: App<unknown> | null\n private _container: HTMLElement | null\n private _timer: ReturnType<typeof setTimeout> | null\n private _wrapper: HTMLElement | SVGElement | null\n private _mountedEl: HTMLElement | null\n\n constructor(options: Partial<ToastOptions> = {}) {\n options = {\n ...options,\n duration: options.duration ? toNumber(options.duration) : 2000\n }\n\n this._mountedApp = null\n this._instance = null\n this._innerApp = null\n this._container = null\n this._timer = null\n this._wrapper = null\n this._mountedEl = null\n this.name = 'Toast'\n this.defaults = {}\n\n this.config(options)\n\n this.open = (content: FuzzyOptions, duration?: number) => {\n return this._open(null, content, duration)\n }\n\n this.success = (content: FuzzyOptions, duration?: number) => {\n return this._open('success', content, duration)\n }\n\n this.warning = (content: FuzzyOptions, duration?: number) => {\n return this._open('warning', content, duration)\n }\n\n this.error = (content: FuzzyOptions, duration?: number) => {\n return this._open('error', content, duration)\n }\n\n this.loading = (content: FuzzyOptions, duration?: number) => {\n return this._open('loading', content, duration)\n }\n }\n\n close() {\n this._timer && clearTimeout(this._timer)\n this._getInstance()?.closeToast()\n }\n\n config(options: Record<string, unknown>) {\n this.defaults = { ...this.defaults, ...options }\n }\n\n clone() {\n const manager = new ToastManager(this.defaults)\n\n manager._mountedApp = this._mountedApp\n\n return manager\n }\n\n destroy() {\n this._mountedEl && this._wrapper?.removeChild(this._mountedEl)\n this._innerApp?.unmount()\n this._container && render(null, this._container)\n destroyObject(this)\n }\n\n isDestroyed() {\n return false\n }\n\n install(app: App, options: Partial<ToastOptions> & { property?: string } = {}) {\n const { property, ...others } = options\n\n this.config(others)\n this._mountedApp = app\n\n if (property || !app.config.globalProperties.$toast) {\n app.config.globalProperties[property || '$toast'] = this\n }\n }\n\n transferTo(target: MaybeRef<string | MaybeInstance>) {\n if (!isClient) return\n\n const el = unrefElement(target)\n\n if (el) {\n this._wrapper = el\n\n if (this._instance) {\n this._mountedEl && this._wrapper.appendChild(this._mountedEl)\n } else {\n this._getInstance()\n }\n }\n }\n\n private _getInstance() {\n if (!this._instance && isClient) {\n if (!this._mountedApp) {\n console.warn('[vexip-ui:Toast]: App missing, the plugin maybe not installed.')\n\n this._container = document.createElement('div')\n this._innerApp = createApp(Component)\n this._instance = this._innerApp.mount(this._container) as ToastInstance\n } else {\n const vnode = createVNode(Component, null, null)\n\n this._container = document.createElement('div')\n vnode.appContext = this._mountedApp._context\n\n render(vnode, this._container)\n\n this._instance = proxyExposed<ToastInstance>(vnode)\n }\n\n this._mountedEl = this._container.firstElementChild as HTMLElement\n ;(this._wrapper || document.body).appendChild(this._mountedEl)\n }\n\n return this._instance\n }\n\n private _open(type: null | ToastType, content: FuzzyOptions, _duration?: number) {\n if (!isClient) {\n return noop\n }\n\n this._timer && clearTimeout(this._timer)\n\n const options = typeof content === 'string' ? { content, duration: _duration } : content\n const convenienceOptions = type ? (conveniences[type] ?? {}) : {}\n\n const userCloseFn = options.onClose\n const onClose = () => {\n this._timer && clearTimeout(this._timer)\n\n if (typeof userCloseFn === 'function') {\n return userCloseFn()\n }\n }\n\n const toast = this._getInstance()!\n const item: ToastOptions = {\n ...this.defaults,\n ...convenienceOptions,\n ...options,\n type: type ?? options.type,\n onClose\n }\n\n if (item.icon && typeof item.icon !== 'function') {\n item.icon = markRaw(item.icon)\n }\n\n toast.openToast(item)\n\n const duration = typeof item.duration === 'number' ? item.duration : 2000\n\n if (duration >= 500) {\n this._timer = setTimeout(() => {\n toast.closeToast()\n }, duration)\n }\n\n return () => {\n this._timer && clearTimeout(this._timer)\n toast.closeToast()\n }\n }\n}\n\nexport const Toast = new ToastManager()\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;AAAA,EAC3D,SAAS,CAAC;AAAA,EACV,SAAS,CAAC;AAAA,EACV,OAAO,CAAC;AAAA,EACR,SAAS;AAAA,IACP,UAAU;AAAA,EAAA;AAEd;AAEO,MAAMC,EAAa;AAAA,EAkBxB,YAAYC,IAAiC,IAAI;AAjBjD,IAAAC,EAAA;AACA,IAAAA,EAAA;AAEA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAEQ,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AAGI,IAAAD,IAAA;AAAA,MACR,GAAGA;AAAA,MACH,UAAUA,EAAQ,WAAWE,EAASF,EAAQ,QAAQ,IAAI;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