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
This commit is contained in:
parent
bac4d22614
commit
408a8cad48
@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
ref,
|
||||||
render,
|
render,
|
||||||
useCssVars,
|
useCssVars,
|
||||||
h,
|
h,
|
||||||
@ -125,4 +126,29 @@ describe('useCssVars', () => {
|
|||||||
id
|
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')
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -2,11 +2,12 @@ import {
|
|||||||
ComponentPublicInstance,
|
ComponentPublicInstance,
|
||||||
getCurrentInstance,
|
getCurrentInstance,
|
||||||
onMounted,
|
onMounted,
|
||||||
watchEffect,
|
|
||||||
warn,
|
warn,
|
||||||
VNode,
|
VNode,
|
||||||
Fragment,
|
Fragment,
|
||||||
unref
|
unref,
|
||||||
|
onUpdated,
|
||||||
|
watchEffect
|
||||||
} from '@vue/runtime-core'
|
} from '@vue/runtime-core'
|
||||||
import { ShapeFlags } from '@vue/shared'
|
import { ShapeFlags } from '@vue/shared'
|
||||||
|
|
||||||
@ -27,11 +28,10 @@ export function useCssVars(
|
|||||||
? `${instance.type.__scopeId.replace(/^data-v-/, '')}-`
|
? `${instance.type.__scopeId.replace(/^data-v-/, '')}-`
|
||||||
: ``
|
: ``
|
||||||
|
|
||||||
onMounted(() => {
|
const setVars = () =>
|
||||||
watchEffect(() => {
|
|
||||||
setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
|
setVarsOnVNode(instance.subTree, getter(instance.proxy!), prefix)
|
||||||
})
|
onMounted(() => watchEffect(setVars))
|
||||||
})
|
onUpdated(setVars)
|
||||||
}
|
}
|
||||||
|
|
||||||
function setVarsOnVNode(
|
function setVarsOnVNode(
|
||||||
|
Loading…
Reference in New Issue
Block a user