/* Default animation settings */
:root {
--flourish-duration: 0.3s;
--flourish-easing: ease-in-out;
}
/* Base class for all flourish animations */
.flourish {
/* By default, we don't set any animation, but we can set common properties */
animation-duration: var(--flourish-duration);
animation-timing-function: var(--flourish-easing);
animation-fill-mode: both;
}
/* Example: fade in */
.flourish-fade-in {
animation-name: flourishFadeIn;
}
@keyframes flourishFadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* Example: slide up */
.flourish-slide-up {
animation-name: flourishSlideUp;
}
@keyframes flourishSlideUp {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* When animations are disabled, override */
.no-flourish-animations .flourish {
animation: none !important;
transition: none !important;
opacity: 1 !important;
transform: none !important;
}
/* Also, we might want to set initial states for elements that are about to be animated?
But note: if we are using the animation class, the element will start at the from state and then animate.
However, if animations are disabled, we want the element to be in the final state immediately.
The above rule with `opacity:1` and `transform:none` should do that.
But caution: if the element already has an inline style for opacity or transform, we might override it.
We can try to be more specific: only override if the element has a flourish class? But we are already targeting .flourish inside .no-flourish-animations.
Alternatively, we can have a class that we add to the element to indicate it's a flourish element, and then when animations are off, we set the final state.
But the above should work because we are using !important. However, that might be too heavy and override important user styles.
We can try without !important and rely on specificity? The .no-flourish-animations .flourish has higher specificity than just .flourish, so it should override.
But note: if the user has set an inline style, that has higher specificity. So we cannot override inline styles without !important.
We can decide: when animations are off, we want to force the final state. So we might need !important.
Alternatively, we can avoid using the .flourish class for the animation and instead use a data attribute? But that doesn't change specificity.
We can also have a separate class for the initial state? Actually, we don't set initial state in CSS for the flourish classes; we rely on the animation to start from the from state. So if we disable animation, the element will be at whatever state it was before. We want to set it to the final state.
So we need to set the final state. We can do:
.no-flourish-animations .flourish-fade-in {
opacity: 1;
}
.no-flourish-animations .flourish-slide-up {
opacity: 1;
transform: translateY(0);
}
But that would require listing every animation. We can try to use a general rule for common properties:
.no-flourish-animations .flourish {
opacity: 1 !important;
transform: none !important;
}
But then if we have an animation that only changes something else (like rotate), we would override it incorrectly? Actually, if we set `transform: none` that would override any transform from the animation, which is what we want because we want to cancel the animation and set the final state. But the final state of a rotation animation is not `none` but `rotate(0deg)`? Actually, if we animate from rotate(0) to rotate(360), the final state is rotate(360). But if we set `transform:none` that would be equivalent to `rotate(0)`? Not exactly: `transform:none` means no transformation, which is the same as identity matrix. So if we want the final state of the rotation to be the rotated state, then setting `none` would be wrong. But in that case, we might not want to cancel the rotation? Actually, if we are disabling animations, we want the element to be in the state it would be at the end of the animation. For a rotation that ends at 360deg, that is the same as 0deg? No, 360deg is the same as 0deg visually, but the computed value is different. However, for most purposes, it's the same.
But consider: if we have an animation that rotates from 0 to 360, and we set `transform:none`, that is 0deg, which is the starting point, not the ending point (which is also 0deg but with a full rotation). Actually, 360deg and 0deg are the same. So it's okay.
However, if we have an animation that rotates from 0 to 180, then the final state is 180deg. Setting `transform:none` would be 0deg, which is not the final state. So we would be overriding the intended final state.
Therefore, we cannot have a single rule for all transform changes. We need to know the final state of each animation.
So maybe we should not try to set the final state with a generic rule. Instead, we should design our animations so that the final state is the same as the element's default state? That is, the animation should start from an altered state and return to normal. For example, fadeIn starts at opacity:0 and goes to opacity:1. The final state (opacity:1) is the normal state. Similarly, slideUp starts at translateY(20px) and goes to translateY(0). The final state is the normal state (no transform). For a rotation that ends at 0deg, that is also the normal state. But if we have an animation that ends at a non-zero transform, then we would need to set that transform as the final state when animations are off.
So we should design our animations such that the final state is the element's natural state (no transform, opacity:1, etc.). Then we can safely set:
.no-flourish-animations .flourish {
opacity: 1 !important;
transform: none !important;
}
But what about animations that change other properties? We don't use those for performance.
So we restrict our library to only animate `opacity` and `transform`. Then the final state should be `opacity:1` and `transform:none`.
However, note that some animations might only change one of them. For example, a fade-in only changes opacity. Then setting `transform:none` is harmless because the element didn't have a transform anyway.
So we can do:
.no-flourish-animations .flourish {
opacity: 1 !important;
transform: none !important;
}
But what if the element already has a transform for layout purposes? We would be overriding it. That's a problem.
We can change our approach: instead of using a class that sets the animation, we use two classes: one for the initial state and one for the animation? Actually, we can have:
For an element that we want to fade in, we start with `flourish-fade-in-init` which sets `opacity:0`, and then we add `flourish-fade-in` which animates to `opacity:1`. Then when animations are off, we just remove the init class? But that would require JS to toggle classes.
Alternatively, we can use the `animation` property to set the animation, and when animations are off, we set the element to the final state by adding a class that sets the final state. But we don't want to have to add an extra class for every animation.
Let's think: we want to have a way to apply an animation by adding a single class. And when animations are disabled, that class should not animate but should set the element to the final state.
We can do:
.flourish-fade-in {
animation: flourishFadeIn var(--flourish-duration) var(--flourish-easing) forwards;
}
@keyframes flourishFadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
Then when animations are disabled, we want to set opacity:1. We can do:
.no-flourish-animations .flourish-fade-in {
animation: none;
opacity: 1;
}
But we would have to write that for every animation class. We can generate these rules with a preprocessor, but in plain CSS we have to write them.
Alternatively, we can use a common pattern: all our animations that we want to support have the same final state (opacity:1 and transform:none). Then we can write:
.no-flourish-animations .flourish {
opacity: 1 !important;
transform: none !important;
}
But as noted, this might override existing transforms.
So we need to be cautious. Maybe we should not set `transform:none` for all, but only for those that use transform? We can't know which ones do.
Another idea: use a data attribute to indicate that the element is a flourish and then in the no-flourish-animations state, we set the final state for that element based on the animation class? That would require a lot of CSS rules.
Given the complexity, perhaps we should not try to override the final state automatically. Instead, we can require that the developer also adds a class for the final state when animations are disabled? That's not user-friendly.
Alternatively, we can design our animations to use a technique where the final state is the same as the element's default, and we don't set any inline styles. Then when we disable animations, we just remove the animation and the element will be in its default state. But if we use `forwards` fill mode, the animation will set the final state via the animation. When we set `animation:none`, the element will revert to whatever style it had before the animation started. So if we want it to be in the final state, we need to set that state in the base style.
So we can do:
For an element that we want to fade in, we set:
<div class="flourish-fade-in">...</div>
And in CSS:
.flourish-fade-in {
opacity: 0; /* initial state */
animation: flourishFadeIn var(--flourish-duration) var(--flourish-easing) forwards;
}
Then when animations are off, we want the element to have `opacity:1`. We can override:
.no-flourish-animations .flourish-fade-in {
opacity: 1;
animation: none;
}
But note: we set `opacity:0` in the base class. So when we override with `opacity:1` in the reduced motion query, that works.
But what about the initial state? Without animation, we want the element to be visible (opacity:1) from the start. So we set that in the override.
This approach works for each animation class individually. We have to write an override for each animation class we provide.
We can generate these overrides with a script in the build process, but for a small library we can write them manually.
Let's list the animations we plan to support and write the overrides.
We'll support:
- fade-in: initial opacity:0, final opacity:1.
- fade-out: initial opacity:1, final opacity:0. (But note: if we are disabling animations, we might not want to fade out? Actually, if we are toggling off, we might want to immediately show or hide? This is tricky. We might want to disable animations for entering and leaving. So for fade-out, we might want to set opacity:0 immediately when reduced motion? But that would hide the element. That might be acceptable if the element is being removed. However, if we are conditionally rendering, we might not have the element at all. So maybe we don't need to worry about exit animations? We are focusing on entrance animations mostly.
But we should consider both. We can design our library to have separate classes for enter and exit.
However, to keep it simple, let's only do entrance animations for now.
So for each entrance animation, we set an initial state (like opacity:0 or transform: translateX(-100%)) and then animate to the final state (opacity:1, transform:none). Then when animations are disabled, we set the final state immediately.
We can do:
.flourish-fade-in {
opacity: 0;
animation: flourishFadeIn var(--flourish-duration) var(--flourish-easing) forwards;
}
.no-flourish-animations .flourish-fade-in {
opacity: 1;
animation: none;
}
Similarly for slide-in:
.flourish-slide-in-left {
transform: translateX(-100%);
animation: flourishSlideInLeft var(--flourish-duration) var(--flourish-easing) forwards;
}
.no-flourish-animations .flourish-slide-in-left {
transform: translateX(0);
animation: none;
}
But note: we are setting `transform: translateX(0)` which is the same as `none`. But if the element already has a transform, we would override it. That's a problem.
We can avoid setting `transform` in the base class and instead set it in the keyframe? But then the initial state is set by the keyframe, and when we disable animation, we don't have that initial state. We need to set the initial state in the base class so that when animations are off, we can override it.
Alternatively, we can use a wrapper element that we apply the transform to, so we don't interfere with the element's own transforms.
But that might be too heavy.
Given the complexity, we might decide that our library is for decorative animations on elements that don't have their own transforms. Or we can require that if the element has a transform, the developer should not use our transform-based animations on that element.
Alternatively, we can use `margin` or `left` for slide animations? Those trigger layout and are less performant. So we don't want that.
So we stick to `transform` and accept that it might conflict with existing transforms. We can document that.
Another idea: use `translate` in a way that doesn't conflict? We can use `translate3d` and set the element's existing transform as a variable? Not possible in CSS.
So we'll have to live with the limitation.
Alternatively, we can design our animations to use `transform` only if the element doesn't have one? Not possible in CSS.
So we'll just document that our transform-based animations will override any existing transform on the element.
Given that, we can proceed.
8. **JavaScript for dynamic animations**:
Sometimes we want to trigger an animation on an element that is already in the DOM, like on hover or on click. We can use CSS for hover, but again we need to respect reduced motion.
We can provide utility classes for hover, but we must be cautious because hover animations can be problematic for motor impairments and also for reduced motion.
We can do:
.flourish-hover-scale {
transition: transform var(--flourish-duration) var(--flourish-easing);
}
.flourish-hover-scale:hover {
transform: scale(1.1);
}
Then for reduced motion:
.no-flourish-animations .flourish-hover-scale {
transition: none;
}
.no-flourish-animations .flourish-hover-scale:hover {
transform: none;
}
But again, we are overriding transform.
So we might avoid hover animations that change transform? Or we can use `scale` which is a transform and then set `transform:none` in the reduced motion state? That would reset any existing transform.
We can try to use a wrapper for hover effects? But that's not always feasible.
Given the trade-offs, we might decide to not include hover animations that change transform, or we can use `box-shadow` or `color` changes for hover? Those are less performant but might be acceptable for small elements.
Alternatively, we can use `transform: translateZ(0)` to promote to GPU without changing the visual? That doesn't help.
We might have to accept that our library is not suitable for elements that already have transforms if we use transform-based animations.
We can provide a mixin for developers to create their own animations that don't conflict? That's beyond the scope.
So we'll proceed with the understanding that our library's transform-based animations will override any existing transform on the element.
9. **Conclusion**:
We'll create a library with:
- A CSS file with a set of animation classes (entrance only for now) that use `opacity` and `transform`.
- A JavaScript file that manages the global reduced motion state (system preference and user toggle) and adds/removes a class on the root element.
- For each animation class, we provide an override for when the global class is present.
We'll also provide a simple toggle button component that uses the JS API.
We'll write documentation about the limitations and best practices.
Let's code a minimal example.
We'll have:
flourish.css:
```css
:root {
--flourish-duration: 0.3s;
--flourish-easing: ease-in-out;
}
.flourish {
/* Common properties for all animations? Not necessary */
}
/* Fade In */
.flourish-fade-in {
opacity: 0;
animation: flourishFadeIn var(--flourish-duration) var(--flourish-easing) forwards;
}
@keyframes flourishFadeIn {
to { opacity: 1; }
}
.no-flourish-animations .flourish-fade-in {
opacity: 1;
animation: none;
}
/* Slide Up */
.flourish-slide-up {
transform: translateY(20px);
opacity: 0;
animation: flourishSlideUp var(--flourish-duration) var(--flourish-easing) forwards;
}
@keyframes flourishSlideUp {
to {
transform: translateY(0);
opacity: 1;
}
}
.no-flourish-animations .flourish-slide-up {
transform: translateY(0);
opacity: 1;
animation: none;
}
/* Scale In */
.flourish-scale-in {
transform: scale(0.9);
opacity: 0;
animation: flourishScaleIn var(--flourish-duration) var(--flourish-easing) forwards;
}
@keyframes flourishScaleIn {
to {
transform: scale(1);
opacity: 1;
}
}
.no-flourish-animations .flourish-scale-in {
transform: scale(1);
opacity: 1;
animation: none;
}