mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.1 KiB
104 lines
3.1 KiB
2 years ago
|
import type { DefineComponent, VNode } from '@vue/runtime-dom'
|
||
|
import { isVNode, render } from '@vue/runtime-dom'
|
||
2 years ago
|
import type { ComponentPublicInstance } from '@vue/runtime-core'
|
||
2 years ago
|
import { isClient } from '@vueuse/core'
|
||
2 years ago
|
import { createEventHook, h, ref, toReactive, tryOnScopeDispose, useNuxtApp, watch } from '#imports'
|
||
2 years ago
|
|
||
|
/**
|
||
|
* Programmatically create a component and attach it to the body (or a specific mount target), like a dialog or modal.
|
||
2 years ago
|
* This composable is not SSR friendly - it should be used only on the client.
|
||
|
*
|
||
|
* @param componentOrVNode The component to create and attach. Can be a VNode or a component definition.
|
||
|
* @param props The props to pass to the component.
|
||
|
* @param mountTarget The target to attach the component to. Defaults to the document body
|
||
|
*
|
||
|
* @example
|
||
|
* import { useDialog } from '#imports'
|
||
|
* import DlgQuickImport from '~/components/dlg/QuickImport.vue'
|
||
|
*
|
||
|
* function openQuickImportDialog(type: string) {
|
||
|
* // create a ref for showing/hiding the modal
|
||
|
* const isOpen = ref(true)
|
||
|
*
|
||
|
* const { close, vNode } = useDialog(DlgQuickImport, {
|
||
|
* 'modelValue': isOpen,
|
||
|
* 'importType': type,
|
||
|
* 'onUpdate:modelValue': closeDialog,
|
||
|
* })
|
||
|
*
|
||
|
* function closeDialog() {
|
||
|
* // hide the modal
|
||
|
* isOpen.value = false
|
||
|
*
|
||
|
* // debounce destroying the component, so the modal transition can finish
|
||
|
* close(1000)
|
||
|
* }
|
||
|
* }
|
||
2 years ago
|
*/
|
||
|
export function useDialog(
|
||
2 years ago
|
componentOrVNode: DefineComponent<any, any, any> | VNode,
|
||
2 years ago
|
props: NonNullable<Parameters<typeof h>[1]> = {},
|
||
|
mountTarget?: Element | ComponentPublicInstance,
|
||
|
) {
|
||
2 years ago
|
if (typeof document === 'undefined' || !isClient) {
|
||
|
console.warn('[useDialog]: Cannot use outside of browser!')
|
||
|
}
|
||
|
|
||
2 years ago
|
const closeHook = createEventHook<void>()
|
||
|
const mountedHook = createEventHook<void>()
|
||
|
|
||
|
const isMounted = $ref(false)
|
||
|
|
||
|
const domNode = document.createElement('div')
|
||
|
|
||
2 years ago
|
const vNodeRef = ref<VNode>()
|
||
|
|
||
2 years ago
|
mountTarget = mountTarget ? ('$el' in mountTarget ? (mountTarget.$el as HTMLElement) : mountTarget) : document.body
|
||
|
|
||
2 years ago
|
/** if specified, append vnode to mount target instead of document.body */
|
||
2 years ago
|
mountTarget.appendChild(domNode)
|
||
2 years ago
|
|
||
|
/** When props change, we want to re-render the element with the new prop values */
|
||
2 years ago
|
const stop = watch(
|
||
2 years ago
|
toReactive(props),
|
||
|
(reactiveProps) => {
|
||
2 years ago
|
const vNode = isVNode(componentOrVNode) ? componentOrVNode : h(componentOrVNode, reactiveProps)
|
||
2 years ago
|
|
||
|
vNode.appContext = useNuxtApp().vueApp._context
|
||
|
|
||
2 years ago
|
vNodeRef.value = vNode
|
||
|
|
||
2 years ago
|
render(vNode, domNode)
|
||
|
|
||
|
if (!isMounted) mountedHook.trigger()
|
||
|
},
|
||
2 years ago
|
{ deep: true, immediate: true, flush: 'post' },
|
||
2 years ago
|
)
|
||
|
|
||
|
/** When calling scope is disposed, destroy component */
|
||
2 years ago
|
tryOnScopeDispose(close)
|
||
2 years ago
|
|
||
|
/** destroy component, can be debounced */
|
||
|
function close(debounce = 0) {
|
||
|
setTimeout(() => {
|
||
2 years ago
|
stop()
|
||
|
|
||
2 years ago
|
render(null, domNode)
|
||
|
|
||
|
setTimeout(() => {
|
||
2 years ago
|
;(mountTarget as HTMLElement)!.removeChild(domNode)
|
||
2 years ago
|
}, 100)
|
||
|
|
||
|
closeHook.trigger()
|
||
|
}, debounce)
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
close,
|
||
|
onClose: closeHook.on,
|
||
|
onMounted: mountedHook.on,
|
||
2 years ago
|
domNode,
|
||
|
vNode: vNodeRef,
|
||
2 years ago
|
}
|
||
|
}
|