]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-vapor): apply v-show to vdom child (#13767)
authoredison <daiwei521@126.com>
Wed, 20 Aug 2025 09:49:33 +0000 (17:49 +0800)
committerGitHub <noreply@github.com>
Wed, 20 Aug 2025 09:49:33 +0000 (17:49 +0800)
close #13765

packages/runtime-vapor/__tests__/vdomInterop.spec.ts
packages/runtime-vapor/src/directives/vShow.ts
packages/runtime-vapor/src/vdomInterop.ts

index 98c4cdbc9d03186bcc2a3addbb3dc483dc79e0e4..c7cc41d4fc85d939ccd1206b9f5d537a3b434ce1 100644 (file)
@@ -11,6 +11,7 @@ import {
 import { makeInteropRender } from './_utils'
 import {
   applyTextModel,
+  applyVShow,
   child,
   createComponent,
   defineVaporComponent,
@@ -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({
index ac4c066b71d6cbc78ed3d36fe1daf7bf0c2ba4b7..6ed28dfddfedff207e33d050c94a80b87430e0fd 100644 (file)
@@ -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 {
@@ -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()))
@@ -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)) {
index 1573a306922aacd6813fc8cdfe8f7c7a84ec4f4a..adc54526175c09642e2bb5f0d759e6835a63778a 100644 (file)
@@ -216,6 +216,8 @@ function createVDOMComponent(
         parentInstance as any,
       )
     }
+
+    frag.nodes = vnode.el as Block
   }
 
   frag.remove = unmount