I need to apply the following entering and leaving animation
They are presented using tailwindcss classes
Entering: "transform ease-out duration-300 transition"
From: "translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2"
To: "translate-y-0 opacity-100 sm:translate-x-0"
Leaving: "transition ease-in duration-100"
From: "opacity-100"
To: "opacity-0"
I tried to use the following but doesn’t seem to work.
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.alt-pop {
animation: alt-pop-enter, alt-pop-leave;
}
@keyframes alt-pop-enter {
@apply transform ease-out duration-300 transition;
from { @apply translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2; }
to { @apply translate-y-0 opacity-100 sm:translate-x-0; }
}
@keyframes alt-pop-leave {
@apply transition ease-in duration-100;
from { @apply opacity-100; }
to { @apply opacity-0; }
}
}
My suspicions are that:
- keyframes doesn’t work with
@apply
- keyframes work exclusively with from and to, so cannot add extra css styles within the keyframe
How do I use css as much as possible to setup this entering and leaving animation instead of JS?
And how do I use tailwindcss classes as much as possible?
And how do I set the delay before switching from entering to leaving?
One approach I can think of is convert all the tailwind classes back to the actual tailwind then write my own keyframes and animation css class.
Is there a better approach than this?
3
Answers
There are some invalid syntax/conceptions with the provided code:
This applies both of these named animations at the same time – probably not you’d want. Also note that no duration has been provided so the duration for both these animations is
0s
and thus there would be no visual effect.You can’t use
@apply
as immediate "child" of@keyframes
, since this would conceptually output something like:And this is invalid CSS.
It feels like you are trying to manipulate the
animation
, but you are using classes that settransition-*
properties. These properties do not affectanimation
.There will always be some aspect of JavaScript needed, since the CSS needs to be applied on the event when the animation needs to play.
Use your existing solution with the enter/leave classes.
This would be best done in the JavaScript that controls the enter/leaving classes. Otherwise, you could add a
delay-*
class to theleaving
set of classes.Yes, your existing enter/leave classes.
Edit 1: Reply to @Kim Stacks
It might seem working, but if you see keenly, your asked requirement is not met. Further I would refer your own comment to prove my point.
Adding on you can’t use variants in
@keyframes
since it would produce invalid CSS. In this instance, you’d create a separate@keyframes
for thesm:
case and then apply that to the.alt-pop
class. Something like:Edit 2: Reply to @Rifky Niyas
Your @keyframes compile to:
Where the @media rules inside the @keyframes is invalid CSS
@apply
is a feature oftailwindcss
that allows you to extract a set of CSS declarations as a class and apply it to an element. You can use it to simplify writing and maintain CSS styles.Keyframes
, on the other hand, define the intermediate steps in an animation. They are not applied directly to an element. You can reference them in an animation definition.So, No, it is not possible to use
@apply
intailwindcss
for keyframes.You can though achieve what you want like below:
The recommended approach
The approach recommended in the documentation inorder to define custom animations you would require to define
@keyframes
andanimation
properties intailwind.config.js
file and apply via theanimate-my-animation-name
class.The above approach requires you to mostly use the conventional CSSinstead of tailwind classes. But coming back to your original requirements,
Yes, you can achieve this by mostly using tailwind classes using the following approach.
Using
@apply
in@keyframes
First let’s try solving your suspicions
According to the docs for the
@apply
directive,When we are defining a
@keyframes
rule, the CSS styling is applied withinfrom
andto
blocks in order to define styles for the waypoints within the animation. So your animation style definition would look like this.Error in applying animation
The following lines in your code is invalid when applying CSS animations.
You seem to have tried to use
transition
property inside@keyframes
together. In CSSanimation
andtransition
are two different ways to animate an object. You can read more here. As mentioned in MDN docs aboveAlso note that the property
transform
class isn’t required in tailwind Version 3 since the transform values such asrotate
,scale
, etc are applied via individual styles such asrotate-1
andscale-50
instead of adjusting tailwind variables as in version 2Fixing the animation
Now since we are having two separate animations to be defined, we need to use a custom CSS property to extract a separate class. We can make use of animation shorthand property. For our purpose, the order of
animation
property values would look like thisHere is how it would look in CSS code
Notice we add an extra
500ms
value to animation for it to delay by 500ms after the animation has started. This includes 300ms for the initial animation to occur and then another 00ms pause before the second animation inititaes. Then you can apply the class to HTML element and use as requiredHere is a link to the solved animation: https://play.tailwindcss.com/uLgEAAd5Of?file=css