Skip to content

Commit

Permalink
feat: add useCSSModule api
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Jul 3, 2020
1 parent 43d8732 commit 0645989
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/apis/useCssModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getCurrentInstance } from '../runtimeContext'
import { warn } from '../utils'

const EMPTY_OBJ: { readonly [key: string]: string } = __DEV__
? Object.freeze({})
: {}

export const useCSSModule = (name = '$style'): Record<string, string> => {
const instance = getCurrentInstance()
if (!instance) {
__DEV__ && warn(`useCSSModule must be called inside setup()`)
return EMPTY_OBJ
}

const mod = instance[name]
if (!mod) {
__DEV__ && warn(`Current instance does not have CSS module named "${name}".`)
return EMPTY_OBJ
}

return mod
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './apis/lifecycle'
export * from './apis/watch'
export * from './apis/computed'
export * from './apis/inject'
export * from './apis/useCssModule'

// auto install when using CDN
if (typeof window !== 'undefined' && window.Vue) {
Expand Down

0 comments on commit 0645989

Please sign in to comment.