Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 39 ยท Projects and Practice

Project: An Animated UI Kit with Sass

Build a small animation toolkit in Sass: generate keyframes from a map, stagger list items with @for, add spinners and skeleton loaders, and respect prefers-reduced-motion.

Animations are repetitive by nature: the same fade with four directions, ten list items that appear one after another, three loaders in three sizes. That makes them an ideal project for Sass loops and mixins. In this project you build a compact animation kit with entrance effects, a staggered list, a spinner, and a skeleton loader, and you make all of it respect users who ask for less motion.

You will use maps and @each, mixins with @content, and interpolation.

Requirements

  1. Entrance animations (.fade-up, .fade-down, .fade-left, .fade-right) generated from a map of start transforms.
  2. A .stagger container whose children appear one after another, with a configurable delay step.
  3. A .spinner and a .skeleton shimmer for loading states.
  4. Everything animated is inside a motion-safe wrapper that only applies when the user has not asked to reduce motion.
  5. Durations and delays are tokens, not magic numbers.

Step 1: tokens and the motion-safe mixin

SCSS
$duration: 500ms !default;
$easing: cubic-bezier(0.22, 1, 0.36, 1) !default;
$stagger-step: 80ms !default;
$stagger-max: 8 !default;

@mixin motion-safe {
  @media (prefers-reduced-motion: no-preference) { @content; }
}

Design the kit opt-in for motion: animations apply only when the media query says no-preference. Users who prefer reduced motion simply see the final state, so nothing is hidden or broken for them. (The reverse approach, turning animations off with reduce, risks forgetting a rule.)

Step 2: keyframes from a map

@keyframes can be generated inside a loop, with the name built by interpolation:

SCSS
$directions: (
  up:    translateY(1rem),
  down:  translateY(-1rem),
  left:  translateX(1rem),
  right: translateX(-1rem),
);

@include motion-safe {
  @each $name, $from in $directions {
    @keyframes fade-#{$name} {
      from { opacity: 0; transform: $from; }
      to   { opacity: 1; transform: none; }
    }
    .fade-#{$name} {
      animation: fade-#{$name} $duration $easing both;
    }
  }
}

The map values (translateY(1rem)) are plain CSS function calls; Sass passes unknown functions through untouched. both keeps the starting style before the animation starts and the end style after.

Step 3: staggered lists

Children of .stagger appear one by one. A @for loop sets a growing delay with nth-child:

SCSS
@include motion-safe {
  .stagger > * {
    animation: fade-up $duration $easing both;

    @for $i from 1 through $stagger-max {
      &:nth-child(#{$i}) { animation-delay: $i * $stagger-step; }
    }
  }
}

$i * $stagger-step multiplies a number by a time value and keeps the ms unit. Items beyond the maximum share the last delay, which avoids a 4-second wait on a long list. Another approach is to set an index custom property from the template (style="--i: 3") and compute the delay in CSS with calc(var(--i) * 80ms), which works for any length list without generating selectors.

Step 4: spinner and skeleton

SCSS
@include motion-safe {
  @keyframes spin { to { transform: rotate(360deg); } }
  @keyframes shimmer {
    from { background-position: 200% 0; }
    to   { background-position: -200% 0; }
  }
}

.spinner {
  width: 2rem;
  height: 2rem;
  border: 4px solid #e5e7eb;
  border-top-color: #4f46e5;
  border-radius: 50%;
  @include motion-safe { animation: spin 800ms linear infinite; }
}

.skeleton {
  height: 1rem;
  border-radius: 4px;
  background: linear-gradient(90deg, #e5e7eb 25%, #f3f4f6 50%, #e5e7eb 75%);
  background-size: 200% 100%;
  @include motion-safe { animation: shimmer 1.4s linear infinite; }
}

A static spinner ring is still a recognizable "loading" cue for reduced-motion users, and the skeleton stays as a gray bar. Pair spinners with role="status" and visually hidden text in your HTML so screen readers hear "Loading".

Try the kit

The animation kit

Edit the file and watch the animations replay. If you do not see motion, your operating system may be set to reduce motion, which is exactly what the kit is designed to respect. Read the compiled CSS: every rule sits inside @media (prefers-reduced-motion: no-preference).

Hover and focus transitions as mixins

Small interactions benefit from a shared mixin so durations stay consistent:

SCSS
@use "sass:list";

@mixin transition($props...) {
  $list: ();
  @each $p in $props {
    $list: list.append($list, $p $duration $easing, comma);
  }
  @include motion-safe { transition: $list; }
}

.card {
  @include transition(transform, box-shadow);
  &:hover { transform: translateY(-4px); box-shadow: 0 8px 20px rgb(0 0 0 / 0.15); }
}

The mixin takes a variable number of arguments ($props...), builds a comma-separated list, and wraps it in motion-safe. The mixin builds the list with a loop, but for two or three properties writing transition: transform $duration $easing, box-shadow $duration $easing is equally good. Reach for generators when repetition is real, not by default.

Extension ideas

  • Add scale and rotate entrance effects to the $directions map style by using a more general $effects map with nested from transforms.
  • Add a delay-1 ... delay-5 utility family generated with @for.
  • Provide an animation-play-state toggle class so scripts can pause long animations.
  • Add scroll-driven animations (animation-timeline: view()) as an enhancement behind @supports.
  • Write a @mixin pulse($color) that animates a box-shadow ring for attention, again inside motion-safe.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is @keyframes fade-#{$name} allowed in a loop?

    Interpolation builds the identifier; Sass emits one @keyframes block per iteration.
  2. 2. What does animation-delay: $i * $stagger-step produce when $i is 3 and the step is 90ms?

    Multiplying a number by a time value keeps the unit: 270ms.
  3. 3. Why wrap animations in prefers-reduced-motion: no-preference?

    Making motion opt-in respects accessibility settings by default.
  4. 4. What is the advantage of setting --i in markup and using calc(var(--i) * 80ms)?

    The delay is computed in the browser for each element, so no nth-child rules are needed.
  5. 5. What does $props... do in a mixin definition?

    A rest argument gathers extra arguments into a single list.

Summary

  • Generate repeated animation code from maps with @each, @for and interpolation; @keyframes names can be interpolated.
  • Wrap all motion in a motion-safe mixin so reduced-motion users get the final state.
  • Use tokens for duration, easing and stagger step; multiply numbers by time values to compute delays.
  • Rest arguments ($props...) and list building make small helper mixins flexible.
  • Generate only what the page uses; sometimes a custom property in CSS is simpler than a loop.

Next: Practice Set and Interview Questions