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(slot-fallback): fix hiding fallback slot content issue when the slotted element is a text node #5496

Merged
5 changes: 3 additions & 2 deletions src/runtime/vdom/vdom-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,9 @@ export const updateFallbackSlotVisibility = (elm: d.RenderNode) => {
// this sibling node is from a different component OR is a named
// fallback slot node
if (
siblingNode.nodeType === NODE_TYPE.ElementNode &&
(slotName === siblingNode.getAttribute('slot') || slotName === siblingNode['s-sn'])
(siblingNode.nodeType === NODE_TYPE.ElementNode &&
(slotName === siblingNode.getAttribute('slot') || slotName === siblingNode['s-sn'])) ||
(siblingNode.nodeType === NODE_TYPE.TextNode && slotName === siblingNode['s-sn'])
) {
childNode.hidden = true;
break;
Expand Down
16 changes: 16 additions & 0 deletions test/wdio/slot-fallback-with-textnode/cmp-avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Component, h } from '@stencil/core';

@Component({
tag: 'cmp-avatar',
shadow: false,
scoped: true,
})
export class CmpAvatar {
render() {
return (
<div class="container">
<slot>DEFAULT</slot>
</div>
);
}
}
19 changes: 19 additions & 0 deletions test/wdio/slot-fallback-with-textnode/cmp.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { h } from '@stencil/core';
import { render, waitForChanges } from '@wdio/browser-runner/stencil';
yigityuce marked this conversation as resolved.
Show resolved Hide resolved

describe('slot-fallback-with-textnode', function () {
beforeEach(() => {
render({
template: () => <my-component></my-component>,
});
});

it('should hide fallback content when provided slot is text node', async () => {
await expect($('.container')).toHaveText(expect.stringContaining('DEFAULT'))
yigityuce marked this conversation as resolved.
Show resolved Hide resolved
await $('#toggle-button').click();
yigityuce marked this conversation as resolved.
Show resolved Hide resolved
await waitForChanges();
yigityuce marked this conversation as resolved.
Show resolved Hide resolved

await expect($('.container')).not.toHaveText(expect.stringContaining('DEFAULT'));
yigityuce marked this conversation as resolved.
Show resolved Hide resolved
await expect($('.container')).toHaveText(expect.stringContaining('JD'));
yigityuce marked this conversation as resolved.
Show resolved Hide resolved
});
});
21 changes: 21 additions & 0 deletions test/wdio/slot-fallback-with-textnode/cmp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component, Fragment, h, State } from '@stencil/core';

@Component({
tag: 'my-component',
shadow: false,
scoped: true,
})
export class MyComponent {
@State() shortName: null | string;

render() {
return (
<Fragment>
<cmp-avatar>{this.shortName}</cmp-avatar>
<button id="toggle-button" onClick={() => (this.shortName = this.shortName ? null : 'JD')}>
Toggle ShortName
</button>
</Fragment>
);
}
}