Skip to content

Commit 2ae6f5f

Browse files
fix(vdom): hide fallback slot when content present in scoped/non-shadow components (#2650)
1 parent c91e0c8 commit 2ae6f5f

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

src/runtime/vdom/test/scoped-slot.spec.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -755,4 +755,48 @@ describe('scoped slot', () => {
755755
// expect(root.firstElementChild.firstElementChild.children[1].firstElementChild.children[1].nodeName).toBe('GOAT');
756756
// expect(root.firstElementChild.firstElementChild.children[1].firstElementChild.children[1].textContent).toBe('hey goat!');
757757
});
758+
759+
it('should hide the slot\'s fallback content for a scoped component when slot content passed in', async () => {
760+
@Component({ tag: 'fallback-test', scoped: true })
761+
class ScopedFallbackSlotTest {
762+
render() {
763+
return (
764+
<div>
765+
<slot>
766+
<p>Fallback Content</p>
767+
</slot>
768+
</div>
769+
);
770+
}
771+
}
772+
const { root } = await newSpecPage({
773+
components: [ScopedFallbackSlotTest],
774+
html: `<fallback-test><span>Content</span></fallback-test>`,
775+
});
776+
777+
expect(root.firstElementChild.children[1].nodeName).toBe('SLOT-FB');
778+
expect(root.firstElementChild.children[1]).toHaveAttribute('hidden');
779+
});
780+
781+
it('should hide the slot\'s fallback content for a non-shadow component when slot content passed in', async () => {
782+
@Component({ tag: 'fallback-test', shadow: false })
783+
class NonShadowFallbackSlotTest {
784+
render() {
785+
return (
786+
<div>
787+
<slot>
788+
<p>Fallback Content</p>
789+
</slot>
790+
</div>
791+
);
792+
}
793+
}
794+
const { root } = await newSpecPage({
795+
components: [NonShadowFallbackSlotTest],
796+
html: `<fallback-test><span>Content</span></fallback-test>`,
797+
});
798+
799+
expect(root.firstElementChild.children[1].nodeName).toBe('SLOT-FB');
800+
expect(root.firstElementChild.children[1]).toHaveAttribute('hidden');
801+
});
758802
});

src/runtime/vdom/vdom-render.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -418,24 +418,24 @@ const updateFallbackSlotVisibility = (elm: d.RenderNode) => {
418418
childNode.hidden = false;
419419

420420
for (j = 0; j < ilen; j++) {
421-
if (childNodes[j]['s-hn'] !== childNode['s-hn']) {
422-
// this sibling node is from a different component
423-
nodeType = childNodes[j].nodeType;
424-
425-
if (slotNameAttr !== '') {
426-
// this is a named fallback slot node
427-
if (nodeType === NODE_TYPE.ElementNode && slotNameAttr === childNodes[j].getAttribute('slot')) {
428-
childNode.hidden = true;
429-
break;
430-
}
431-
} else {
432-
// this is a default fallback slot node
433-
// any element or text node (with content)
434-
// should hide the default fallback slot node
435-
if (nodeType === NODE_TYPE.ElementNode || (nodeType === NODE_TYPE.TextNode && childNodes[j].textContent.trim() !== '')) {
436-
childNode.hidden = true;
437-
break;
438-
}
421+
nodeType = childNodes[j].nodeType;
422+
423+
if (childNodes[j]['s-hn'] !== childNode['s-hn'] || slotNameAttr !== '') {
424+
// this sibling node is from a different component OR is a named fallback slot node
425+
if (nodeType === NODE_TYPE.ElementNode && slotNameAttr === childNodes[j].getAttribute('slot')) {
426+
childNode.hidden = true;
427+
break;
428+
}
429+
} else {
430+
// this is a default fallback slot node
431+
// any element or text node (with content)
432+
// should hide the default fallback slot node
433+
if (
434+
nodeType === NODE_TYPE.ElementNode ||
435+
(nodeType === NODE_TYPE.TextNode && childNodes[j].textContent.trim() !== '')
436+
) {
437+
childNode.hidden = true;
438+
break;
439439
}
440440
}
441441
}

0 commit comments

Comments
 (0)