Skip to content

fix(runtime-vapor): component emits vdom interop #13498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@ const define = makeInteropRender()
describe('vdomInterop', () => {
describe.todo('props', () => {})

describe.todo('emit', () => {})
describe('emit', () => {
test('emit from vapor child to vdom parent', () => {
const VaporChild = defineVaporComponent({
emits: ['click'],
setup(_, { emit }) {
emit('click')
return []
},
})

const fn = vi.fn()
define({
setup() {
return () => h(VaporChild as any, { onClick: fn })
},
}).render()

// fn should be called once
expect(fn).toHaveBeenCalledTimes(1)
})
})

describe.todo('slots', () => {})

Expand Down
11 changes: 8 additions & 3 deletions packages/runtime-vapor/src/componentEmits.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { type ObjectEmitsOptions, baseEmit } from '@vue/runtime-dom'
import type { VaporComponent, VaporComponentInstance } from './component'
import { EMPTY_OBJ, hasOwn, isArray } from '@vue/shared'
import { resolveSource } from './componentProps'
import { type RawProps, resolveSource } from './componentProps'
import { interopKey } from './vdomInterop'

/**
* The logic from core isn't too reusable so it's better to duplicate here
Expand Down Expand Up @@ -40,13 +41,17 @@ export function emit(
)
}

function propGetter(rawProps: Record<string, any>, key: string) {
function propGetter(rawProps: RawProps, key: string) {
const dynamicSources = rawProps.$
if (dynamicSources) {
let i = dynamicSources.length
while (i--) {
const source = resolveSource(dynamicSources[i])
if (hasOwn(source, key)) return resolveSource(source[key])
if (hasOwn(source, key))
// for props passed from VDOM component, no need to resolve
return dynamicSources[interopKey]
? source[key]
: resolveSource(source[key])
}
}
return rawProps[key] && resolveSource(rawProps[key])
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import {
import { ReactiveFlags } from '@vue/reactivity'
import { normalizeEmitsOptions } from './componentEmits'
import { renderEffect } from './renderEffect'
import type { interopKey } from './vdomInterop'

export type RawProps = Record<string, () => unknown> & {
// generated by compiler for :[key]="x" or v-bind="x"
$?: DynamicPropsSource[]
$?: DynamicPropsSource[] & { [interopKey]?: boolean }
}

export type DynamicPropsSource =
Expand Down
9 changes: 8 additions & 1 deletion packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { renderEffect } from './renderEffect'
import { createTextNode } from './dom/node'
import { optimizePropertyLookup } from './dom/prop'

export const interopKey: unique symbol = Symbol(`interop`)

// mounting vapor components and slots in vdom
const vaporInteropImpl: Omit<
VaporInteropInterface,
Expand All @@ -51,11 +53,16 @@ const vaporInteropImpl: Omit<
const propsRef = shallowRef(vnode.props)
const slotsRef = shallowRef(vnode.children)

const dynamicPropSource: (() => any)[] & { [interopKey]?: boolean } = [
() => propsRef.value,
]
// mark as interop props
dynamicPropSource[interopKey] = true
// @ts-expect-error
const instance = (vnode.component = createComponent(
vnode.type as any as VaporComponent,
{
$: [() => propsRef.value],
$: dynamicPropSource,
} as RawProps,
{
_: slotsRef, // pass the slots ref
Expand Down