Microinteractions & Animation — UI/UX

A microinteraction is one small moment around one task. Saffer (2013) splits it into Trigger (what starts it), Rules (what may happen), Feedback (how people…

You tap the like button in a social media app, and the heart swells for a moment, then turns red. You pull a chat screen down, and a little circle spins before new messages come in. These movements are tiny, but without them you'd wonder whether your tap was received.

Small moments such as the growing heart and the spinning circle are called microinteractions: one small moment around one task. Other examples: a button that shrinks slightly as you press it, a button label that changes to "Booking...", or a toast (a small message that appears, then leaves on its own). Animation is how that moment moves. A microinteraction works like the light on a lift button. You press the button, and its light comes on right away: you know your press was received. The lift arrives, and the light goes out as the doors open. If the light didn't come on, you would press the button five times. That light is the lift button's microinteraction.

The four parts of a microinteraction

Dan Saffer, in his book Microinteractions: Designing with Details (O'Reilly, 2013), splits every microinteraction into four parts: Trigger, Rules, Feedback, Loops & Modes. Here they are on the "Book the room, pay on arrival" button:

Part What it means On the Book button
Trigger what starts it: a person's action (manual trigger) or a system state (system trigger) Mr. Hadi taps the button; the server's answer starts the toast
Rules what may happen and in what order the button moves between idle (at rest), hover (the cursor is over it) and pressed (being pressed); while booking, a second tap is ignored
Feedback how people know what's happening: visuals, sound, vibration the button shrinks 2% when pressed, the label "Booking...", the toast "Booking sent"
Loops & Modes loop: what happens over time; mode: a special condition that changes the rules for a while the toast leaves by itself after 5 seconds; the "booking" mode in which the button takes no taps

In order: the trigger starts it, the rules decide, the feedback tells people, and the loops & modes control what comes after.

Rules as a state machine

The Book button's rules are clearest written as a state machine: a list of states (conditions), and which event moves the button from which state to which. An event is something the browser reports. Here the events are pointer events, such as the pointer entering the button. The pointer can be a mouse cursor, a finger or a pen, and the events are the same for all three.

The JavaScript code below keeps those rules in a table, TRANSITIONS:

const TRANSITIONS = {
  idle:    { pointerenter: 'hover' },                        // the pointer enters the button
  hover:   { pointerdown: 'pressed', pointerleave: 'idle' }, // pressed, or the pointer leaves
  pressed: { pointerup: 'hover', pointerleave: 'idle' },     // released, or dragged away
};

This is the trace of one full tap, starting from idle:

Event From To
pointerenter idle hover
pointerdown hover pressed
pointerup pressed hover
pointerleave hover idle

Each state has its own look in CSS, through a data-state attribute that the code writes on the button: in hover the button is darker (#166534), and in pressed it shrinks to scale(0.98) (98% of its size). In the exercise you'll write nextState(current, event) and runSequence(events) from this table.

Duration and easing in numbers

Page Laubheimer of Nielsen Norman Group (NN/g, a UX research company) wrote about this in "Executing UX Animations: Duration and Motion Characteristics" (2020):

For hovering and pressing a button, use 100 to 200 ms. The middle is 150 ms. The CSS below puts those numbers on Mrs. Sri's page:

.nights button { transition: transform 100ms ease-out, background-color 150ms ease-out; }
.nights button:active { transform: scale(0.96); }

.book { transition: transform 100ms ease-out, background-color 150ms ease-out; }
.book[data-state="hover"]   { background: #166534; }
.book[data-state="pressed"] { transform: scale(0.98); }

.toast { transform: translateY(120%); opacity: 0;
         transition: transform 150ms ease-in, opacity 150ms ease-in; }    /* leaving */
.toast.show { transform: none; opacity: 1;
              transition-duration: 200ms; transition-timing-function: ease-out; } /* entering */

Easing is an animation's speed curve: when it moves fast, and when it slows. We ran a 1-second, 100px animation with the Web Animations API (a way to run animations from JavaScript), then read its position in Chromium 147, WebKit 26.4 and Firefox 146. The results were the same in all three:

Easing 25% of the time 50% of the time 75% of the time
linear 25px 50px 75px
ease-out 37.8px 68.5px 90.7px
ease-in 9.3px 31.5px 62.2px
ease-in-out 12.9px 50px 87.1px
cubic-bezier(0.34, 1.56, 0.64, 1) 81.6px 108.7px 106px

Animate transform and opacity only

A browser draws a page in three steps, in this order:

  1. layout: calculating positions and sizes,
  2. paint: colouring,
  3. composite: stacking the layers.

transform and opacity only need the composite step. top, bottom (the distance from the top or bottom edge), width and margin change a position or a size. When one of them is animated, the browser has to run layout again on every frame (each time the screen is drawn).

We animated the same toast for 200 ms in Chromium 147, then counted how often layout ran again, and the layout shifts (jumps in the content):

Toast rises through Layout run again Layout shifts
transform: translateY(...) 0 to 1 times 0
bottom: -64px to 16px 23 to 24 times 13 entries

On screen, both look the same. But the bottom version calculates the whole page's positions again on almost every frame. On a cheap phone, that recalculation is what makes an animation stutter. Changing background-color on the night buttons triggers paint, not layout (0 times in our test), so it's still light.

prefers-reduced-motion: motion reduced, not thrown away

Some people get dizzy or nauseous from motion on screen, because of a vestibular disorder (a problem with the sense of balance). They turn on a system setting: "Reduce motion" on iPhone and Mac, "Remove animations" on Android, or "Animation effects" turned off on Windows. The browser passes that setting to the page as the media query (a block of CSS that applies only in certain conditions) prefers-reduced-motion: reduce. WCAG 2.3.3 (Animation from Interactions, level AAA) asks that animation triggered by interaction can be turned off.

Before you see the result, guess. Mr. Hadi (58) turns on "Reduce motion" on his phone. Mrs. Sri's team respects that setting with transition: none on every element. The "Booking sent" toast leaves after 5 seconds. transitionend is the event the browser sends when a transition finishes, and when that event arrives, Mrs. Sri's code hides the toast (toast.hidden = true). The toast is now out of sight. Is it also gone from the accessibility tree, which screen readers read?

We turned on reduce in Chromium, WebKit and Firefox, then moved an element with a 200 ms transition:

Rule inside @media (prefers-reduced-motion: reduce) transitionend
transition: none 0 times
transition-duration: 0s 0 times
transition-duration: 0.01ms once, after 1 to 16 ms

So the answer to the guess: not yet. With transition: none, the toast jumps to opacity: 0 and to its hidden position. But transitionend never arrives, so toast.hidden = true never runs. In Chromium 147, 7 seconds after booking, the text "Booking sent. Mrs. Sri will confirm on WhatsApp." was still in the accessibility tree. A screen reader user going through the page still hears that old news as if it were new. With the fade rule below, it was gone.

Better: drop the movement, keep a short fade.

@media (prefers-reduced-motion: reduce) {
  .toast, .toast.show {
    transform: none;                      /* no sliding */
    transition: opacity 150ms linear;     /* only fades, transitionend still arrives */
  }
  .nights button:active, .book[data-state="pressed"] { transform: none; }
}

⚠️ Motion that gets in the way

TL;DR: A microinteraction is one small moment around one task. Saffer (2013) splits it into Trigger (what starts it), Rules (what may happen), Feedback (how people know), and Loops & Modes (what comes after, and special conditions that change the rules). A button's rules are written as a state machine, a list of states (conditions) and the events (things the browser reports) that move between them: idle (at rest) on pointerenter (the pointer enters) becomes hover, pointerdown (pressed) makes it pressed, pointerup (released) goes back to hover, pointerleave (the pointer leaves) to idle. Any other event moves nothing. Duration: small feedback 100 to 200 ms (about 150), a modal 200 to 300 ms, and at 500 ms an animation starts to feel too slow (NN/g 2020). Easing (the speed curve): ease-out for what enters, ease-in for what leaves, linear for what spins. Animate transform (moving and resizing) and opacity. In Chromium, a toast (a small message) moved with bottom runs layout 23 to 24 times in 200 ms, with transform 0 to 1 times. Respect prefers-reduced-motion (the "reduce motion" setting) with a short fade, not transition: none, which means transitionend (the event when a transition finishes) never arrives. Things repeated often: short, or not animated.

⚡ The four parts of a microinteraction

Meaning: A microinteraction is one small moment around one task. Dan Saffer splits it into four parts. Trigger: what starts it, for example a guest tapping the Book button. Rules: what may happen. Feedback: how people know, for example the button label changing to "Booking...". Loops & Modes: what comes after, for example the toast (a small message) leaving by itself after 5 seconds.

⚡ Rules as a state machine

Write:

const TRANSITIONS = {
  idle:    { pointerenter: 'hover' },                        // the pointer enters the button
  hover:   { pointerdown: 'pressed', pointerleave: 'idle' }, // pressed, or the pointer leaves
  pressed: { pointerup: 'hover', pointerleave: 'idle' },     // released, or dragged away
};
Guess first: this table moves the button from one state (condition) to another when an event (something the browser reports) arrives. The button is idle (at rest), then a click event arrives. What state is it in now?

Still idle. An event missing from that state's line moves nothing.

Meaning: Each line is one state, holding the events it knows and the state each one leads to. pointerup (the button is released) moves pressed (being pressed) back to hover, not to idle, because the pointer (the cursor or a finger) is still over the button.

⚡ Short, and only transform or opacity

Guess first: how many ms (milliseconds) suit a hover or press animation on a button?

About 150 ms, the middle of 100 to 200 ms. At 500 ms, an animation starts to feel too slow.

Meaning: Easing is an animation's speed curve: ease-out (fast at the start) for an element that enters, ease-in (slow at the start) for one that leaves. Animate only transform (movement and size) and opacity (see-through), because top or bottom (the position from the top or bottom) make the browser calculate the layout (positions and sizes) again on almost every frame.

⚡ Motion reduced, not thrown away

Write:

@media (prefers-reduced-motion: reduce) {
  .toast, .toast.show {
    transform: none;                      /* no sliding */
    transition: opacity 150ms linear;     /* only fades, transitionend still arrives */
  }
  .nights button:active, .book[data-state="pressed"] { transform: none; }
}

Meaning: This block applies when the user turns on a "reduce motion" setting. The toast no longer slides, but it still fades. With transition: none, the transitionend event (when a transition finishes) never arrives, so the code that hides the toast never runs, and a screen reader still reads it. A short fade is still safe for people who get dizzy from motion.

What you will learn