Skip to content

fix(custom-element): ensure proper remount and prevent redundant slot parsing with shadowRoot false #13201

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
86 changes: 86 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,92 @@ describe('defineCustomElement', () => {
expect(target.innerHTML).toBe(`<span>default</span>`)
app.unmount()
})

test('toggle nested custom element with shadowRoot: false', async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @edison1105 , thanks for the fix, it works fine in our app as well!

I'm not sure if it is related to the current issue or I need to create a separate one, but If in custom element slot we have a v-if clause, then it never renders even if the clause is true, but if it's v-show - it works fine.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to add a test case to this branch or I can create an issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please create new one, I will take a look later.

Copy link

@rbecheras rbecheras Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this @edison1105
The issue is created here #13206

customElements.define(
'my-el-child-shadow-false',
defineCustomElement(
{
render(ctx: any) {
return h('div', null, [renderSlot(ctx.$slots, 'default')])
},
},
{ shadowRoot: false },
),
)
const ChildWrapper = {
render() {
return h('my-el-child-shadow-false', null, 'child')
},
}

customElements.define(
'my-el-parent-shadow-false',
defineCustomElement(
{
props: {
isShown: { type: Boolean, required: true },
},
render(ctx: any, _: any, $props: any) {
return $props.isShown
? h('div', { key: 0 }, [renderSlot(ctx.$slots, 'default')])
: null
},
},
{ shadowRoot: false },
),
)
const ParentWrapper = {
props: {
isShown: { type: Boolean, required: true },
},
render(ctx: any, _: any, $props: any) {
return h('my-el-parent-shadow-false', { isShown: $props.isShown }, [
renderSlot(ctx.$slots, 'default'),
])
},
}

const isShown = ref(true)
const App = {
render() {
return h(ParentWrapper, { isShown: isShown.value } as any, {
default: () => [h(ChildWrapper)],
})
},
}
const container = document.createElement('div')
document.body.appendChild(container)
const app = createApp(App)
app.mount(container)
expect(container.innerHTML).toBe(
`<my-el-parent-shadow-false is-shown="" data-v-app="">` +
`<div>` +
`<my-el-child-shadow-false data-v-app="">` +
`<div>child</div>` +
`</my-el-child-shadow-false>` +
`</div>` +
`</my-el-parent-shadow-false>`,
)

isShown.value = false
await nextTick()
expect(container.innerHTML).toBe(
`<my-el-parent-shadow-false data-v-app=""><!----></my-el-parent-shadow-false>`,
)

isShown.value = true
await nextTick()
expect(container.innerHTML).toBe(
`<my-el-parent-shadow-false data-v-app="" is-shown="">` +
`<div>` +
`<my-el-child-shadow-false data-v-app="">` +
`<div>child</div>` +
`</my-el-child-shadow-false>` +
`</div>` +
`</my-el-parent-shadow-false>`,
)
})
})

describe('helpers', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ export class VueElement
// avoid resolving component if it's not connected
if (!this.isConnected) return

if (!this.shadowRoot) {
// avoid re-parsing slots if already resolved
if (!this.shadowRoot && !this._resolved) {
this._parseSlots()
}
this._connected = true
Expand All @@ -298,8 +299,7 @@ export class VueElement

if (!this._instance) {
if (this._resolved) {
this._setParent()
this._update()
this._mount(this._def)
} else {
if (parent && parent._pendingResolve) {
this._pendingResolve = parent._pendingResolve.then(() => {
Expand Down