Skip to content

feat(runtime-vapor): support functional slot in vdom component #13571

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion packages/runtime-core/src/helpers/renderSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function renderSlot(
let slot = slots[name]

// vapor slots rendered in vdom
if (slot && slots._vapor) {
if (slot && (slot as any).__vapor) {
const ret = (openBlock(), createBlock(VaporSlot, props))
ret.vs = { slot, fallback }
return ret
Expand Down
63 changes: 61 additions & 2 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineComponent, h } from '@vue/runtime-dom'
import { createVNode, defineComponent, h, renderSlot } from '@vue/runtime-dom'
import { makeInteropRender } from './_utils'
import { createComponent, defineVaporComponent } from '../src'

Expand All @@ -9,7 +9,66 @@ describe('vdomInterop', () => {

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

describe.todo('slots', () => {})
describe('slots', () => {
test('basic', () => {
const VDomChild = defineComponent({
setup(_, { slots }) {
return () => renderSlot(slots, 'default')
},
})

const VaporChild = defineVaporComponent({
setup() {
return createComponent(
VDomChild as any,
null,
{
default: () => document.createTextNode('default slot'),
},
true,
)
},
})

const { html } = define({
setup() {
return () => h(VaporChild as any)
},
}).render()

expect(html()).toBe('default slot')
})

test('functional slot', () => {
const VDomChild = defineComponent({
setup(_, { slots }) {
console.log(slots.default)
return () => createVNode(slots.default!)
},
})

const VaporChild = defineVaporComponent({
setup() {
return createComponent(
VDomChild as any,
null,
{
default: () => document.createTextNode('default slot'),
},
true,
)
},
})

const { html } = define({
setup() {
return () => h(VaporChild as any)
},
}).render()

expect(html()).toBe('default slot')
})
})

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

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function getAttrFromRawProps(rawProps: RawProps, key: string): unknown {
source = dynamicSources[i]
isDynamic = isFunction(source)
source = isDynamic ? (source as Function)() : source
if (hasOwn(source, key)) {
if (source && hasOwn(source, key)) {
const value = isDynamic ? source[key] : source[key]()
if (merged) {
merged.push(value)
Expand Down
16 changes: 11 additions & 5 deletions packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,17 @@ const vaporSlotPropsProxyHandler: ProxyHandler<

const vaporSlotsProxyHandler: ProxyHandler<any> = {
get(target, key) {
if (key === '_vapor') {
return target
} else {
return target[key]
}
const fn = target[key]
return isFunction(fn)
? new Proxy(fn, {
get(fnTarget, fnKey) {
if (fnKey === '__vapor') {
return true
}
return fnTarget[fnKey]
},
})
: fn
},
}

Expand Down