mirror of https://github.com/vuejs/core.git
fix(sfc/style-vars): properly re-apply style vars on component root elements change
Now uses MutationObserver to ensure it works even for HOCs fix #3894
This commit is contained in:
parent
317654b34f
commit
49dc2dd1e4
|
@ -7,7 +7,8 @@ import {
|
||||||
reactive,
|
reactive,
|
||||||
nextTick,
|
nextTick,
|
||||||
ComponentOptions,
|
ComponentOptions,
|
||||||
Suspense
|
Suspense,
|
||||||
|
FunctionalComponent
|
||||||
} from '@vue/runtime-dom'
|
} from '@vue/runtime-dom'
|
||||||
|
|
||||||
describe('useCssVars', () => {
|
describe('useCssVars', () => {
|
||||||
|
@ -142,6 +143,40 @@ describe('useCssVars', () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// #3894
|
||||||
|
test('with subTree change inside HOC', async () => {
|
||||||
|
const state = reactive({ color: 'red' })
|
||||||
|
const value = ref(true)
|
||||||
|
const root = document.createElement('div')
|
||||||
|
|
||||||
|
const Child: FunctionalComponent = (_, { slots }) => slots.default!()
|
||||||
|
|
||||||
|
const App = {
|
||||||
|
setup() {
|
||||||
|
useCssVars(() => state)
|
||||||
|
return () =>
|
||||||
|
h(
|
||||||
|
Child,
|
||||||
|
null,
|
||||||
|
() => (value.value ? [h('div')] : [h('div'), h('div')])
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render(h(App), root)
|
||||||
|
await nextTick()
|
||||||
|
// 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')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
test('with createStaticVNode', async () => {
|
test('with createStaticVNode', async () => {
|
||||||
const state = reactive({ color: 'red' })
|
const state = reactive({ color: 'red' })
|
||||||
const root = document.createElement('div')
|
const root = document.createElement('div')
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import {
|
import {
|
||||||
getCurrentInstance,
|
getCurrentInstance,
|
||||||
onMounted,
|
|
||||||
warn,
|
warn,
|
||||||
VNode,
|
VNode,
|
||||||
Fragment,
|
Fragment,
|
||||||
Static,
|
Static,
|
||||||
onUpdated,
|
watchPostEffect,
|
||||||
watchEffect
|
onMounted,
|
||||||
|
onUnmounted
|
||||||
} from '@vue/runtime-core'
|
} from '@vue/runtime-core'
|
||||||
import { ShapeFlags } from '@vue/shared'
|
import { ShapeFlags } from '@vue/shared'
|
||||||
|
|
||||||
|
@ -27,8 +27,12 @@ export function useCssVars(getter: (ctx: any) => Record<string, string>) {
|
||||||
|
|
||||||
const setVars = () =>
|
const setVars = () =>
|
||||||
setVarsOnVNode(instance.subTree, getter(instance.proxy!))
|
setVarsOnVNode(instance.subTree, getter(instance.proxy!))
|
||||||
onMounted(() => watchEffect(setVars, { flush: 'post' }))
|
watchPostEffect(setVars)
|
||||||
onUpdated(setVars)
|
onMounted(() => {
|
||||||
|
const ob = new MutationObserver(setVars)
|
||||||
|
ob.observe(instance.subTree.el!.parentNode, { childList: true })
|
||||||
|
onUnmounted(() => ob.disconnect())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
|
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
|
||||||
|
|
Loading…
Reference in New Issue