ionic-team / stencil

A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
https://stenciljs.com
Other
12.55k stars 784 forks source link

bug: broken rendering of slot elements on component rerender #5136

Open aniederer-chatham opened 10 months ago

aniederer-chatham commented 10 months ago

Prerequisites

Stencil Version

4.8.1

Current Behavior

After performing the repro steps below, the app displays:

Body: Header

Expected Behavior

After performing the repro steps below, the app displays:

Body: Body

System Info

System: node 20.5.0
    Platform: linux (5.15.133.1-microsoft-standard-WSL2)
   CPU Model: 12th Gen Intel(R) Core(TM) i7-12700H (20 cpus)
    Compiler: /home/adam/Programs/stencil-slot-bug-poc/node_modules/@stencil/core/compiler/stencil.js
       Build: 1701707120
     Stencil: 4.8.1 🍹
  TypeScript: 5.2.2
      Rollup: 2.42.3
      Parse5: 7.1.2
      Sizzle: 2.42.3
      Terser: 5.24.0

Steps to Reproduce

Create this component:

import { Component, ComponentInterface, Host, State, h } from '@stencil/core';

@Component({
  tag: 'stencil-slot-bug-poc',
  shadow: true,
})
export class StencilBugPoc implements ComponentInterface {
  @State() private hasHeader: boolean = true;
  render() {
    return (
      <Host>
        <button onClick={() => this.hasHeader = !this.hasHeader}>
          hasHeader: {''+this.hasHeader}
        </button>
        <div>
          {this.hasHeader && <div>Header: <slot name="header" /></div>}
          <div>Body: <slot /></div>
        </div>
      </Host>
    );
  }
}

Use it like so:

import { Component, Host, h } from '@stencil/core';

@Component({
  tag: 'app-root',
  shadow: true,
})
export class AppRoot {
  render() {
    return (
      <Host>
        <stencil-slot-bug-poc>
          <span slot="header">Header</span>
          Body
        </stencil-slot-bug-poc>
      </Host>
    );
  }
}

Build the app. It will display this:

image

Click the button. It will now display this:

image

Code Reproduction URL

https://github.com/aniederer-chatham/stencil-slot-bug-poc

Additional Information

aniederer-chatham commented 10 months ago

Appears to not be fixed by #5143

alicewriteswrongs commented 10 months ago

I think that makes sense based on what I understand about the child-reconciliation issues that #5143 aims to address, I wouldn't actually expect that it would fix this issue since it's not directly addressing slot relocation logic and whatnot