defineCustomElement,
h,
nextTick,
+ onMounted,
reactive,
ref,
render,
`<css-vars-ce style="--color: red;"></css-vars-ce>`,
)
})
+
+ test('should set vars before child component onMount hook', () => {
+ const state = reactive({ color: 'red' })
+ const root = document.createElement('div')
+ let colorInOnMount
+
+ const App = {
+ setup() {
+ useCssVars(() => state)
+ onMounted(() => {
+ colorInOnMount = (
+ root.children[0] as HTMLElement
+ ).style.getPropertyValue(`--color`)
+ })
+ return () => h('div')
+ },
+ }
+
+ render(h(App), root)
+ expect(colorInOnMount).toBe(`red`)
+ })
})
Static,
type VNode,
getCurrentInstance,
- onBeforeMount,
onMounted,
onUnmounted,
warn,
- watchPostEffect,
+ watch,
} from '@vue/runtime-core'
-import { ShapeFlags } from '@vue/shared'
+import { NOOP, ShapeFlags } from '@vue/shared'
export const CSS_VAR_TEXT: unique symbol = Symbol(__DEV__ ? 'CSS_VAR_TEXT' : '')
/**
updateTeleports(vars)
}
- onBeforeMount(() => {
- watchPostEffect(setVars)
- })
-
onMounted(() => {
+ // run setVars synchronously here, but run as post-effect on changes
+ watch(setVars, NOOP, { flush: 'post' })
const ob = new MutationObserver(setVars)
ob.observe(instance.subTree.el!.parentNode, { childList: true })
onUnmounted(() => ob.disconnect())