Skip to content

Commit 879ea17

Browse files
committed
test: tests for useCSSVars
1 parent bb47510 commit 879ea17

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
render,
3+
useCSSVars,
4+
h,
5+
reactive,
6+
nextTick,
7+
ComponentOptions
8+
} from '@vue/runtime-dom'
9+
10+
describe('useCssVars', () => {
11+
async function assertCssVars(
12+
getApp: (state: any) => ComponentOptions,
13+
scopeId?: string
14+
) {
15+
const state = reactive({ color: 'red' })
16+
const App = getApp(state)
17+
const root = document.createElement('div')
18+
const prefix = scopeId ? `${scopeId}-` : ``
19+
20+
render(h(App), root)
21+
for (const c of [].slice.call(root.children as any)) {
22+
expect((c as HTMLElement).style.getPropertyValue(`--${prefix}color`))
23+
}
24+
25+
state.color = 'green'
26+
await nextTick()
27+
for (const c of [].slice.call(root.children as any)) {
28+
expect((c as HTMLElement).style.getPropertyValue(`--${prefix}color`))
29+
}
30+
}
31+
32+
test('basic', async () => {
33+
await assertCssVars(state => ({
34+
setup() {
35+
// test receiving render context
36+
useCSSVars((ctx: any) => ({
37+
color: ctx.color
38+
}))
39+
return state
40+
},
41+
render() {
42+
return h('div')
43+
}
44+
}))
45+
})
46+
47+
test('on fragment root', async () => {
48+
await assertCssVars(state => ({
49+
setup() {
50+
useCSSVars(() => state)
51+
return () => [h('div'), h('div')]
52+
}
53+
}))
54+
})
55+
56+
test('on HOCs', async () => {
57+
const Child = () => [h('div'), h('div')]
58+
59+
await assertCssVars(state => ({
60+
setup() {
61+
useCSSVars(() => state)
62+
return () => h(Child)
63+
}
64+
}))
65+
})
66+
67+
test('with scopeId', async () => {
68+
const id = 'v-12345'
69+
70+
await assertCssVars(
71+
state => ({
72+
setup() {
73+
useCSSVars(() => state, id)
74+
return () => h('div')
75+
}
76+
}),
77+
id
78+
)
79+
})
80+
})

packages/runtime-dom/src/helpers/useCssVars.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
import { ShapeFlags } from '@vue/shared/src'
1111

1212
export function useCSSVars(
13-
getter: (ctx: ComponentPublicInstance) => Record<string, string>
13+
getter: (ctx: ComponentPublicInstance) => Record<string, string>,
14+
scopeId?: string
1415
) {
1516
const instance = getCurrentInstance()
1617
if (!instance) {
@@ -20,22 +21,27 @@ export function useCSSVars(
2021
}
2122
onMounted(() => {
2223
watchEffect(() => {
23-
setVarsOnVNode(instance.vnode, getter(instance.proxy!))
24+
setVarsOnVNode(instance.vnode, getter(instance.proxy!), scopeId)
2425
})
2526
})
2627
}
2728

28-
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
29+
function setVarsOnVNode(
30+
vnode: VNode,
31+
vars: Record<string, string>,
32+
scopeId?: string
33+
) {
2934
// drill down HOCs until it's a non-component vnode
3035
while (vnode.component) {
3136
vnode = vnode.component.subTree
3237
}
3338
if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
3439
const style = vnode.el.style
40+
const prefix = scopeId ? scopeId + '-' : ''
3541
for (const key in vars) {
36-
style.setProperty(`--${key}`, vars[key])
42+
style.setProperty(`--${prefix}${key}`, vars[key])
3743
}
3844
} else if (vnode.type === Fragment) {
39-
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars))
45+
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars, scopeId))
4046
}
4147
}

0 commit comments

Comments
 (0)