Skip to content

Commit 1ceac1d

Browse files
authored
feat: add useCSSModule api (#420)
1 parent a24951a commit 1ceac1d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './lifecycle'
33
export * from './watch'
44
export * from './computed'
55
export * from './inject'
6+
export { useCSSModule } from './useCssModule'
67
export { createApp } from './createApp'
78
export { nextTick } from './nextTick'
89
export { createElement as h } from './createElement'

src/apis/useCssModule.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getCurrentInstance } from '../runtimeContext'
2+
import { warn } from '../utils'
3+
4+
const EMPTY_OBJ: { readonly [key: string]: string } = __DEV__
5+
? Object.freeze({})
6+
: {}
7+
8+
export const useCSSModule = (name = '$style'): Record<string, string> => {
9+
const instance = getCurrentInstance()
10+
if (!instance) {
11+
__DEV__ && warn(`useCSSModule must be called inside setup()`)
12+
return EMPTY_OBJ
13+
}
14+
15+
const mod = (instance as any)[name]
16+
if (!mod) {
17+
__DEV__ &&
18+
warn(`Current instance does not have CSS module named "${name}".`)
19+
return EMPTY_OBJ
20+
}
21+
22+
return mod as Record<string, string>
23+
}

0 commit comments

Comments
 (0)