Skip to content

Commit

Permalink
feat(custom-element): support css :host selector by applying css va…
Browse files Browse the repository at this point in the history
…rs on host element (#8830)

close #8826
  • Loading branch information
baiwusanyu-c authored Aug 6, 2024
1 parent 917c063 commit 03a9ea2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Suspense,
Teleport,
createStaticVNode,
defineCustomElement,
h,
nextTick,
reactive,
Expand Down Expand Up @@ -381,4 +382,26 @@ describe('useCssVars', () => {
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
}
})

// #8826
test('with custom element', async () => {
const state = reactive({ color: 'red' })
const container = document.createElement('div')
const App = defineCustomElement({
styles: [`div { color: red; }`],
setup() {
useCssVars(() => state)
return () => {
return h('div', 'hello')
}
},
})
customElements.define('css-vars-ce', App)
container.innerHTML = `<css-vars-ce></css-vars-ce>`
document.body.appendChild(container)
await nextTick()
expect(container.innerHTML).toBe(
`<css-vars-ce style="--color: red;"></css-vars-ce>`,
)
})
})
6 changes: 5 additions & 1 deletion packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export function useCssVars(getter: (ctx: any) => Record<string, string>) {

const setVars = () => {
const vars = getter(instance.proxy)
setVarsOnVNode(instance.subTree, vars)
if (instance.ce) {
setVarsOnNode(instance.ce as any, vars)
} else {
setVarsOnVNode(instance.subTree, vars)
}
updateTeleports(vars)
}

Expand Down

0 comments on commit 03a9ea2

Please sign in to comment.