test: tests for useCSSVars

This commit is contained in:
Evan You
2020-07-10 09:44:00 -04:00
parent bb47510aae
commit 879ea17985
2 changed files with 91 additions and 5 deletions

View File

@@ -10,7 +10,8 @@ import {
import { ShapeFlags } from '@vue/shared/src'
export function useCSSVars(
getter: (ctx: ComponentPublicInstance) => Record<string, string>
getter: (ctx: ComponentPublicInstance) => Record<string, string>,
scopeId?: string
) {
const instance = getCurrentInstance()
if (!instance) {
@@ -20,22 +21,27 @@ export function useCSSVars(
}
onMounted(() => {
watchEffect(() => {
setVarsOnVNode(instance.vnode, getter(instance.proxy!))
setVarsOnVNode(instance.vnode, getter(instance.proxy!), scopeId)
})
})
}
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
function setVarsOnVNode(
vnode: VNode,
vars: Record<string, string>,
scopeId?: string
) {
// drill down HOCs until it's a non-component vnode
while (vnode.component) {
vnode = vnode.component.subTree
}
if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
const style = vnode.el.style
const prefix = scopeId ? scopeId + '-' : ''
for (const key in vars) {
style.setProperty(`--${key}`, vars[key])
style.setProperty(`--${prefix}${key}`, vars[key])
}
} else if (vnode.type === Fragment) {
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars))
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars, scopeId))
}
}