Skip to content

Commit f9149f9

Browse files
committed
wip: support set style vars on custom element
1 parent ecd9cae commit f9149f9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
createComponent,
33
createIf,
4+
createPlainElement,
45
defineVaporComponent,
6+
defineVaporCustomElement,
57
renderEffect,
68
setStyle,
79
template,
@@ -258,4 +260,30 @@ describe('useVaporCssVars', () => {
258260

259261
expect(colorInOnMount).toBe(`red`)
260262
})
263+
264+
test('with custom element', () => {
265+
const CE = defineVaporCustomElement({
266+
setup() {
267+
useVaporCssVars(() => ({
268+
v727f5271: color.value,
269+
}))
270+
271+
const color = ref('red')
272+
return template('<h1></h1>', true)()
273+
},
274+
})
275+
276+
customElements.define('custom-el-with-css-vars', CE)
277+
278+
const { html } = define({
279+
setup() {
280+
return createPlainElement('custom-el-with-css-vars', null, null, true)
281+
},
282+
}).render()
283+
284+
// should have css vars
285+
expect(html()).toBe(
286+
'<custom-el-with-css-vars style="--v727f5271: red;"></custom-el-with-css-vars>',
287+
)
288+
})
261289
})

packages/runtime-vapor/src/helpers/useCssVars.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
type GenericComponentInstance,
23
baseUseCssVars,
34
currentInstance,
45
setVarsOnNode,
@@ -14,7 +15,7 @@ export function useVaporCssVars(getter: () => Record<string, string>): void {
1415
instance,
1516
() => resolveParentNode(instance.block),
1617
getter,
17-
vars => setVarsOnBlock(instance.block, vars),
18+
vars => setVars(instance, vars),
1819
)
1920
}
2021

@@ -30,6 +31,17 @@ function resolveParentNode(block: Block): Node {
3031
}
3132
}
3233

34+
function setVars(
35+
instance: VaporComponentInstance,
36+
vars: Record<string, string>,
37+
): void {
38+
if ((instance as GenericComponentInstance).ce) {
39+
setVarsOnNode((instance as GenericComponentInstance).ce as any, vars)
40+
} else {
41+
setVarsOnBlock(instance.block, vars)
42+
}
43+
}
44+
3345
function setVarsOnBlock(block: Block, vars: Record<string, string>): void {
3446
if (block instanceof Node) {
3547
setVarsOnNode(block, vars)

0 commit comments

Comments
 (0)