Skip to content

Commit 408a8ca

Browse files
authored
fix(sfc/style-vars): should attach css vars while subtree changed (#2178)
* fix(cssVars): should attach css vars while `subtree` changed fix #2177 * fix: fix test
1 parent bac4d22 commit 408a8ca

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ref,
23
render,
34
useCssVars,
45
h,
@@ -125,4 +126,29 @@ describe('useCssVars', () => {
125126
id
126127
)
127128
})
129+
130+
test('with subTree changed', async () => {
131+
const state = reactive({ color: 'red' })
132+
const value = ref(true)
133+
const root = document.createElement('div')
134+
135+
const App = {
136+
setup() {
137+
useCssVars(() => state)
138+
return () => (value.value ? [h('div')] : [h('div'), h('div')])
139+
}
140+
}
141+
142+
render(h(App), root)
143+
// css vars use with fallback tree
144+
for (const c of [].slice.call(root.children as any)) {
145+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
146+
}
147+
148+
value.value = false
149+
await nextTick()
150+
for (const c of [].slice.call(root.children as any)) {
151+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
152+
}
153+
})
128154
})

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
ComponentPublicInstance,
33
getCurrentInstance,
44
onMounted,
5-
watchEffect,
65
warn,
76
VNode,
87
Fragment,
9-
unref
8+
unref,
9+
onUpdated,
10+
watchEffect
1011
} from '@vue/runtime-core'
1112
import { ShapeFlags } from '@vue/shared'
1213

@@ -27,11 +28,10 @@ export function useCssVars(
2728
? `${instance.type.__scopeId.replace(/^data-v-/, '')}-`
2829
: ``
2930

30-
onMounted(() => {
31-
watchEffect(() => {
32-
setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
33-
})
34-
})
31+
const setVars = () =>
32+
setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
33+
onMounted(() => watchEffect(setVars))
34+
onUpdated(setVars)
3535
}
3636

3737
function setVarsOnVNode(

0 commit comments

Comments
 (0)