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

class and class:list conflict #998

Open
1 task
FrancoJavierGadea opened this issue Apr 7, 2024 · 4 comments
Open
1 task

class and class:list conflict #998

FrancoJavierGadea opened this issue Apr 7, 2024 · 4 comments
Labels
- P2: nice to have Not breaking anything but nice to have (priority)

Comments

@FrancoJavierGadea
Copy link

Astro Info

Astro                    v4.5.16
Node                     v20.11.0
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/react

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

No response

Describe the Bug

Attribute class with template string not work with class:list directive

---
const name = 'work'
const classString = `container ${name}`
---

<!-- Work -->
<div class={`container ${name}`} />

<!-- Not work -->
<div class={`container ${name}`} class:list={{'active': true}} />

<div class={classString} class:list={{'active': true}} />

<div class={"container"} class:list={{'active': true}} />

<!-- Very bad -->
<!-- <div class={'container'} class:list={{'active': true}} /> -->

render

<div class="container work"></div>

<div class="`container ${name}` active"></div>

<div class="classString active"></div>

<div class="&quot;container&quot; active"></div>

What's the expected result?

---
const name = 'work'
const classString = `container ${name}`
---

<div class={`container ${name}`} class:list={{'active': true}} />

render

<div class="container work active"  />

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-6vn7fi?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Apr 7, 2024
@ematipico ematipico added - P2: nice to have Not breaking anything but nice to have (priority) and removed needs triage Issue needs to be triaged labels Apr 8, 2024
@ematipico
Copy link
Member

I don't know if this is actually a bug or not, but you can pass name to class:list instead, and it will work

@bluwy
Copy link
Member

bluwy commented Apr 8, 2024

You can use https://live-astro-compiler.vercel.app to inspect how Astro compiles the code, and I think it revealed some issues here. It seems like the compiler is not correctly constructing the array passed to $addAttribute class:list.

@bluwy bluwy transferred this issue from withastro/astro Apr 8, 2024
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Apr 8, 2024
@FrancoJavierGadea
Copy link
Author

FrancoJavierGadea commented Apr 8, 2024

I try the compiler and yep

---
const name = 'work'
---

<!-- Work fine -->
<div class={`container ${work}`}></div>

<div class="container" class:list={}></div>

<div class='container' class:list={}></div>

<!-- Not work -->
<div class={`container ${work}`} class:list={}></div>

<div class={name} class:list={}></div>

<div class={"container " + name} class:list={}></div>

<div class={"container"} class:list={}></div>


<!-- Crash -->
<div class={'container'} class:list={}></div>

<div class={'container ' + name} class:list={}></div>

Output

//Work Fine
`
<div${$$addAttribute(`container ${work}`, "class")}></div>

<div${$$addAttribute(['container', ], "class:list")}></div>

<div${$$addAttribute(['container', ], "class:list")}></div>
`

//Not Work
`
<div${$$addAttribute(['`container ${work}`', ], "class:list")}></div>

<div${$$addAttribute(['name', ], "class:list")}></div>

<div${$$addAttribute(['"container " + name', ], "class:list")}></div>

<div${$$addAttribute(['"container"', ], "class:list")}></div>
`

//Crash 
`
<div${$$addAttribute([''container'', ], "class:list")}></div>

<div${$$addAttribute([''container ' + name', ], "class:list")}></div>
`

show

@MoustaphaDev
Copy link
Member

MoustaphaDev commented Apr 10, 2024

If someone wants work on this, I believe the issue is happening here
https://github.com/withastro/compiler/blob/main/internal/transform/transform.go#L618

The quotes should be set according to the class attribute's type, for example it should be template literals (`) for elements which class attribute is a template literal one (attr.Type == TemplateLiteralAttribute)

@florian-lefebvre florian-lefebvre removed the needs triage Issue needs to be triaged label May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: nice to have Not breaking anything but nice to have (priority)
Projects
None yet
Development

No branches or pull requests

5 participants