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
8 changes: 7 additions & 1 deletion packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ export interface VaporInteropInterface {
update(n1: VNode, n2: VNode, shouldUpdate: boolean): void
unmount(vnode: VNode, doRemove?: boolean): void
move(vnode: VNode, container: any, anchor: any): void
slot(n1: VNode | null, n2: VNode, container: any, anchor: any): void
slot(
n1: VNode | null,
n2: VNode,
container: any,
anchor: any,
parentComponent: ComponentInternalInstance | null,
): void
hydrate(
vnode: VNode,
node: any,
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,13 @@ function baseCreateRenderer(
)
break
case VaporSlot:
getVaporInterface(parentComponent, n2).slot(n1, n2, container, anchor)
getVaporInterface(parentComponent, n2).slot(
n1,
n2,
container,
anchor,
parentComponent,
)
break
default:
if (shapeFlag & ShapeFlags.ELEMENT) {
Expand Down
50 changes: 49 additions & 1 deletion packages/runtime-vapor/__tests__/apiInject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
reactive,
readonly,
ref,
renderSlot,
toDisplayString,
} from '@vue/runtime-dom'
import {
Expand All @@ -18,11 +19,12 @@ import {
createVaporApp,
defineVaporComponent,
renderEffect,
template,
vaporInteropPlugin,
withVaporCtx,
} from '../src'
import { makeRender } from './_utils'
import { setElementText } from '../src/dom/prop'
import { setElementText, setText } from '../src/dom/prop'

const define = makeRender<any>()

Expand Down Expand Up @@ -426,6 +428,10 @@ describe('api: provide/inject', () => {
})

describe('vdom interop', () => {
beforeEach(() => {
document.body.innerHTML = ''
})

test('should inject value from vapor parent', async () => {
const VdomChild = {
setup() {
Expand Down Expand Up @@ -453,5 +459,47 @@ describe('vdom interop', () => {
value.value = 'bar'
await nextTick()
expect(root.innerHTML).toBe('<div>bar</div>')

app.unmount()
})

test('slotted vapor child should inject value from vdom parent', async () => {
const value = ref('foo')
const VdomParent = {
setup(_: any, { slots }: any) {
provide('foo', value)
return () => renderSlot(slots, 'default')
},
}

const VaporChild = defineVaporComponent({
setup() {
const foo = inject('foo')
const n0 = template(' ')() as any
renderEffect(() => setText(n0, toDisplayString(foo)))
return n0
},
})

const App = defineVaporComponent({
setup() {
return createComponent(VdomParent, null, {
default: () => createComponent(VaporChild),
})
},
})

const root = document.createElement('div')
document.body.appendChild(root)
const app = createVaporApp(App)
app.use(vaporInteropPlugin)
app.mount(root)

expect(root.innerHTML).toBe('foo')

value.value = 'bar'
await nextTick()
expect(root.innerHTML).toBe('bar')
app.unmount()
})
})
5 changes: 4 additions & 1 deletion packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ const vaporInteropImpl: Omit<
/**
* vapor slot in vdom
*/
slot(n1: VNode, n2: VNode, container, anchor) {
slot(n1: VNode, n2: VNode, container, anchor, parentComponent) {
if (!n1) {
const prev = currentInstance
simpleSetCurrentInstance(parentComponent)
// mount
let selfAnchor: Node | undefined
const { slot, fallback } = n2.vs!
Expand All @@ -198,6 +200,7 @@ const vaporInteropImpl: Omit<
// use fragment's anchor when possible
selfAnchor = slotBlock.anchor
}
simpleSetCurrentInstance(prev)
if (!selfAnchor) selfAnchor = createTextNode()
insert((n2.el = n2.anchor = selfAnchor), container, anchor)
insert((n2.vb = slotBlock), container, selfAnchor)
Expand Down
Loading