-
Notifications
You must be signed in to change notification settings - Fork 14
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
Dynamic CSS class names do not work #80
Comments
Hi @AleFossati Indeed, passing a dynamic variable as a class will not work because the preprocessor won't be able to interpreter and transform the data in something else before it runs. Since it could contain any value, there is no way to know the exact content and apply the rule from The only work around I can think of, is to always append a Could adding a specific syntax help? such as Any suggestion? |
@micantoine Any update on this issue? |
Ops, I'm sorry. I solved this problem using the first approach, which is not the best solution, but it works fine. Since then, I completely forgot about this issue, my bad. Tomorrow I will try this once more and come back here with my suggestion. |
@micantoine your proposal is to build something like this? <script lang="ts">
export let bgColor: 'white' | 'black';
</script>
<button class:dyn(bgColor) />
<style module>
.white {
background-color: #fff;
}
.black {
background-color: #000;
}
</style> If yes, I think it is a good solution. The only problem would be the syntax highlighting on IDE's 🤔 |
Oh no, I think he was suggesting In that case @micantoine is it simply avoiding hashing that particular class name? Otherwise (given how it processes in the markup stage) how can it know how to hash that dynamic variable? I’m wondering if maybe instead of Just my 2 cents! Not worth much since I only just started Svelte and am only investigating using this module. 😊 |
That's right, there is no way to know the content of a dynamic variable, so it becomes impossible to transform it completely when applying the rule of @AleFossati In the following example, <script lang="ts">
export let bgColor: 'white' | 'black';
</script>
<button class:dyn(bgColor) />
<style module>
.white {
background-color: #fff;
}
.black {
background-color: #000;
}
</style> So we need to make a connection between the dynamic variable name and the classnames listed in The content of the variable cannot and should not be changed
One thing that can be done with the current preprocessor is to use the provided <script lang="ts">
export let bgColor: 'white' | 'black';
</script>
<button class={bgColor} />
<style module>
:local(.white) {
background-color: #fff;
}
:local(.black) {
background-color: #000;
}
</style> generating <button class="white svelte-1ijxqhs" />
<style>
.white.svelte-1ijxqhs { background-color: #fff; }
.black.svelte-1ijxqhs { background-color: #000; }
</style> However, if the reason of using the preprocessor is to avoid inheriting styling from a class of the same name; we can elaborate a solution keeping in mind:
SuggestionUse of a special selector in
Example <script lang="ts">
export let bgColor: 'white' | 'black';
</script>
<button class={bgColor} />
<style module>
:dyn(bgColor.white) { background-color: #fff; }
:dyn(bgColor.black) { background-color: #000; }
</style> after preprocessing <script>
export let bgColor;
</script>
<button class={`${bgColor}-ha5h0sq`} />
<style>
.white-ha5h0sq { background-color: #fff; }
.black-ha5h0sq { background-color: #000; }
</style> generating <button class="white-ha5h0sq" />
<!-- or -->
<button class="black-ha5h0sq" />
<style>
.white-ha5h0sq { background-color: #fff; }
.black-ha5h0sq { background-color: #000; }
</style> |
I agree that it should not rely on typescript, that was a bad idea. |
+1 on the last example |
Is the issue I opened here related to this issue? Because although it did not produce any hash in Svelte 4, it started to produce hash in Svelte 5 when I used the class directive. |
@Enes5519 If I understand well your issue is that even though your class has been globalize ( This current issue has nothing to do with that and I believe it's not related to the preprocessor. To be 100% sure, remove the |
Thank you for your comment on the issue. I could only produce an example with the preprocessor. The link is here: sveltejs/svelte#15188 (comment) |
@Enes5519 it's definitely not coming from the preprocessor. On your example you can disable it and the issue will still persist. |
When using the directive
class={someVariableWithTheClassName}
, it fails because the generated css class will have a hash, but the applied class won't.Result:

Result:

The text was updated successfully, but these errors were encountered: