Skip to content

fix(runtime-dom): before update should also set css vars #11561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 14, 2024
38 changes: 38 additions & 0 deletions packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,44 @@ describe('useCssVars', () => {
}
})

test('with delay mount child', async () => {
const state = reactive({ color: 'red' })
const value = ref(false)
const root = document.createElement('div')

const Child = {
setup() {
onMounted(() => {
const childEl = root.children[0]
expect(getComputedStyle(childEl!).getPropertyValue(`--color`)).toBe(
`red`,
)
})
return () => h('div', { id: 'childId' })
},
}
const App = {
setup() {
useCssVars(() => state)
return () => (value.value ? h(Child) : [h('span')])
},
}

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`)
}

// mount child
value.value = true
await nextTick()
for (const c of [].slice.call(root.children as any)) {
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
}
})

// #8826
test('with custom element', async () => {
const state = reactive({ color: 'red' })
Expand Down
8 changes: 8 additions & 0 deletions packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
Static,
type VNode,
getCurrentInstance,
onBeforeUpdate,
onMounted,
onUnmounted,
queuePostFlushCb,
warn,
watch,
} from '@vue/runtime-core'
Expand Down Expand Up @@ -47,6 +49,12 @@ export function useCssVars(getter: (ctx: any) => Record<string, string>): void {
updateTeleports(vars)
}

// handle cases where child component root is affected
// and triggers reflow in onMounted
onBeforeUpdate(() => {
queuePostFlushCb(setVars)
})

onMounted(() => {
// run setVars synchronously here, but run as post-effect on changes
watch(setVars, NOOP, { flush: 'post' })
Expand Down