import {
createComponent,
createIf,
+ createPlainElement,
defineVaporComponent,
+ defineVaporCustomElement,
renderEffect,
setStyle,
template,
}
})
- test.todo('with custom element', async () => {})
+ test('with custom element', async () => {
+ const state = reactive({ color: 'red' })
+ const CE = defineVaporCustomElement({
+ setup() {
+ useVaporCssVars(() => state)
+ return template('<div>hello</div>', true)()
+ },
+ })
+
+ customElements.define('css-vars-ce', CE)
+
+ const { html } = define({
+ setup() {
+ return createPlainElement('css-vars-ce', null, null, true)
+ },
+ }).render()
+
+ expect(html()).toBe('<css-vars-ce style="--color: red;"></css-vars-ce>')
+
+ state.color = 'green'
+ await nextTick()
+ expect(html()).toBe('<css-vars-ce style="--color: green;"></css-vars-ce>')
+ })
test('should set vars before child component onMounted hook', () => {
const state = reactive({ color: 'red' })
import {
+ type GenericComponentInstance,
baseUseCssVars,
currentInstance,
setVarsOnNode,
instance,
() => resolveParentNode(instance.block),
getter,
- vars => setVarsOnBlock(instance.block, vars),
+ vars => setVars(instance, vars),
)
}
}
}
+function setVars(
+ instance: VaporComponentInstance,
+ vars: Record<string, string>,
+): void {
+ if ((instance as GenericComponentInstance).ce) {
+ setVarsOnNode((instance as GenericComponentInstance).ce as any, vars)
+ } else {
+ setVarsOnBlock(instance.block, vars)
+ }
+}
+
function setVarsOnBlock(block: Block, vars: Record<string, string>): void {
if (block instanceof Node) {
setVarsOnNode(block, vars)