-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce
useProgrammaticDialog
composable
- Loading branch information
Showing
3 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
AllowedComponentProps, | ||
Component, | ||
createVNode, | ||
mergeProps, | ||
onUnmounted, | ||
render, | ||
VNode, | ||
VNodeProps | ||
} from "vue"; | ||
|
||
export type Prettify<T> = { [K in keyof T]: T[K] } & {}; | ||
|
||
export type ComponentProps<C extends Component> = C extends new (...args: unknown[]) => unknown | ||
? Prettify<Omit<InstanceType<C>["$props"], keyof VNodeProps | keyof AllowedComponentProps>> | ||
: never; | ||
|
||
export type DialogLikeComponent = new (...args: unknown[]) => { | ||
open: () => void; | ||
close: () => void; | ||
} & Component; | ||
|
||
export function useProgrammaticDialog<T extends DialogLikeComponent>( | ||
component: T, | ||
initialProps?: ComponentProps<T> | ||
) { | ||
let instance: InstanceType<T> | null; | ||
let props = { ...initialProps }; | ||
let vnode: VNode | null; | ||
let container: HTMLDivElement | null; | ||
|
||
const mount = (mountProps?: ComponentProps<T>) => { | ||
patchProps(mountProps); | ||
|
||
container = document.createElement("div"); | ||
vnode = createVNode(component, props); | ||
|
||
render(vnode, container); | ||
instance = vnode.component?.exposed as InstanceType<T>; | ||
|
||
document.body.appendChild(container); | ||
}; | ||
|
||
const patchProps = (newProps?: ComponentProps<T>) => { | ||
if (!newProps || Object.keys(newProps).length === 0) return; | ||
|
||
props = mergeProps(props, newProps); | ||
if (vnode && vnode.component) { | ||
Object.assign(vnode.component.props, props); | ||
} | ||
}; | ||
|
||
const open = (props?: ComponentProps<T>) => { | ||
if (!instance) { | ||
mount(props); | ||
} else { | ||
patchProps(props); | ||
instance.open(); | ||
} | ||
}; | ||
|
||
const close = () => instance?.close(); | ||
|
||
const destroy = () => { | ||
if (!container || !instance) return; | ||
|
||
render(null, container); | ||
container.remove(); | ||
container = null; | ||
instance = null; | ||
vnode = null; | ||
}; | ||
|
||
onUnmounted(destroy); | ||
|
||
return { | ||
open, | ||
close | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters