Skip to content

Commit 9f706a9

Browse files
committed
feat(runtime-dom): useCssVars
1 parent 18c537d commit 9f706a9

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

packages/runtime-core/src/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ export {
6565
} from './components/BaseTransition'
6666
// For using custom directives
6767
export { withDirectives } from './directives'
68-
// SFC CSS Modules
69-
export { useCSSModule } from './helpers/useCssModule'
7068
// SSR context
7169
export { useSSRContext, ssrContextKey } from './helpers/useSsrContext'
7270

packages/runtime-core/src/helpers/useCssModule.ts renamed to packages/runtime-dom/src/helpers/useCssModule.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { getCurrentInstance } from '../component'
1+
import { warn, getCurrentInstance } from '@vue/runtime-core'
22
import { EMPTY_OBJ } from '@vue/shared'
3-
import { warn } from '../warning'
43

5-
export const useCSSModule = (name = '$style'): Record<string, string> => {
4+
export function useCSSModule(name = '$style'): Record<string, string> {
65
if (!__GLOBAL__) {
76
const instance = getCurrentInstance()!
87
if (!instance) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
ComponentPublicInstance,
3+
getCurrentInstance,
4+
onMounted,
5+
watchEffect,
6+
warn,
7+
VNode,
8+
Fragment
9+
} from '@vue/runtime-core'
10+
import { ShapeFlags } from '@vue/shared/src'
11+
12+
export function useCSSVars(
13+
getter: (ctx: ComponentPublicInstance) => Record<string, string>
14+
) {
15+
const instance = getCurrentInstance()
16+
if (!instance) {
17+
__DEV__ &&
18+
warn(`useCssVars is called without current active component instance.`)
19+
return
20+
}
21+
onMounted(() => {
22+
watchEffect(() => {
23+
setVarsOnVNode(instance.vnode, getter(instance.proxy!))
24+
})
25+
})
26+
}
27+
28+
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
29+
// drill down HOCs until it's a non-component vnode
30+
while (vnode.component) {
31+
vnode = vnode.component.subTree
32+
}
33+
if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
34+
const style = vnode.el.style
35+
for (const key in vars) {
36+
style.setProperty(`--${key}`, vars[key])
37+
}
38+
} else if (vnode.type === Fragment) {
39+
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars))
40+
}
41+
}

packages/runtime-dom/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ function normalizeContainer(container: Element | string): Element | null {
113113
return container
114114
}
115115

116+
// SFC CSS utilities
117+
export { useCSSModule } from './helpers/useCssModule'
118+
export { useCSSVars } from './helpers/useCssVars'
119+
116120
// DOM-only components
117121
export { Transition, TransitionProps } from './components/Transition'
118122
export {

0 commit comments

Comments
 (0)