Skip to content

Commit def21b6

Browse files
authored
fix(runtime-vapor): apply v-show to vdom child (#13767)
close #13765
1 parent 248b394 commit def21b6

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

packages/runtime-vapor/__tests__/vdomInterop.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { makeInteropRender } from './_utils'
1212
import {
1313
applyTextModel,
14+
applyVShow,
1415
child,
1516
createComponent,
1617
defineVaporComponent,
@@ -113,6 +114,37 @@ describe('vdomInterop', () => {
113114
})
114115
})
115116

117+
describe('v-show', () => {
118+
test('apply v-show to vdom child', async () => {
119+
const VDomChild = {
120+
setup() {
121+
return () => h('div')
122+
},
123+
}
124+
125+
const show = ref(false)
126+
const VaporChild = defineVaporComponent({
127+
setup() {
128+
const n1 = createComponent(VDomChild as any)
129+
applyVShow(n1, () => show.value)
130+
return n1
131+
},
132+
})
133+
134+
const { html } = define({
135+
setup() {
136+
return () => h(VaporChild as any)
137+
},
138+
}).render()
139+
140+
expect(html()).toBe('<div style="display: none;"></div>')
141+
142+
show.value = true
143+
await nextTick()
144+
expect(html()).toBe('<div style=""></div>')
145+
})
146+
})
147+
116148
describe('slots', () => {
117149
test('basic', () => {
118150
const VDomChild = defineComponent({

packages/runtime-vapor/src/directives/vShow.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@vue/runtime-dom'
77
import { renderEffect } from '../renderEffect'
88
import { isVaporComponent } from '../component'
9-
import { type Block, DynamicFragment } from '../block'
9+
import { type Block, DynamicFragment, VaporFragment } from '../block'
1010
import { isArray } from '@vue/shared'
1111

1212
export function applyVShow(target: Block, source: () => any): void {
@@ -24,6 +24,12 @@ export function applyVShow(target: Block, source: () => any): void {
2424
update.call(target, render, key)
2525
setDisplay(target, source())
2626
}
27+
} else if (target instanceof VaporFragment && target.insert) {
28+
const insert = target.insert
29+
target.insert = (parent, anchor) => {
30+
insert.call(target, parent, anchor)
31+
setDisplay(target, source())
32+
}
2733
}
2834

2935
renderEffect(() => setDisplay(target, source()))
@@ -33,12 +39,16 @@ function setDisplay(target: Block, value: unknown): void {
3339
if (isVaporComponent(target)) {
3440
return setDisplay(target, value)
3541
}
36-
if (isArray(target) && target.length === 1) {
37-
return setDisplay(target[0], value)
42+
if (isArray(target)) {
43+
if (target.length === 0) return
44+
if (target.length === 1) return setDisplay(target[0], value)
3845
}
3946
if (target instanceof DynamicFragment) {
4047
return setDisplay(target.nodes, value)
4148
}
49+
if (target instanceof VaporFragment && target.insert) {
50+
return setDisplay(target.nodes, value)
51+
}
4252
if (target instanceof Element) {
4353
const el = target as VShowElement
4454
if (!(vShowOriginalDisplay in el)) {

packages/runtime-vapor/src/vdomInterop.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ function createVDOMComponent(
216216
parentInstance as any,
217217
)
218218
}
219+
220+
frag.nodes = vnode.el as Block
219221
}
220222

221223
frag.remove = unmount

0 commit comments

Comments
 (0)