Skip to content

Commit fce4707

Browse files
committedNov 27, 2024
fix(custom-element): fix parent resolving issue when slotted in shadow dom (#12479)
1 parent 5a5406d commit fce4707

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
 

‎packages/runtime-dom/__tests__/customElement.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,31 @@ describe('defineCustomElement', () => {
708708
`<div>changedA! changedB!</div>`,
709709
)
710710
})
711+
712+
test('should resolve correct parent when element is slotted in shadow DOM', async () => {
713+
const GrandParent = defineCustomElement({
714+
provide: {
715+
foo: ref('GrandParent'),
716+
},
717+
render() {
718+
return h('my-parent-in-shadow', h('slot'))
719+
},
720+
})
721+
const Parent = defineCustomElement({
722+
provide: {
723+
foo: ref('Parent'),
724+
},
725+
render() {
726+
return h('slot')
727+
},
728+
})
729+
customElements.define('my-grand-parent', GrandParent)
730+
customElements.define('my-parent-in-shadow', Parent)
731+
container.innerHTML = `<my-grand-parent><my-consumer></my-consumer></my-grand-parent>`
732+
const grandParent = container.childNodes[0] as VueElement,
733+
consumer = grandParent.firstElementChild as VueElement
734+
expect(consumer.shadowRoot!.textContent).toBe('Parent')
735+
})
711736
})
712737

713738
describe('styles', () => {

‎packages/runtime-dom/src/apiCustomElement.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,12 @@ export class VueElement
288288
// locate nearest Vue custom element parent for provide/inject
289289
let parent: Node | null = this
290290
while (
291-
(parent = parent && (parent.parentNode || (parent as ShadowRoot).host))
291+
(parent =
292+
parent &&
293+
// #12479 should check assignedSlot first to get correct parent
294+
((parent as Element).assignedSlot ||
295+
parent.parentNode ||
296+
(parent as ShadowRoot).host))
292297
) {
293298
if (parent instanceof VueElement) {
294299
this._parent = parent

0 commit comments

Comments
 (0)