Skip to content

Commit abe8fc2

Browse files
authored
fix(runtime-dom): useCssModule vapor support (#13711)
1 parent 615db5e commit abe8fc2

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

packages/runtime-core/src/componentCurrentInstance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const setCurrentInstance = (
9292
}
9393
}
9494

95-
const internalOptions = ['ce'] as const
95+
const internalOptions = ['ce', 'type'] as const
9696

9797
/**
9898
* @internal

packages/runtime-dom/src/helpers/useCssModule.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { getCurrentInstance, warn } from '@vue/runtime-core'
1+
import { useInstanceOption, warn } from '@vue/runtime-core'
22
import { EMPTY_OBJ } from '@vue/shared'
33

44
export function useCssModule(name = '$style'): Record<string, string> {
55
if (!__GLOBAL__) {
6-
const instance = getCurrentInstance()!
7-
if (!instance) {
6+
const { hasInstance, value: type } = useInstanceOption('type', true)
7+
if (!hasInstance) {
88
__DEV__ && warn(`useCssModule must be called inside setup()`)
99
return EMPTY_OBJ
1010
}
11-
const modules = instance.type.__cssModules
11+
const modules = type!.__cssModules
1212
if (!modules) {
1313
__DEV__ && warn(`Current instance does not have CSS modules injected.`)
1414
return EMPTY_OBJ
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useCssModule } from '@vue/runtime-dom'
2+
import { makeRender } from '../_utils'
3+
import { defineVaporComponent, template } from '@vue/runtime-vapor'
4+
5+
const define = makeRender<any>()
6+
7+
describe('useCssModule', () => {
8+
function mountWithModule(modules: any, name?: string) {
9+
let res
10+
define(
11+
defineVaporComponent({
12+
__cssModules: modules,
13+
setup() {
14+
res = useCssModule(name)
15+
const n0 = template('<div></div>')()
16+
return n0
17+
},
18+
}),
19+
).render()
20+
return res
21+
}
22+
23+
test('basic usage', () => {
24+
const modules = {
25+
$style: {
26+
red: 'red',
27+
},
28+
}
29+
expect(mountWithModule(modules)).toMatchObject(modules.$style)
30+
})
31+
32+
test('basic usage', () => {
33+
const modules = {
34+
foo: {
35+
red: 'red',
36+
},
37+
}
38+
expect(mountWithModule(modules, 'foo')).toMatchObject(modules.foo)
39+
})
40+
41+
test('warn out of setup usage', () => {
42+
useCssModule()
43+
expect('must be called inside setup').toHaveBeenWarned()
44+
})
45+
46+
test('warn missing injection', () => {
47+
mountWithModule(undefined)
48+
expect('instance does not have CSS modules').toHaveBeenWarned()
49+
})
50+
51+
test('warn missing injection', () => {
52+
mountWithModule({ $style: { red: 'red' } }, 'foo')
53+
expect('instance does not have CSS module named "foo"').toHaveBeenWarned()
54+
})
55+
})

0 commit comments

Comments
 (0)