Skip to content

Commit

Permalink
feat: add useCSSModule api (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Jul 5, 2020
1 parent a24951a commit 1ceac1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './lifecycle'
export * from './watch'
export * from './computed'
export * from './inject'
export { useCSSModule } from './useCssModule'
export { createApp } from './createApp'
export { nextTick } from './nextTick'
export { createElement as h } from './createElement'
23 changes: 23 additions & 0 deletions src/apis/useCssModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 as any)[name]
if (!mod) {
__DEV__ &&
warn(`Current instance does not have CSS module named "${name}".`)
return EMPTY_OBJ
}

return mod as Record<string, string>
}

0 comments on commit 1ceac1d

Please sign in to comment.