-
Notifications
You must be signed in to change notification settings - Fork 13
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
Using dynamic classes #117
Comments
TLDR: at all I think you are using for dynamic classes on variable, the svelte way would be <div class:bg-red-800={toggle} class:bg-blue-800={toggle}>My background is togglable</div>
you need to import it, since
that should work, maybe use
just use <div class={toggle ? 'bg-blue-500' : 'bg-red-500'}>My background is togglable</div>
we would need |
Thank you @alexanderniebuhr, I agree that's too much use for I could not import I did more dinging in the code and I noticed that this does not work: <div class={toggle ? windi`bg-yellow-500` : windi`bg-blue-500`}>My background is togglable</div> because it seems that we are only taking the first match of svelte-windicss-preprocess/src/index.ts Line 136 in 2c7003c
and for this: <div class={toggle ? "red text-4xl" : "blue text-6xl"}>My background is togglable</div> it looks like we do format the code first, then we only match classes like This ignores This is the current regex:
|
I don't think this would be good way of writing this, so not sure if we make it work. I think to use <script>
let myDynamicToggleClasses = windi`bg-yellow-500`
</script>
<div class={toggle ? myDynamicToogleClassses}>My background is togglable</div>
yeah i will fix this right now.. seems that there are some bugs |
I agree it's not a good way to write the code like that but it was just to test since it's valid syntax. The issue is that only the first <script>
let toggle = true
let dynamicClassA = windi`bg-yellow-500`
let dynamicClassB = windi`bg-red-500`
$: dynamicClassFinal = toggle ? dynamicClassA : dynamicClassB
$: dynamicClass = toggle ? windi`bg-blue-500` : windi`bg-green-500`
// for completion this does not work either
// $: dynamicClass = windi`${toggle ? "bg-blue-500" : "bg-green-500"}`
let [dynamicClassC, dynamicClassD]= [windi`bg-pink-500`, windi`bg-orange-500`]
</script>
<button on:click={() => toggle = !toggle}>Toggle [{toggle}]</button>
<div class={toggle ? dynamicClassA : dynamicClassB}>This works fine</div>
<div class={dynamicClassFinal}>This works fine</div>
<div class={dynamicClass}>only the first class is taken</div>
<div class={toggle ? dynamicClassC : dynamicClassD}>only the first class is taken</div> Again I'm not saying either of those is good way to write, but it's valid syntax and someone else may see a similar issue so it's worth documenting. Thank you again @alexanderniebuhr for the quick response I would like to help more but I'm still new to OSS, and still trying to understand the codebase better. hopefully I'll be able to contribute more soon 😃 |
@tedmeftah do not try to understand the codebase now.. check branch |
@tedmeftah you said, you would like to contribute. if you have time, feel free to open PR onto the |
still tracking by v4.1 |
should be fixed with 7482f9a |
After finding out that using
windi
fromwindicss/helpers
did not work in dev mode (windicss/windicss#244). I noticed that in the code of this package there is a check for the for thewindi
template:svelte-windicss-preprocess/src/index.ts
Lines 133 to 136 in 2c7003c
so we don't actually need to import the
windi
helper we can just fake it like this:which make this work
of course this is not so useful, since the helper does update the classes in runtime, so all these examples below don't work:
I feel that the last example should work but it does not (
class={windi`${toggle ? 'bg-blue-500' : 'bg-red-500'}`}
).what's weird is when I do this:
it partially works, only taking the first
windi
expression (windibg-yellow-500
) but it toggle just fine.I found the best way to do dynamic styles is to use custom classes and
@apply
,I think we should advocate this way to use dynamic classes in the docs, since this avoids adding code to the runtime.
An improvement that can be added to this is allow the mixing of windicss classes, because this does not work right now:
it only render the
red
andblue
classes, and like before only renderwindi
red text-4xl` in the second example.The text was updated successfully, but these errors were encountered: