11import {
22 Fragment ,
3+ type GenericComponentInstance ,
34 Static ,
45 type VNode ,
56 getCurrentInstance ,
@@ -22,51 +23,25 @@ export function useCssVars(
2223) : void {
2324 if ( ! __BROWSER__ && ! __TEST__ ) return
2425
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 )
26+ const instance = getCurrentInstance ( ) ! // to be check in baseUseCssVars
27+ const getVars = ( ) => getter ( instance . proxy )
28+ const setVars = ( vars : Record < string , any > ) => {
4629 if ( instance . ce ) {
4730 setVarsOnNode ( instance . ce as any , vars )
4831 } else {
4932 setVarsOnVNode ( instance . subTree , vars )
5033 }
51- updateTeleports ( vars )
5234 }
5335
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- } )
36+ baseUseCssVars (
37+ instance as GenericComponentInstance ,
38+ ( ) => instance . subTree . el ! . parentNode ! ,
39+ getVars ,
40+ setVars ,
41+ )
6742}
6843
69- function setVarsOnVNode ( vnode : VNode , vars : Record < string , unknown > ) {
44+ function setVarsOnVNode ( vnode : VNode , vars : Record < string , string > ) {
7045 if ( __FEATURE_SUSPENSE__ && vnode . shapeFlag & ShapeFlags . SUSPENSE ) {
7146 const suspense = vnode . suspense !
7247 vnode = suspense . activeBranch !
@@ -96,7 +71,60 @@ function setVarsOnVNode(vnode: VNode, vars: Record<string, unknown>) {
9671 }
9772}
9873
99- function setVarsOnNode ( el : Node , vars : Record < string , unknown > ) {
74+ /**
75+ * @internal
76+ * shared between vdom and vapor
77+ */
78+ export function baseUseCssVars (
79+ instance : GenericComponentInstance | null ,
80+ getParentNode : ( ) => Node ,
81+ getVars : ( ) => Record < string , any > ,
82+ setVars : ( vars : Record < string , any > ) => void ,
83+ ) : void {
84+ /* v8 ignore start */
85+ if ( ! instance ) {
86+ __DEV__ &&
87+ warn ( `useCssVars is called without current active component instance.` )
88+ return
89+ }
90+ /* v8 ignore stop */
91+
92+ if ( __DEV__ ) {
93+ instance . getCssVars = getVars
94+ }
95+
96+ const updateTeleports = ( instance . ut = ( vars = getVars ( ) ) => {
97+ Array . from (
98+ document . querySelectorAll ( `[data-v-owner="${ instance . uid } "]` ) ,
99+ ) . forEach ( node => setVarsOnNode ( node , vars ) )
100+ } )
101+
102+ const applyCssCars = ( ) => {
103+ const vars = getVars ( )
104+ setVars ( vars )
105+ updateTeleports ( vars )
106+ }
107+
108+ // handle cases where child component root is affected
109+ // and triggers reflow in onMounted
110+ onBeforeUpdate ( ( ) => {
111+ queuePostFlushCb ( applyCssCars )
112+ } )
113+
114+ onMounted ( ( ) => {
115+ // run setVars synchronously here, but run as post-effect on changes
116+ watch ( applyCssCars , NOOP , { flush : 'post' } )
117+ const ob = new MutationObserver ( applyCssCars )
118+ ob . observe ( getParentNode ( ) , { childList : true } )
119+ onUnmounted ( ( ) => ob . disconnect ( ) )
120+ } )
121+ }
122+
123+ /**
124+ * @internal
125+ * shared between vdom and vapor
126+ */
127+ export function setVarsOnNode ( el : Node , vars : Record < string , string > ) : void {
100128 if ( el . nodeType === 1 ) {
101129 const style = ( el as HTMLElement ) . style
102130 let cssText = ''
0 commit comments