Replies: 3 comments 3 replies
-
Using it this way is a good suggestion, as described in @lea Verou‘s 《The eg. <div class="element">
<span>Hover Me (^_^)</span>
</div> :root {
--ON: initial;
--OFF: ;
}
.element {
--rotate-switch: var(--OFF);
--rotate-deg: 45deg;
min-width: 50vmin;
aspect-ratio: 1 / 1;
background-color: #f36;
display: flex;
justify-content: center;
align-items: center;
font-size: 2rem;
color: #fff;
padding: 2rem;
gap: 2rem;
transition: all 0.2s linear;
cursor: pointer;
transform: rotate(var(--rotate-switch, var(--rotate-deg)));
}
.element span {
transition: all 0.2s linear;
transform: rotate(var(--rotate-switch, -45deg));
}
.element:hover {
--rotate-switch: var(--ON);
} CodePen: https://codepen.io/airen/pen/oNYyVag @lea Verou's other a blog post 《Custom properties with defaults: 3+1 strategies》 that mentions suggestions that are also worth considering |
Beta Was this translation helpful? Give feedback.
-
super fun idea! I forked idea 2 and played around, ended up with this before I decided to go to bed: .foo {
background: var(--underline-animation-1)
}
.foo:hover {
background-size: var(--underline-animation-in)
} debatable if it's any better, probably not but regarding strategy 1, those vars can ship under @keyframes circle-swoop {
from {
clip-path: var(--circle-top-right-out);
}
to {
clip-path: var(--circle-bottom-right-in);
}
} Calling them i havent made any decision yet 🙁 , still soaking it all in. but it easy for me to slip into making mixins with properties, adding options and params, but i feel its a different beast, like a whole lib should be like open props but it's like open-mixins. lean heavy into props as params. |
Beta Was this translation helpful? Give feedback.
-
I agree with the fact of keeping the variables inside :root and the the in/out trick. I have reworked my initial idea to come with the following: :root {
--underline-color:currentColor;
--size-thin:0.1em;
--size-thick:0.2em;
--underline-left-thin-in: linear-gradient(var(--underline-color) 0 0) 0 100%/0 var(--size-thin) no-repeat;
--underline-left-thin-out: linear-gradient(var(--underline-color) 0 0) 0 100%/100% var(--size-thin) no-repeat;
--underline-left-thick-in: linear-gradient(var(--underline-color) 0 0) 0 100%/0 var(--size-thick) no-repeat;
--underline-left-thick-out: linear-gradient(var(--underline-color) 0 0) 0 100%/100% var(--size-thick) no-repeat;
--underline-right-thin-in: linear-gradient(var(--underline-color) 0 0) 100% 100%/0 var(--size-thin) no-repeat;
--underline-right-thin-out: linear-gradient(var(--underline-color) 0 0) 100% 100%/100% var(--size-thin) no-repeat;
--underline-right-thick-in: linear-gradient(var(--underline-color) 0 0) 100% 100%/0 var(--size-thick) no-repeat;
--underline-right-thick-out: linear-gradient(var(--underline-color) 0 0) 100% 100%/100% var(--size-thick) no-repeat;
--underline-center-thin-in: linear-gradient(var(--underline-color) 0 0) 50% 100%/0 var(--size-thin) no-repeat;
--underline-center-thin-out: linear-gradient(var(--underline-color) 0 0) 50% 100%/100% var(--size-thin) no-repeat;
--underline-center-thick-in: linear-gradient(var(--underline-color) 0 0) 50% 100%/0 var(--size-thick) no-repeat;
--underline-center-thick-out: linear-gradient(var(--underline-color) 0 0) 50% 100%/100% var(--size-thick) no-repeat;
--underline-double-1-thin-in:
linear-gradient(var(--underline-color) 0 0) 0 100%/0 var(--size-thin) no-repeat,
linear-gradient(var(--underline-color) 0 0) 100% 100%/0 var(--size-thin) no-repeat;
--underline-double-1-thin-out:
linear-gradient(var(--underline-color) 0 0) 0 100%/51% var(--size-thin) no-repeat,
linear-gradient(var(--underline-color) 0 0) 100% 100%/51% var(--size-thin) no-repeat;
--underline-double-1-thick-in:
linear-gradient(var(--underline-color) 0 0) 0 100%/0 var(--size-thick) no-repeat,
linear-gradient(var(--underline-color) 0 0) 100% 100%/0 var(--size-thick) no-repeat;
--underline-double-1-thick-out:
linear-gradient(var(--underline-color) 0 0) 0 100%/51% var(--size-thick) no-repeat,
linear-gradient(var(--underline-color) 0 0) 100% 100%/51% var(--size-thick) no-repeat;
--underline-double-2-thin-in:
linear-gradient(var(--underline-color) 0 0) 0 100% /0 var(--size-thin) no-repeat,
linear-gradient(90deg, #0000 50%, var(--underline-color) 0) bottom /0 var(--size-thin) no-repeat;
--underline-double-2-thin-out:
linear-gradient(var(--underline-color) 0 0) 0 100% /51% var(--size-thin) no-repeat,
linear-gradient(90deg, #0000 50%, var(--underline-color) 0) bottom /100% var(--size-thin) no-repeat;
--underline-double-2-thick-in:
linear-gradient(var(--underline-color) 0 0) 0 100% /0 var(--size-thick) no-repeat,
linear-gradient(90deg, #0000 50%, var(--underline-color) 0) bottom /0 var(--size-thick) no-repeat;
--underline-double-2-thick-out:
linear-gradient(var(--underline-color) 0 0) 0 100% /51% var(--size-thick) no-repeat,
linear-gradient(90deg, #0000 50%, var(--underline-color) 0) bottom /100% var(--size-thick) no-repeat;
--underline-double-3-thin-in:
linear-gradient(var(--underline-color) 0 0) 100% 100% /0 var(--size-thin) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) bottom /0 var(--size-thin) no-repeat;
--underline-double-3-thin-out:
linear-gradient(var(--underline-color) 0 0) 100% 100% /51% var(--size-thin) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) bottom /100% var(--size-thin) no-repeat;
--underline-double-3-thick-in:
linear-gradient(var(--underline-color) 0 0) 100% 100% /0 var(--size-thick) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) bottom /0 var(--size-thick) no-repeat;
--underline-double-3-thick-out:
linear-gradient(var(--underline-color) 0 0) 100% 100% /51% var(--size-thick) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) bottom /100% var(--size-thick) no-repeat;
--underline-double-4-thin-in:
linear-gradient( 90deg, #0000 50%, var(--underline-color) 0) 75% 100% /0 var(--size-thin) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) 25% 100% /0 var(--size-thin) no-repeat;
--underline-double-4-thin-out:
linear-gradient( 90deg, #0000 50%, var(--underline-color) 0) 75% 100% /100% var(--size-thin) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) 25% 100% /100% var(--size-thin) no-repeat;
--underline-double-4-thick-in:
linear-gradient( 90deg, #0000 50%, var(--underline-color) 0) 75% 100% /0 var(--size-thick) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) 25% 100% /0 var(--size-thick) no-repeat;
--underline-double-4-thick-out:
linear-gradient( 90deg, #0000 50%, var(--underline-color) 0) 75% 100% /100% var(--size-thick) no-repeat,
linear-gradient(-90deg, #0000 50%, var(--underline-color) 0) 25% 100% /100% var(--size-thick) no-repeat;
} 7 different hover effect, each one with two variations (a thin and a thick) and we have 3 global and shared variables that we can easily adjust (the color and the size of each variation). A bit verbose, yes, but I think the usage is pretty simple and intuitive. Demo: https://codepen.io/t_afif/pen/VwMKXEg I still have more animations but I think we should start with only a few of them until we get the perfect organization and syntax before adding more. |
Beta Was this translation helpful? Give feedback.
-
Thinking about the different hover animation I think it would suitable to have a "hovers.css" bundle with some particular rules (instead of adding this to "Utilities.css").
I would imagine 3 cases.
1) Separate the non-hover and hover state:
In this case we can have the following:
Each state will have its own variable. It's easy to define but not a good for sizing because we will for sure have a lot of duplication between both states. They will be almost the same with one or two value of difference.
I am not really OK for this one
2) one variable for the hover state
In this case you will have the following:
Here we express the effect using only one variable (less code) BUT we need to include the --is-hover as a switch variable using some calc() and other stuff (it may be tricky to code in some cases). Later we simply update the --is-hover. The latter can also be shared between all the variables.
I like more this idea and this is what I started to do: https://codepen.io/t_afif/pen/qBPbbWb
3) one variable and implicit hover state.
This case is an optimization of the (2) by having:
The hover state is defined inside the library so we don't have to write too much CSS code (only one line is needed). This can avoid having to deal with the --is-hover variable that remain internal. The drawback here is that we force the hover effect so it may have some sides effect (I don't see any actually)
Let me know which one you consider as the best or maybe you have another alternative. I have different ideas of animation using gradient/mask and I can express them using one of the above.
Beta Was this translation helpful? Give feedback.
All reactions