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(runtime-core): add 'container' to component instance #8195

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ export interface ComponentInternalInstance {
parent: ComponentInternalInstance | null
root: ComponentInternalInstance
appContext: AppContext
/**
* ShadowRoot of custom element if it is
*/
container: ShadowRoot | null
/**
* Vnode representing this component in its parent's vdom tree
*/
Expand Down Expand Up @@ -497,6 +501,7 @@ export function createComponentInstance(
type,
parent,
appContext,
container: null, // will be set synchronously before setup
root: null!, // to be immediately set
next: null,
subTree: null!, // will be set synchronously right after creation
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ function baseCreateRenderer(
parentComponent,
parentSuspense
))
instance.isCE && (instance.container = container as ShadowRoot)

if (__DEV__ && instance.type.__hmrId) {
registerHMR(instance)
Expand Down Expand Up @@ -2237,6 +2238,8 @@ function baseCreateRenderer(
invokeArrayFns(bum)
}

instance.container = null

if (
__COMPAT__ &&
isCompatEnabled(DeprecationTypes.INSTANCE_EVENT_HOOKS, instance)
Expand Down
21 changes: 21 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
defineAsyncComponent,
defineComponent,
defineCustomElement,
getCurrentInstance,
h,
inject,
nextTick,
Expand Down Expand Up @@ -693,4 +694,24 @@ describe('defineCustomElement', () => {
)
})
})

describe('shadowRoot', () => {
// #6113
test('shadowRoot accessible for css-in-js', () => {
const Foo = defineCustomElement({
setup() {
const ins = getCurrentInstance()!
const style = document.createElement('style')
style.innerHTML = `div { color: red; }`
ins.container!.appendChild(style)
return () => h('div', 'hello')
}
})
customElements.define('my-el', Foo)
container.innerHTML = `<my-el></my-el>`
const el = container.childNodes[0] as VueElement
const style = el.shadowRoot?.querySelector('style')!
expect(style.textContent).toBe(`div { color: red; }`)
})
})
})