]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(sfc/style-vars): should attach css vars while `subtree` changed (#2178)
authorunderfin <likui6666666@gmail.com>
Tue, 13 Oct 2020 19:37:45 +0000 (03:37 +0800)
committerGitHub <noreply@github.com>
Tue, 13 Oct 2020 19:37:45 +0000 (15:37 -0400)
* fix(cssVars): should attach css vars while `subtree` changed

fix #2177

* fix: fix test

packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
packages/runtime-dom/src/helpers/useCssVars.ts

index 293dc70499338701383e345ab6bb8c77e4681c6d..0766bda806d60489e79f5b8c112e5b064ec3198c 100644 (file)
@@ -1,4 +1,5 @@
 import {
+  ref,
   render,
   useCssVars,
   h,
@@ -125,4 +126,29 @@ describe('useCssVars', () => {
       id
     )
   })
+
+  test('with subTree changed', async () => {
+    const state = reactive({ color: 'red' })
+    const value = ref(true)
+    const root = document.createElement('div')
+
+    const App = {
+      setup() {
+        useCssVars(() => state)
+        return () => (value.value ? [h('div')] : [h('div'), h('div')])
+      }
+    }
+
+    render(h(App), root)
+    // css vars use with fallback tree
+    for (const c of [].slice.call(root.children as any)) {
+      expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
+    }
+
+    value.value = false
+    await nextTick()
+    for (const c of [].slice.call(root.children as any)) {
+      expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
+    }
+  })
 })
index 86d571c2c356b23d2b23da5742530af1b2c57583..ff90ec8ce646b7cfcba58006f9f01f4dc02a0256 100644 (file)
@@ -2,11 +2,12 @@ import {
   ComponentPublicInstance,
   getCurrentInstance,
   onMounted,
-  watchEffect,
   warn,
   VNode,
   Fragment,
-  unref
+  unref,
+  onUpdated,
+  watchEffect
 } from '@vue/runtime-core'
 import { ShapeFlags } from '@vue/shared'
 
@@ -27,11 +28,10 @@ export function useCssVars(
       ? `${instance.type.__scopeId.replace(/^data-v-/, '')}-`
       : ``
 
-  onMounted(() => {
-    watchEffect(() => {
-      setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
-    })
-  })
+  const setVars = () =>
+    setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
+  onMounted(() => watchEffect(setVars))
+  onUpdated(setVars)
 }
 
 function setVarsOnVNode(