Skip to content

Commit

Permalink
fix(runtime): re-relocate slot if parent element's tagname changes
Browse files Browse the repository at this point in the history
If a slot is located in an element and that element's tag is dynamically changed (e.g. from `p` to `span`), we need to re-relocate the slot on re-render

STENCIL-672: slot element loses its parent reference and disappears when its parent is rendered conditionally

Fixes: #4284, #3913
  • Loading branch information
Tanner Reits committed Nov 30, 2023
1 parent 33f5158 commit 16136a2
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/runtime/vdom/vdom-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,18 @@ render() {
}
}

if (BUILD.experimentalSlotFixes) {
// Figure out if there are any slotRefNodes that are also nodes to relocate.
// If so, those nodes need to be relocated before any content is relocated to them.
relocateNodes.forEach((relocateData, _, arr) => {
const indexOfNodeToRelocate = arr.findIndex((r) => r.$nodeToRelocate$ === relocateData.$slotRefNode$);
if (indexOfNodeToRelocate > -1) {
arr.unshift(arr[indexOfNodeToRelocate]);
arr.splice(indexOfNodeToRelocate + 1, 1);
}
});
}

for (const relocateData of relocateNodes) {
const nodeToRelocate = relocateData.$nodeToRelocate$;
const slotRefNode = relocateData.$slotRefNode$;
Expand Down Expand Up @@ -1027,7 +1039,7 @@ render() {
// has a different next sibling or parent relocated

if (nodeToRelocate !== insertBeforeNode) {
if (!nodeToRelocate['s-hn'] && nodeToRelocate['s-ol']) {
if (!BUILD.experimentalSlotFixes && !nodeToRelocate['s-hn'] && nodeToRelocate['s-ol']) {
// probably a component in the index.html that doesn't have its hostname set
nodeToRelocate['s-hn'] = nodeToRelocate['s-ol'].parentNode.nodeName;
}
Expand Down

0 comments on commit 16136a2

Please # to comment.