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

fix(custom-element): fix parent, observer and exposed issues when remove element and append it back immediately or after fully unmounted #12413

Open
wants to merge 4 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
48 changes: 48 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,54 @@ describe('defineCustomElement', () => {
expect(e._instance).toBeTruthy()
expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
})

// #12412
const ContextEl = defineCustomElement({
props: {
msg: String,
},
setup(props, { expose }) {
expose({
text: () => props.msg,
})
provide('context', props)
const context = inject('context', {}) as typeof props
return () => context.msg || props.msg
},
})
customElements.define('my-context-el', ContextEl)
test('remove element with child custom element and wait fully disconnected then append and change attribute', async () => {
container.innerHTML = `<div><my-context-el msg="msg1"><my-context-el></my-context-el></my-context-el></div>`
const parent = container.children[0].children[0] as VueElement & {
text: () => string
}
const child = parent.children[0] as VueElement
parent.remove()
await nextTick()
await nextTick() // wait two ticks for disconnect
expect('text' in parent).toBe(false)
container.appendChild(parent) // should not throw Error
await nextTick()
expect(parent.text()).toBe('msg1')
expect(parent.shadowRoot!.textContent).toBe('msg1')
expect(child.shadowRoot!.textContent).toBe('msg1')
parent.setAttribute('msg', 'msg2')
await nextTick()
expect(parent.shadowRoot!.textContent).toBe('msg2')
await nextTick()
expect(child.shadowRoot!.textContent).toBe('msg2')
})

test('move element to change parent and context', async () => {
container.innerHTML = `<my-context-el msg="msg1"></my-context-el><my-context-el msg="msg2"></my-context-el>`
const first = container.children[0] as VueElement,
second = container.children[1] as VueElement
await nextTick()
expect(second.shadowRoot!.textContent).toBe('msg2')
first.append(second)
await nextTick()
expect(second.shadowRoot!.textContent).toBe('msg1')
})
})

describe('props', () => {
Expand Down
42 changes: 31 additions & 11 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,20 @@ export class VueElement
this._connected = true

// locate nearest Vue custom element parent for provide/inject
let parent: Node | null = this
let parent: Node | null = this,
parentChanged = false
while (
(parent = parent && (parent.parentNode || (parent as ShadowRoot).host))
) {
if (parent instanceof VueElement) {
parentChanged = parent !== this._parent
this._parent = parent
break
}
}

// unmount if parent changed and previously mounted, should keep parent
if (this._instance && parentChanged) this._unmount(true)
if (!this._instance) {
if (this._resolved) {
this._setParent()
Expand All @@ -320,18 +324,31 @@ export class VueElement
}
}

private _unmount(keepParent?: boolean) {
if (this._ob) {
this._ob.disconnect()
this._ob = null
}
this._app && this._app.unmount()
if (this._instance) {
const exposed = this._instance.exposed
if (exposed) {
for (const key in exposed) {
delete this[key as keyof this]
}
}
this._instance.ce = undefined
}
this._app = this._instance = null
if (!keepParent) this._parent = undefined
this._resolved = false
}

disconnectedCallback(): void {
this._connected = false
nextTick(() => {
if (!this._connected) {
if (this._ob) {
this._ob.disconnect()
this._ob = null
}
// unmount
this._app && this._app.unmount()
if (this._instance) this._instance.ce = undefined
this._app = this._instance = null
this._unmount()
}
})
}
Expand Down Expand Up @@ -430,11 +447,14 @@ export class VueElement
if (!hasOwn(this, key)) {
// exposed properties are readonly
Object.defineProperty(this, key, {
configurable: true, // should be configurable to allow deleting when disconnected
// unwrap ref to be consistent with public instance behavior
get: () => unref(exposed[key]),
})
} else if (__DEV__) {
warn(`Exposed property "${key}" already exists on custom element.`)
} else {
delete exposed[key] // delete it from exposed in case of deleting wrong exposed key when disconnected
Copy link
Member

Choose a reason for hiding this comment

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

Could you provide more info on why need this line?

Copy link
Author

@lejunyang lejunyang Nov 18, 2024

Choose a reason for hiding this comment

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

This is to remove invalid exposed key.
Let's say we have an element with prop foo, and we accidentally expose an object {foo: 1}, it will be ignored as foo is already on this element. In disconnectedCallback, we'll delete exposed properties according to exposed, but deleting foo can cause an error as it's non-configurable prop.
Therefore, we should remove that key ahead

if (__DEV__)
warn(`Exposed property "${key}" already exists on custom element.`)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vue/__tests__/e2e/ssr-custom-element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ test('work with Teleport (shadowRoot: false)', async () => {
},
{ shadowRoot: false },
)
customElements.define('my-y', Y)
const P = defineSSRCustomElement(
{
render() {
Expand All @@ -110,6 +109,7 @@ test('work with Teleport (shadowRoot: false)', async () => {
{ shadowRoot: false },
)
customElements.define('my-p', P)
customElements.define('my-y', Y)
})

function getInnerHTML() {
Expand Down