Skip to content

Commit 67722ba

Browse files
authored
fix(runtime-dom): fix css v-bind for suspensed components (#8523)
close #8520
1 parent 54a6afa commit 67722ba

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

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

+57
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,63 @@ describe('useCssVars', () => {
118118
}
119119
})
120120

121+
test('with v-if & async component & suspense', async () => {
122+
const state = reactive({ color: 'red' })
123+
const root = document.createElement('div')
124+
const show = ref(false)
125+
let resolveAsync: any
126+
let asyncPromise: any
127+
128+
const AsyncComp = {
129+
setup() {
130+
useCssVars(() => state)
131+
asyncPromise = new Promise(r => {
132+
resolveAsync = () => {
133+
r(() => h('p', 'default'))
134+
}
135+
})
136+
return asyncPromise
137+
},
138+
}
139+
140+
const App = {
141+
setup() {
142+
return () =>
143+
h(Suspense, null, {
144+
default: h('div', {}, show.value ? h(AsyncComp) : h('p')),
145+
})
146+
},
147+
}
148+
149+
render(h(App), root)
150+
await nextTick()
151+
// AsyncComp resolve
152+
show.value = true
153+
await nextTick()
154+
resolveAsync()
155+
await asyncPromise.then(() => {})
156+
// Suspense effects flush
157+
await nextTick()
158+
// css vars use with default tree
159+
for (const c of [].slice.call(root.children as any)) {
160+
expect(
161+
((c as any).children[0] as HTMLElement).style.getPropertyValue(
162+
`--color`,
163+
),
164+
).toBe(`red`)
165+
}
166+
167+
state.color = 'green'
168+
await nextTick()
169+
for (const c of [].slice.call(root.children as any)) {
170+
expect(
171+
((c as any).children[0] as HTMLElement).style.getPropertyValue(
172+
`--color`,
173+
),
174+
).toBe('green')
175+
}
176+
})
177+
121178
test('with subTree changed', async () => {
122179
const state = reactive({ color: 'red' })
123180
const value = ref(true)

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ export function useCssVars(getter: (ctx: any) => Record<string, string>) {
4242
updateTeleports(vars)
4343
}
4444

45-
watchPostEffect(setVars)
46-
4745
onMounted(() => {
46+
watchPostEffect(setVars)
4847
const ob = new MutationObserver(setVars)
4948
ob.observe(instance.subTree.el!.parentNode, { childList: true })
5049
onUnmounted(() => ob.disconnect())

0 commit comments

Comments
 (0)