compiler icon indicating copy to clipboard operation
compiler copied to clipboard

When combining child property spread w/ parent `<style>` tag, the child's styles disappear

Open crutchcorn opened this issue 3 years ago • 9 comments

What version of astro are you using?

1.6.10

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

Windows

Describe the Bug

When using a component that utilizes a spread operator to apply all styles:

---
const props = Astro.props as astroHTML.JSX.ButtonHTMLAttributes;
---

<style>
    .styled-button {
        all: unset;
        background: black;
        color: white;
    }
</style>
<button {...props} class="styled-button">Styled Button here</button>

And then apply a parent <style> tag:

---
import StyledButton from "./StyledButton.astro";
---

<div>
  <h1 class="h1">H1 here</h1>
  <StyledButton />
</div>
<style>
  .h1 {
    background: black;
    color: white;
    font-weight: bold;
  }
</style>

The styling of the child component (StyledButton) is disabled entirely. If you remove either the parent's style tag or the {...props} spread, this issue goes away.

Link to Minimal Reproducible Example

https://github.com/crutchcorn/astro-props-spread-style-bug

Participation

  • [X] I am willing to submit a pull request for this issue.

crutchcorn avatar Nov 21 '22 07:11 crutchcorn

I think the issue here is that we are passing the scoped style class to the child component. Not sure why the spread is being prioritized over the explicit class though.

matthewp avatar Nov 21 '22 13:11 matthewp

Ran into this as well (Astro v1.6.11). Another interesting aspect of this (and possible workaround): if the class attribute declaration comes before the props are spread, the component's styles are applied....

<button class="styled-button" {...props}>Styled Button here</button>

dev-nicolaos avatar Nov 25 '22 06:11 dev-nicolaos

mark

JerryWu1234 avatar Dec 05 '22 02:12 JerryWu1234

I think this bug was from the compiler. image the spreadAttributes function hasn't merged the class name

JerryWu1234 avatar Dec 05 '22 09:12 JerryWu1234

related to https://github.com/withastro/compiler/issues/656 There are two classes in the button dom; the default choice was the first. So the button dom style doesn't work

JerryWu1234 avatar Dec 06 '22 01:12 JerryWu1234

Thanks for investigating this @wulinsheng123. Transferring this over to the compiler repo so it can be tracked there.

bluwy avatar Dec 06 '22 08:12 bluwy

Finally circling back to this. I think we should probably pass all props as an object when there's a spread prop, to ensure that duplicate attributes are not generated.

natemoo-re avatar Aug 09 '23 21:08 natemoo-re

Do you still plan to work on this @bluwy ?

MoustaphaDev avatar Dec 20 '23 09:12 MoustaphaDev

Not for the foreseeable future yet. I actually didn't know I was assigned to this either 😅 I'll remove my assignment for now so feel free to take this if you like. But it's something I can also revisit in the future.

bluwy avatar Dec 20 '23 13:12 bluwy