Skip to content
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

feat(custom-element): Support css :host selector #8830

Merged
merged 13 commits into from
Aug 6, 2024
Merged
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
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
Loading