import { createVNode, defineComponent, getCurrentInstance, onMounted, onUnmounted, type ComponentInternalInstance, type VNode, type VNodeTypes, } from "vue"; import type { ComponentProps } from "vue-component-type-helpers"; import type InstanceRegistry from "@/components/programmatic/InstanceRegistry"; import { isClient } from "@/utils/ssr"; export type ProgrammaticComponentProps = { /** * Component to be injected. * Terminate the component by emitting a 'close' event — emits('close') */ component: C; /** * Props to be binded to the injected component. * Both attributes and properties can be used in props. * Vue automatically picks the right way to assign it. * `class` and `style` have the same object / array value support like in templates. * Event listeners should be passed as onXxx. * @see https://vuejs.org/api/render-function.html#h */ props?: ComponentProps | { container?: HTMLElement }; /** Programmatic component registry instance. */ registry?: InstanceRegistry; }; export type CloseEventArgs = NonNullable["onClose"]> extends (...args: any[]) => any ? Parameters["onClose"]> : never[]; /** The ProgrammaticComponent close funtion definition. */ type CloseFunction = ( ...args: CloseEventArgs | unknown[] ) => void; export type ProgrammaticComponentEmits = { /** * On component close event. * This get called when the component emits `close` or the exposed `close` function get called. */ close?: CloseFunction; /** On component destroy event which get called when the component should be destroyed. */ destroy?: () => void; }; // there is a bug with functional defineComponent and extracting the exposed type // export type ProgrammaticComponentExpose = ComponentExposed< // typeof ProgrammaticComponent // >; export type ProgrammaticComponentExpose = { /** Call the close event of the component. */ close: CloseFunction; /** Promise which get resolved on the close event. */ promise: Promise>>; }; export const ProgrammaticComponent = defineComponent< ProgrammaticComponentProps, ProgrammaticComponentEmits >( ( props: ProgrammaticComponentProps, { expose, emit, slots }, ) => { // getting a hold of the internal instance in setup() const vm = getCurrentInstance(); if (!vm) throw new Error("ProgrammaticComponent initialisation failed."); // create response promise let resolve: (args: Parameters>) => void; const promise = new Promise>>( (p1) => (resolve = p1), ); // add component instance to instance register onMounted(() => props.registry?.add(vm)); // remove component instance from instance register onUnmounted(() => props.registry?.remove(vm)); const close: CloseFunction = (...args) => { // emit `onClose` event emit("close", ...args); // call promise resolve resolve(args); // emit `destory` event after animation is finished setTimeout(() => { if (isClient) window.requestAnimationFrame(() => emit("destroy")); else emit("destroy"); }); }; /** expose public functionalities for programmatic usage */ expose({ close, promise } satisfies ProgrammaticComponentExpose); // return render function which renders given component return (): VNode => createVNode( props.component, { ...props.props, onClose: close }, slots["default"], ); }, { name: "ProgrammaticApp", // manual runtime props declaration is currently still needed. props: [ /** the component which should be rendered */ "component", /** props passed to the rendered component */ "props", /** the programmatic instance registry */ "registry", ], // manual runtime emits declaration // eslint-disable-next-line vue/no-unused-emit-declarations -- emitted via the setup context emits: ["close", "destroy"], // manual runtime slot declaration slots: ["default"], }, );