@@ -5,13 +5,14 @@ import {
5
5
watchEffect ,
6
6
warn ,
7
7
VNode ,
8
- Fragment
8
+ Fragment ,
9
+ ComponentInternalInstance
9
10
} from '@vue/runtime-core'
10
11
import { ShapeFlags } from '@vue/shared/src'
11
12
12
13
export function useCSSVars (
13
14
getter : ( ctx : ComponentPublicInstance ) => Record < string , string > ,
14
- scopeId ?: string
15
+ scoped = false
15
16
) {
16
17
const instance = getCurrentInstance ( )
17
18
if ( ! instance ) {
@@ -21,27 +22,36 @@ export function useCSSVars(
21
22
}
22
23
onMounted ( ( ) => {
23
24
watchEffect ( ( ) => {
24
- setVarsOnVNode ( instance . vnode , getter ( instance . proxy ! ) , scopeId )
25
+ setVarsOnVNode (
26
+ instance . subTree ,
27
+ getter ( instance . proxy ! ) ,
28
+ instance ,
29
+ scoped
30
+ )
25
31
} )
26
32
} )
27
33
}
28
34
29
35
function setVarsOnVNode (
30
36
vnode : VNode ,
31
37
vars : Record < string , string > ,
32
- scopeId ?: string
38
+ owner : ComponentInternalInstance ,
39
+ scoped : boolean
33
40
) {
34
41
// drill down HOCs until it's a non-component vnode
35
42
while ( vnode . component ) {
36
43
vnode = vnode . component . subTree
37
44
}
38
45
if ( vnode . shapeFlag & ShapeFlags . ELEMENT && vnode . el ) {
39
46
const style = vnode . el . style
40
- const prefix = scopeId ? scopeId + '-' : ''
47
+ const prefix =
48
+ scoped && owner . type . __scopeId ? `${ owner . type . __scopeId } -` : ``
41
49
for ( const key in vars ) {
42
50
style . setProperty ( `--${ prefix } ${ key } ` , vars [ key ] )
43
51
}
44
52
} else if ( vnode . type === Fragment ) {
45
- ; ( vnode . children as VNode [ ] ) . forEach ( c => setVarsOnVNode ( c , vars , scopeId ) )
53
+ ; ( vnode . children as VNode [ ] ) . forEach ( c =>
54
+ setVarsOnVNode ( c , vars , owner , scoped )
55
+ )
46
56
}
47
57
}
0 commit comments