CSS animation not triggering
I have this animation and upon calling
open()
with another Svelte component, I can see the toggled-ring
class getting added in devtools but it does not cause the animation to play. When I staticly add this class in code, it does play the animation correctly. How can I toggle this animation with JS?29 Replies
Does it not even run the animation once?
Nope, here is a svelte REPL if that is of help https://svelte.dev/repl/28dc7a6abbb04f17ac8169402895967e?version=4.2.18
Hello world • REPL • Svelte
Cybernetically enhanced web apps
I don't know much about Svelte but I must say that, from looking at your code, I do like the look of it. It basically looks so similar to JavaScript it seems familiar. I should really give this a go!
It appears that, as the toggled-ring class isn't being included in the CSS. I presume that this is due to Svelte detecting that it isn't being used (in the HTML) so it saves space by not exporting it. I tried adding the "toggled-ring" to one of the circles and, on running the code, the class was now included and the animation ran on clicking the button.
I may be well off with this but that certiainly lools like what is happening. If that is the case I presume there must be a way to "force" Svelte to include the class 🤔
... a quick search has confirmed my suspicion - Svelte is not exporting unused classes
you can wrap the selector in
:global()
iirc
so :global{.toggled-ring)
not 100% ideal, but it should force svelte to include the class regardless of it not being used anywhereI literally just found that solution as you replied - thanks Jochem 👍
Thanks so much! I'll look in to using that
That was the issue! Thanks :D
keep in mind that it negates scoped styling, so it'll apply to any element with
toggle-ring
on it. Don't overuse it, especially if you're not the only one writing code for the projectYeah, that is a downside. It is just me writing here so it should not be an issue but if another solution exists then I'll certainly swap it out
I get the idea behind not compiling unused classes but there should be a better solution that having to make it global.
yeah, especially for a component framework. Toggling classes on and off is such a common thing
A possible better way to do this would be. like this:
That will include the class toggled-ring
I do like the look of Svelte! - this is the first time I have ever looked at it beyond a quick read.
Sveltes 🔥 mate you should give a college try
People think I'm just full of it but the DX is hard to beat and getting better in Svetle5 imo
I'm trying to find how they are changing :global() too cause it gets better in 5
They also changed on:event to actual event attributes
onclick=
which allows for better handling of events. (leveraging the platform 👏 )
oh so yea in Svelte5 how we can do global got cleaner which I'm all for ❤️ (there are cases you need this escape hatch so really nice that its a cleaner way to nest them vs writing a :global() for each selector)Ah! I forgot about that. I'll implement that once I figure out how to play the animation multiple times
With the version that I posted a few minutes ago you can do this to toggle the animation:
Except CSS doesn't like re-playing animations
(Unless I am missing something there)
that removes the class from the circles
Yeah, removing the class will not play it in reverse from what I have found
ah, you didn't mention that you want to play it in reverse, just to play it multiple times.
Good point, yeah. This is for a hamburger menu
I would honestly use Sveltes animations maybe at that point
til!
https://svelte.dev/examples/in-and-out
You can make them custom this is just the example
Ooh, that sounds like the clean way to do this...
https://joyofcode.xyz/animation-with-svelte#custom-transitions
Whats really 🔥 is you can use this method to tap into JS animation libraries too and use tick to progress a timeline lets say.
Wow! I'll have to dig in to this. Thanks for pointing me in the right direction <3
np you have options now!