Skip to content

Commit

Permalink
fix(slot-fallback): fix hiding fallback slot content issue when the s…
Browse files Browse the repository at this point in the history
…lotted element is a text node (#5496)

* fix(slot-fallback): fix hiding fallback slot content issue when the slotted element is a text node

* test: fix test related reviews

* test: fix test related reviews

* Update test/wdio/slot-fallback-with-textnode/cmp.test.tsx

---------

Co-authored-by: Christian Bromann <git@bromann.dev>
  • Loading branch information
yigityuce and christian-bromann committed Apr 22, 2024
1 parent 20f74fc commit 29c69c4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
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>
);
}
}
18 changes: 18 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,18 @@
import { h } from '@stencil/core';
import { render } from '@wdio/browser-runner/stencil';

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('DEFAULT', { trim: true });
await $('#toggle-button').click();

await expect($('.container')).not.toHaveText('DEFAULT', { trim: true });
await expect($('.container')).toHaveText('JD', { trim: true });
});
});
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>
);
}
}

0 comments on commit 29c69c4

Please sign in to comment.