---
name: micro-interactions
description: Use when building buttons, toggles, checkboxes, inputs, sliders and other small controls that need tactile, polished feedback on the web.
---

# Micro-interactions

Small controls should answer every touch: hover, press, focus, change and result. Build them with CSS transitions or Framer Motion (`motion/react`).

## The four states every control needs

1. **Hover** (pointer devices only, use `@media (hover: hover)`): subtle background or lift, 120-160ms.
2. **Press**: scale to 0.96-0.97 immediately (about 90ms), spring back on release.
3. **Focus-visible**: a clear 2px outline with offset; never animate it away.
4. **Result**: confirm what happened (check mark, colour, count change).

## Patterns

### Button with loading and success

- Keep the button width stable or animate width with layout animation, never jump.
- Swap the label with a vertical roll: old text slides up 6-8px and fades, new text comes from below.
- Show a spinner inside the button, then morph it into a check mark (`pathLength` 0 to 1 over 220ms).
- Return to idle after about 1.5s.

### Toggle

- Thumb travels with a snappy spring (stiffness 500, damping 32).
- Track colour cross-fades over 160ms.
- Use `role="switch"` and `aria-checked`.

### Checkbox

- Box fills with a quick scale 0.85 to 1.05 to 1.
- Draw the tick with SVG `stroke-dashoffset` over 180-220ms.
- Optional: strike through the label from left to right (`scaleX` with `transform-origin: left`).

### Input

- Label floats up and shrinks on focus (transform, not font-size).
- Error: shake 3 times ±6px over 300ms, then show the message; also announce it with `aria-live`.

### Slider

- Thumb follows the pointer 1:1 while dragging; only spring when released or snapped.
- Show the value in a bubble that rolls its digits.

## Code sketch (Framer Motion)

```tsx
<motion.button
  whileHover={{ y: -1 }}
  whileTap={{ scale: 0.97 }}
  transition={{ type: "spring", stiffness: 500, damping: 32 }}
/>
```

## Done when

- Every state is visible with keyboard only.
- Spamming clicks never breaks the animation.
- Reduced motion keeps colour and label changes but drops movement.
