vue3-core/packages/runtime-vapor/src/apiCreateApp.ts

41 lines
1.1 KiB
TypeScript

import { insert } from './dom/element'
import { type VaporComponent, createComponent } from './component'
import {
type AppMountFn,
type AppUnmountFn,
type CreateAppFunction,
createAppAPI,
normalizeContainer,
} from '@vue/runtime-dom'
import type { RawProps } from './componentProps'
let _createApp: CreateAppFunction<ParentNode, VaporComponent>
const mountApp: AppMountFn<ParentNode> = (app, container) => {
// clear content before mounting
if (container.nodeType === 1 /* Node.ELEMENT_NODE */) {
container.textContent = ''
}
const instance = createComponent(app._component, app._props as RawProps)
insert(instance, container)
return instance
}
const unmountApp: AppUnmountFn = app => {
// TODO
}
export const createVaporApp: CreateAppFunction<
ParentNode,
VaporComponent
> = comp => {
if (!_createApp) _createApp = createAppAPI(mountApp, unmountApp)
const app = _createApp(comp)
const mount = app.mount
app.mount = (container, ...args: any[]) => {
container = normalizeContainer(container) as ParentNode
return mount(container, ...args)
}
return app
}