UNPKG

tdesign-vue-next

Version:
1 lines 6.32 kB
{"version":3,"file":"plugin.mjs","sources":["../../../components/drawer/plugin.tsx"],"sourcesContent":["import { App, createApp, ref, Plugin, defineComponent, h, onMounted, nextTick } from 'vue';\nimport DrawerComponent from './drawer';\nimport { getAttach } from '../utils/dom';\nimport { DrawerOptions, DrawerMethod, DrawerInstance } from './type';\nimport { omit } from 'lodash-es';\n\nconst createDrawer: DrawerMethod = (props: DrawerOptions) => {\n const options = { ...props };\n const wrapper = document.createElement('div');\n const visible = ref(false);\n const { style } = options;\n\n const updateStyle = (style: DrawerOptions['style']) => {\n if (style) {\n (wrapper.firstElementChild as HTMLElement).style.cssText += style;\n }\n };\n\n const component = defineComponent({\n setup(props, { expose }) {\n const drawerOptions = ref<Record<string, any>>(options);\n onMounted(() => {\n visible.value = true;\n (document.activeElement as HTMLElement).blur();\n nextTick(() => {\n updateStyle(style);\n });\n });\n const update = (newOptions: DrawerOptions) => {\n drawerOptions.value = {\n ...options,\n ...newOptions,\n };\n };\n\n expose({\n update,\n });\n\n return () => {\n const onClose =\n options.onClose ||\n function () {\n visible.value = false;\n };\n delete options.style;\n return h(DrawerComponent, {\n onClose,\n visible: visible.value,\n drawerClassName: drawerOptions.value?.className,\n ...drawerOptions.value,\n });\n };\n },\n });\n const drawerComponent = createApp(component);\n const drawer = drawerComponent.mount(wrapper);\n\n const destroyDrawer = () => {\n visible.value = false;\n setTimeout(() => {\n drawerComponent.unmount();\n wrapper.remove();\n }, 300);\n };\n\n const container = getAttach(options.attach);\n if (container) {\n container.appendChild(wrapper);\n } else {\n console.error('attach is not exist');\n }\n\n const drawerNode: DrawerInstance = {\n show: () => {\n visible.value = true;\n },\n hide: () => {\n visible.value = false;\n },\n update: (newOptions: DrawerOptions) => {\n // className & style由updateClassNameStyle来处理\n drawer.update(omit(newOptions, ['style']));\n updateStyle(newOptions.style);\n },\n destroy: () => {\n destroyDrawer();\n },\n };\n return drawerNode;\n};\n\nexport type DrawerPluginType = Plugin & DrawerMethod;\n\nexport const DrawerPlugin = createDrawer as DrawerPluginType;\n\nDrawerPlugin.install = (app: App): void => {\n app.config.globalProperties.$drawer = createDrawer;\n};\n\nexport default DrawerPlugin;\n"],"names":["createDrawer","props","options","_objectSpread","wrapper","document","createElement","visible","ref","style","updateStyle","firstElementChild","cssText","component","defineComponent","setup","expose","_ref","drawerOptions","onMounted","value","activeElement","blur","nextTick","update","newOptions","_drawerOptions$value","onClose","h","DrawerComponent","drawerClassName","className","drawerComponent","createApp","drawer","mount","destroyDrawer","setTimeout","unmount","remove","container","getAttach","attach","appendChild","console","error","drawerNode","show","hide","omit","destroy","DrawerPlugin","install","app","config","globalProperties","$drawer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAMA,YAAA,GAA6B,SAA7BA,YAAAA,CAA8BC,KAAyB,EAAA;AACrD,EAAA,IAAAC,OAAA,GAAAC,aAAA,CAAA,EAAA,EAAeF,KAAM,CAAA,CAAA;AACrB,EAAA,IAAAG,OAAA,GAAUC,QAAS,CAAAC,aAAA,CAAc,KAAK,CAAA,CAAA;AACtC,EAAA,IAAAC,OAAA,GAAUC,IAAI,KAAK,CAAA,CAAA;AACnB,EAAA,IAAEC,QAAUP,OAAA,CAAVO;AAEF,EAAA,IAAAC,WAAA,GAAc,SAAdA,WAAAA,CAAeD,MAAkC,EAAA;AACrD,IAAA,IAAIA,MAAO,EAAA;AACRL,MAAAA,OAAA,CAAQO,iBAAkC,CAAAF,KAAA,CAAMG,OAAWH,IAAAA,MAAAA,CAAAA;AAC9D,KAAA;GACF,CAAA;EAEA,IAAMI,YAAYC,eAAgB,CAAA;AAChCC,IAAAA,KAAMd,WAANc,KAAMd,CAAAA,MAAAA,EAAAA,IAAAA,EAAmB;AAAA,MAAA,IAAVe,MAAA,GAAAC,IAAA,CAAAD,MAAA,CAAA;AACP,MAAA,IAAAE,aAAA,GAAgBV,IAAyBN,OAAO,CAAA,CAAA;A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