Skip to content

fix(runtime-dom): Vapor useCssModule support #13711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export {
// plugins
export { getCurrentInstance } from './component'

/**
* @internal
*/
export { getCurrentGenericInstance } from './component'

// For raw render function users
export { h } from './h'
// Advanced render function utilities
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/helpers/useCssModule.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getCurrentInstance, warn } from '@vue/runtime-core'
import { getCurrentGenericInstance, warn } from '@vue/runtime-core'
import { EMPTY_OBJ } from '@vue/shared'

export function useCssModule(name = '$style'): Record<string, string> {
if (!__GLOBAL__) {
const instance = getCurrentInstance()!
const instance = getCurrentGenericInstance()
if (!instance) {
__DEV__ && warn(`useCssModule must be called inside setup()`)
return EMPTY_OBJ
Expand Down
55 changes: 55 additions & 0 deletions packages/runtime-vapor/__tests__/helpers/useCssModule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useCssModule } from '@vue/runtime-dom'
import { makeRender } from '../_utils'
import { defineVaporComponent, template } from '@vue/runtime-vapor'

const define = makeRender<any>()

describe('useCssModule', () => {
function mountWithModule(modules: any, name?: string) {
let res
define(
defineVaporComponent({
__cssModules: modules,
setup() {
res = useCssModule(name)
const n0 = template('<div></div>')()
return n0
},
}),
).render()
return res
}

test('basic usage', () => {
const modules = {
$style: {
red: 'red',
},
}
expect(mountWithModule(modules)).toMatchObject(modules.$style)
})

test('basic usage', () => {
const modules = {
foo: {
red: 'red',
},
}
expect(mountWithModule(modules, 'foo')).toMatchObject(modules.foo)
})

test('warn out of setup usage', () => {
useCssModule()
expect('must be called inside setup').toHaveBeenWarned()
})

test('warn missing injection', () => {
mountWithModule(undefined)
expect('instance does not have CSS modules').toHaveBeenWarned()
})

test('warn missing injection', () => {
mountWithModule({ $style: { red: 'red' } }, 'foo')
expect('instance does not have CSS module named "foo"').toHaveBeenWarned()
})
})