| 18 | * @private |
| 19 | */ |
| 20 | export function useCssVars( |
| 21 | getter: (ctx: any) => Record<string, unknown>, |
| 22 | ): void { |
| 23 | if (!__BROWSER__ && !__TEST__) return |
| 24 | |
| 25 | const instance = getCurrentInstance() |
| 26 | /* v8 ignore start */ |
| 27 | if (!instance) { |
| 28 | __DEV__ && |
| 29 | warn(`useCssVars is called without current active component instance.`) |
| 30 | return |
| 31 | } |
| 32 | /* v8 ignore stop */ |
| 33 | |
| 34 | const updateTeleports = (instance.ut = (vars = getter(instance.proxy)) => { |
| 35 | Array.from( |
| 36 | document.querySelectorAll(`[data-v-owner="${instance.uid}"]`), |
| 37 | ).forEach(node => setVarsOnNode(node, vars)) |
| 38 | }) |
| 39 | |
| 40 | if (__DEV__) { |
| 41 | instance.getCssVars = () => getter(instance.proxy) |
| 42 | } |
| 43 | |
| 44 | const setVars = () => { |
| 45 | const vars = getter(instance.proxy) |
| 46 | if (instance.ce) { |
| 47 | setVarsOnNode(instance.ce as any, vars) |
| 48 | } else { |
| 49 | setVarsOnVNode(instance.subTree, vars) |
| 50 | } |
| 51 | updateTeleports(vars) |
| 52 | } |
| 53 | |
| 54 | // handle cases where child component root is affected |
| 55 | // and triggers reflow in onMounted |
| 56 | onBeforeUpdate(() => { |
| 57 | queuePostFlushCb(setVars) |
| 58 | }) |
| 59 | |
| 60 | onMounted(() => { |
| 61 | // run setVars synchronously here, but run as post-effect on changes |
| 62 | watch(setVars, NOOP, { flush: 'post' }) |
| 63 | const ob = new MutationObserver(setVars) |
| 64 | ob.observe(instance.subTree.el!.parentNode, { childList: true }) |
| 65 | onUnmounted(() => ob.disconnect()) |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | function setVarsOnVNode(vnode: VNode, vars: Record<string, unknown>) { |
| 70 | if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) { |