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:
Evan You
2021-07-16 12:29:09 -04:00
parent 317654b34f
commit 49dc2dd1e4
2 changed files with 45 additions and 6 deletions

View File

@@ -7,7 +7,8 @@ import {
reactive,
nextTick,
ComponentOptions,
Suspense
Suspense,
FunctionalComponent
} from '@vue/runtime-dom'
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 () => {
const state = reactive({ color: 'red' })
const root = document.createElement('div')