Skip to content

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

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: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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/reactivity/__tests__/readonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ describe('reactivity/readonly', () => {
})
})

test.todo('should be able to trigger with triggerRef', () => {
test('should be able to trigger with triggerRef', () => {
const r = shallowRef({ a: 1 })
const ror = readonly(r)
let dummy
Expand Down
7 changes: 6 additions & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,20 @@ class BaseReactiveHandler implements ProxyHandler<Target> {
}
}

const wasRef = isRef(target)
const res = Reflect.get(
target,
key,
// if this is a proxy wrapping a ref, return methods using the raw ref
// as receiver so that we don't have to call `toRaw` on the ref in all
// its class methods
isRef(target) ? target : receiver,
wasRef ? target : receiver,
)

if (wasRef && key !== 'value') {
return res
}

if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
return res
}
Expand Down
6 changes: 0 additions & 6 deletions packages/reactivity/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ class Dep implements Dependency {
_subs: Link | undefined = undefined
subsTail: Link | undefined = undefined

/**
* @internal
*/
readonly __v_skip = true
// TODO isolatedDeclarations ReactiveFlags.SKIP

constructor(
private map: KeyToDepMap,
private key: unknown,
Expand Down
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