-sd-animation: sd-fadeIn; –sd-duration: 0ms; –sd-easing: ease-in;
This article explains the CSS-like custom properties shown in the title, how they work together to control animations, typical use cases, implementation patterns, and accessibility considerations.
What these properties mean
- -sd-animation: sd-fadeIn; — selects a named animation (here, a fade-in variant). This mirrors how CSS animation or animation-name works but uses a custom property pattern.
- –sd-duration: 0ms; — sets the animation duration to zero milliseconds, meaning the animated property will jump immediately to its final state (no visible transition).
- –sd-easing: ease-in; — specifies the timing function; ease-in makes changes start slowly and speed up. With a 0ms duration, easing has no visual effect.
Practical intent and common scenarios
- Immediate state changes: A 0ms duration is useful when you want the final state already applied but keep the same code path you use for animated transitions (for example, disabling animations in a reduced-motion mode or during initial rendering).
- Feature toggles: Toggle between animated and non-animated modes by changing the duration variable dynamically.
- Design systems: Use custom properties like these to centralize animation behavior across components while still enabling overrides.
Implementation pattern
- Define the named keyframes and default variables:
css
:root{–sd-duration: 250ms; –sd-easing: ease; -sd-animation: sd-fadeIn;}
@keyframes sd-fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); }}
- Apply them to components:
css
.card { animation-name: var(-sd-animation); animation-duration: var(–sd-duration); animation-timing-function: var(–sd-easing); animation-fill-mode: both;}
- Override for instant state (no visual animation):
css
.card.instant { –sd-duration: 0ms;}
JavaScript control examples
- Toggle animation off for users preferring reduced motion:
js
const el = document.querySelector(’.card’);if (window.matchMedia(’(prefers-reduced-motion: reduce)’).matches) { el.style.setProperty(’–sd-duration’, ‘0ms’);}
- Programmatically trigger animated entrance:
js
el.classList.remove(‘hidden’);el.style.removeProperty(’–sd-duration’); // use defaultel.getBoundingClientRect(); // force reflowel.classList.add(‘animate’); // starts animation
Accessibility and best practices
- Respect prefers-reduced-motion: set durations to 0ms when users request reduced motion.
- Avoid relying on zero-duration for focus visibility—ensure focus styles are immediate and clear.
- Use animation-fill-mode (both or forwards) to keep elements in their final state after animation.
- Test with screen readers and keyboard navigation to ensure animations don’t interfere with usability.
Troubleshooting
- No animation occurs: confirm the keyframe name matches the variable and that the element actually receives the CSS variable.
- Easing has no effect: with 0ms duration, easing is irrelevant.
- Unexpected jump on first render: apply 0ms during initial render and switch to a non-zero duration after mount to avoid flash.
Summary
These variables provide a flexible, centralized way to control named animations and let you quickly switch between instantaneous state changes and animated transitions. Setting –sd-duration to 0ms effectively disables animation while preserving the same property-based API, which is valuable for accessibility, feature toggles, and consistent design-system behavior.
Leave a Reply