Skip to content
Merged
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
55 changes: 55 additions & 0 deletions packages/runtime-vapor/__tests__/hydration/interopBasic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,59 @@ describe('VDOM interop', () => {
"
`)
})

test('hydrate a functional child injecting from a vdom parent under vapor', async () => {
const data = ref(0)
const { container } = await testWithVaporApp(
`<template><div><components.G :n="data" /></div></template>`,
{
G: {
code: `<script setup>
import { h, inject, provide } from 'vue'
defineProps(['n'])
provide('k', 'G')
const F = props => h('i', inject('k', 'none') + ':' + props.n)
</script>
<template><F :n="n" /></template>`,
vapor: false,
},
},
data,
)
expect(formatHtml(container.innerHTML)).toBe('<div><i>G:0</i></div>')
expect(`Hydration text content mismatch`).not.toHaveBeenWarned()

data.value++
await nextTick()
expect(formatHtml(container.innerHTML)).toBe('<div><i>G:1</i></div>')
})

test('hydrate VDOM prop defaults injecting from a Vapor parent', async () => {
const { container, html } = await testWithVaporApp(
`<script setup vapor>
import { provide } from 'vue'
const components = _components
provide('k', 'parent')
</script>
<template><components.Child /></template>`,
{
Child: {
code: `<script setup>
import { inject } from 'vue'
defineProps({
value: {
default: () => inject('k', 'missing'),
},
})
</script>
<template><i>{{ value }}</i></template>`,
vapor: false,
},
},
)

expect(html).toBe('<i>parent</i>')
expect(container.innerHTML).toBe(html)
expect('Hydration text content mismatch').not.toHaveBeenWarned()
})
})
136 changes: 136 additions & 0 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11608,4 +11608,140 @@ describe('vdomInterop', () => {
},
)
})

describe('functional child of a vdom component mounted by vapor', () => {
// `inject` in a functional component falls back to the rendering instance
// only while no setup instance is current, as in a VDOM render
const data = ref(0)
const F = (props: { n: number }) =>
h('i', `${inject('k', 'none')}:${props.n}`)
const G = compile(
`<script setup>
import { provide } from 'vue'
defineProps(['n'])
provide('k', 'G')
const components = _components
</script>
<template><components.F :n="n" /></template>`,
data,
{ F },
{ vapor: false },
)
const vdomSfc = (template: string, components: Record<string, any>) =>
compile(
`<script setup>const components = _components; const data = _data</script>` +
template,
data,
components,
{ vapor: false },
)

async function expectInjected(App: any, value = 'G') {
data.value = 0
const { html } = define(App).render()
expect(html()).toContain(`<i>${value}:0</i>`)
data.value++
await nextTick()
expect(html()).toContain(`<i>${value}:1</i>`)
}

test('rendered by a vapor provider', async () => {
const App = compile(
`<script setup vapor>
import { provide } from 'vue'
provide('k', 'A')
const components = _components
const data = _data
</script>
<template><components.F :n="data" /></template>`,
data,
{ F },
)
await expectInjected(App, 'A')
})

test('rendered by a vapor parent', async () => {
await expectInjected(
compile(`<template><components.G :n="data" /></template>`, data, { G }),
)
})

test('passed as a vnode to a vapor dynamic component', async () => {
const RouterView = defineComponent({
setup(_, { slots }) {
return () => slots.default!({ Component: h(G, { n: data.value }) })
},
})
const App = compile(
`<template><components.RouterView v-slot="{ Component }"><component :is="Component" /></components.RouterView></template>`,
data,
{ RouterView },
)
await expectInjected(App)
})

test('rendered in a vdom slot of a vapor child', async () => {
const VaporChild = compile(
`<template><div><slot /></div></template>`,
data,
)
await expectInjected(
vdomSfc(
`<template><components.VaporChild><components.G :n="data" /></components.VaporChild></template>`,
{ VaporChild, G },
),
)
})

test('rendered as a vdom outlet fallback of a forwarded vapor slot', async () => {
const Outer = vdomSfc(
`<template><slot name="foo"><components.G v-if="data !== 1" :n="data" /></slot></template>`,
{ G },
)
const Inner = vdomSfc(
`<template><components.Outer><template #foo><slot name="bar" /></template></components.Outer></template>`,
{ Outer },
)
const Bridge = compile(
`<template><components.Inner><template #bar><slot name="bar" /></template></components.Inner></template>`,
data,
{ Inner },
)
data.value = 0
const { html } = define(Bridge).render()
expect(html()).toContain('<i>G:0</i>')
// the fallback becomes invalid, then valid again
data.value = 1
await nextTick()
expect(html()).not.toContain('<i>')
data.value = 2
await nextTick()
expect(html()).toContain('<i>G:2</i>')
})
})

test('VDOM prop defaults can inject from a Vapor parent', () => {
const Child = defineComponent({
props: {
value: {
default: () => inject('k', 'missing'),
},
},
setup: props => () => h('i', props.value),
})

const App = compile(
`<script setup vapor>
import { provide } from 'vue'
const components = _components
provide('k', 'parent')
</script>
<template><components.Child /></template>`,
ref(0),
{ Child },
)

const { html } = define(App).render()
expect(html()).toContain('<i>parent</i>')
})
})
28 changes: 25 additions & 3 deletions packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,9 @@ function mountVNode(
return
} else {
const prev = currentInstance
simpleSetCurrentInstance(parentComponent)
// vdom renders with no current instance, so that `inject` in a
// functional child falls back to its rendering instance
simpleSetCurrentInstance(null)
if (!isMounted) {
if (transition) setVNodeTransitionHooks(vnode, transition)
namespace = getContainerType(parentNode as Element)
Expand Down Expand Up @@ -1367,7 +1369,7 @@ function mountVNode(
frag.vnode = vnode
frag.$key = vnodeKeyOf(vnode)
const prevInstance = currentInstance
simpleSetCurrentInstance(parentComponent)
simpleSetCurrentInstance(null)
internals.p(
previous,
vnode,
Expand Down Expand Up @@ -1466,6 +1468,10 @@ function createVDOMComponent(

// overwrite how the vdom instance handles props
vnode.vi = (instance: ComponentInternalInstance) => {
// The props wrapper must inherit from the Vapor parent while VDOM renders
// without a current instance.
const prev = currentInstance
simpleSetCurrentInstance(parentComponent)
// Reuse VDOM's normalized options so Options API merging stays in VDOM.
const wrapper = new VaporComponentInstance<Record<string, unknown>>(
useBridge
Expand All @@ -1479,6 +1485,7 @@ function createVDOMComponent(
parentComponent ? parentComponent.appContext : undefined,
once,
)
simpleSetCurrentInstance(prev)

const attrs = createInternalObject()
const isFilteredAttr = (key: string | symbol): boolean =>
Expand Down Expand Up @@ -1612,7 +1619,7 @@ function createVDOMComponent(
)
} else {
const prev = currentInstance
simpleSetCurrentInstance(parentComponent)
simpleSetCurrentInstance(null)
if (!isMounted) {
if (transition) setVNodeTransitionHooks(vnode, transition)
internals.mt(
Expand Down Expand Up @@ -2169,6 +2176,8 @@ function renderVDOMSlot(
}
}
trackSlotVNodeUpdatesWithRefresh(next, refreshSlotVNode, notifyBeforeUpdate)
const prev = currentInstance
simpleSetCurrentInstance(null)
internals.p(
previous,
next,
Expand All @@ -2179,6 +2188,7 @@ function renderVDOMSlot(
slotNamespace,
concatInteropScopeIds(frag.slotScopeIds, slotScopeIds),
)
simpleSetCurrentInstance(prev)
setRendered(next, valid)
finishContentUpdate()
}
Expand Down Expand Up @@ -2625,6 +2635,8 @@ function hydrateVNode(
) {
const node = currentHydrationNode!
if (!vdomHydrateNode) vdomHydrateNode = ensureHydrationRenderer().hydrateNode!
const prev = currentInstance
simpleSetCurrentInstance(null)
const nextNode = vdomHydrateNode(
node,
vnode,
Expand All @@ -2633,6 +2645,7 @@ function hydrateVNode(
slotScopeIds,
false,
)
simpleSetCurrentInstance(prev)
// no next node: the vnode ends its parent, move on from there
if (nextNode) setCurrentHydrationNode(nextNode)
else advanceHydrationNode(parentNode(node)!)
Expand Down Expand Up @@ -3794,6 +3807,8 @@ function createVNodeChildrenFragment(
notifyBeforeUpdate,
)
if (nextChildren.length) {
const prevInstance = currentInstance
simpleSetCurrentInstance(null)
internals.mc(
nextChildren,
currentParentNode!,
Expand All @@ -3804,6 +3819,7 @@ function createVNodeChildrenFragment(
frag.slotScopeIds,
false,
)
simpleSetCurrentInstance(prevInstance)
}
} else {
const nextVNode = createVNode(Fragment, null, nextChildren)
Expand All @@ -3814,6 +3830,8 @@ function createVNodeChildrenFragment(
},
notifyBeforeUpdate,
)
const prevInstance = currentInstance
simpleSetCurrentInstance(null)
internals.pc(
currentVNode,
nextVNode,
Expand All @@ -3825,6 +3843,7 @@ function createVNodeChildrenFragment(
frag.slotScopeIds,
false,
)
simpleSetCurrentInstance(prevInstance)
currentChildren = nextChildren
currentVNode = nextVNode
}
Expand Down Expand Up @@ -3879,6 +3898,8 @@ function createVNodeChildrenFragment(
)
}
if (currentChildren.length) {
const prevInstance = currentInstance
simpleSetCurrentInstance(null)
internals.mc(
currentChildren,
currentParentNode,
Expand All @@ -3889,6 +3910,7 @@ function createVNodeChildrenFragment(
frag.slotScopeIds,
false,
)
simpleSetCurrentInstance(prevInstance)
}
syncResolvedNodes()
isMounted = true
Expand Down
Loading