Skip to content

fix(runtime-vapor): apply v-show to vdom child #13767

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

Merged
merged 1 commit into from
Aug 20, 2025
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
32 changes: 32 additions & 0 deletions packages/runtime-vapor/__tests__/vdomInterop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { makeInteropRender } from './_utils'
import {
applyTextModel,
applyVShow,
child,
createComponent,
defineVaporComponent,
Expand Down Expand Up @@ -113,6 +114,37 @@ describe('vdomInterop', () => {
})
})

describe('v-show', () => {
test('apply v-show to vdom child', async () => {
const VDomChild = {
setup() {
return () => h('div')
},
}

const show = ref(false)
const VaporChild = defineVaporComponent({
setup() {
const n1 = createComponent(VDomChild as any)
applyVShow(n1, () => show.value)
return n1
},
})

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

expect(html()).toBe('<div style="display: none;"></div>')

show.value = true
await nextTick()
expect(html()).toBe('<div style=""></div>')
})
})

describe('slots', () => {
test('basic', () => {
const VDomChild = defineComponent({
Expand Down
16 changes: 13 additions & 3 deletions packages/runtime-vapor/src/directives/vShow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@vue/runtime-dom'
import { renderEffect } from '../renderEffect'
import { isVaporComponent } from '../component'
import { type Block, DynamicFragment } from '../block'
import { type Block, DynamicFragment, VaporFragment } from '../block'
import { isArray } from '@vue/shared'

export function applyVShow(target: Block, source: () => any): void {
Expand All @@ -24,6 +24,12 @@ export function applyVShow(target: Block, source: () => any): void {
update.call(target, render, key)
setDisplay(target, source())
}
} else if (target instanceof VaporFragment && target.insert) {
const insert = target.insert
target.insert = (parent, anchor) => {
insert.call(target, parent, anchor)
setDisplay(target, source())
}
}

renderEffect(() => setDisplay(target, source()))
Expand All @@ -33,12 +39,16 @@ function setDisplay(target: Block, value: unknown): void {
if (isVaporComponent(target)) {
return setDisplay(target, value)
}
if (isArray(target) && target.length === 1) {
return setDisplay(target[0], value)
if (isArray(target)) {
if (target.length === 0) return
if (target.length === 1) return setDisplay(target[0], value)
}
if (target instanceof DynamicFragment) {
return setDisplay(target.nodes, value)
}
if (target instanceof VaporFragment && target.insert) {
return setDisplay(target.nodes, value)
}
if (target instanceof Element) {
const el = target as VShowElement
if (!(vShowOriginalDisplay in el)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-vapor/src/vdomInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ function createVDOMComponent(
parentInstance as any,
)
}

frag.nodes = vnode.el as Block
}

frag.remove = unmount
Expand Down
Loading