withastro / compiler

The Astro compiler. Written in Go. Distributed as WASM.
Other
502 stars 59 forks source link

Rendering issue: variable in `th` or `caption` inside a Table component wrapped with another element #1015

Open ArmandPhilippot opened 4 months ago

ArmandPhilippot commented 4 months ago

Astro Info

Astro                    v4.11.5
Node                     v20.12.0
System                   Linux (x64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/node
Integrations             astro-icon
                         dev-only-pages
                         @astrojs/mdx
                         pagefind
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

Context

Issue

The <tbody> of a <Table> component is not correctly rendered when the component is wrapped with another element and that a variable is present in a <th> (i.e. <thead><th>{myVariable}</th></thead>) or in a <caption> (i.e. <caption>{myVariable}</caption>).

For example:

---
import Table from "./base/table.astro";

const headingContent = "foo";
const cellContent = "bar";
---

<figure>
  <Table>
    <thead>
      <tr>
        <th>{headingContent}</th>
        <th>Cell 2</th>
        <th>Cell 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>{cellContent}</td>
        <td>Data 2</td>
        <td>Data 3</td>
      </tr>
    </tbody>
  </Table>
</figure>

Is rendered as below:

Astro table: incorrect rendering example

Instead of:

Astro table: correct rendering example

The rendering is correct only when:

Yet the <Table> component is very simple, for example:

---
import type { HTMLAttributes } from 'astro/types';

type Props = HTMLAttributes<'table'>;

const {...attrs} = Astro.props;
---

<table {...attrs}><slot /></table>

<style>
  :global(table :where(th, td)) {
    border: 1px solid #000;
  }
</style>

Workaround

Reproduction

I put a Stackblitz link with a minimal reproduction.

In table-component-wrapped-in-figure.astro:

Additional notes

I don't think the <figure> element is the problem here. While I was writing this issue, I tried to wrap the <Table> component with a custom element instead of a <figure>, and the same rendering issue occurs.

What's the expected result?

I expect the <tbody> to be correctly rendered when using a variable in a <th> inside <Table> component wrapped in a another element (<figure>, custom element, etc.).

Link to Minimal Reproducible Example

https://stackblitz.com/edit/astro-table-th-figure?file=src%2Fpages%2Findex.astro

Participation