MCPcopy
hub / github.com/vuejs/core / useCssVars

Function useCssVars

packages/runtime-dom/src/helpers/useCssVars.ts:20–67  ·  view source on GitHub ↗
(
  getter: (ctx: any) => Record<string, unknown>,
)

Source from the content-addressed store, hash-verified

18 * @private
19 */
20export 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
69function setVarsOnVNode(vnode: VNode, vars: Record<string, unknown>) {
70 if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {

Callers 2

setupFunction · 0.90
setupFunction · 0.90

Calls 7

getCurrentInstanceFunction · 0.90
warnFunction · 0.90
queuePostFlushCbFunction · 0.90
watchFunction · 0.90
setVarsOnNodeFunction · 0.85
forEachMethod · 0.80
getterFunction · 0.50

Tested by 2

setupFunction · 0.72
setupFunction · 0.72