---
name: cottoncandy
description: Rules for using, composing, restyling or building cottoncandy components (files under components/mu/) in a shadcn app. Use before editing anything in components/mu/, adding a cottoncandy item to a page, or making a new item in the same style.
---

# cottoncandy rules

cottoncandy items are motion-led React components installed with the shadcn CLI into `components/mu/<id>/`. They wear the host's shadcn theme and share `components/mu/foundation.css` (tokens), `motion.ts` (springs, `useStill`, timing twins) and `sounds.ts` (sound, off unless asked). Docs for each item: https://cottoncandy.dev/md/<id>.md; the index: https://cottoncandy.dev/llms.txt.

## Using an item

- Prefer an existing cottoncandy item over writing a new one; build sections from its components.
- Read the item's markdown first and use only the props it documents. Other HTML attributes and `ref` go to its root element.
- Keep the host's theme: never hard-code colours, timings or fonts into a file under components/mu/. Restyle through its `--<id>-*` variables, `className`, or the shadcn roles.
- Keep keyboard behaviour and the reduced-motion path. Never edit foundation.css, motion.ts or sounds.ts by hand: newer items update them, and names there are append-only.

## The rules every item keeps

**Hard rules**:
- Colour only from roles: no hex, colour functions, named colours or Tailwind palette classes in a block.
- No `dark:`: roles flip on their own, so dark mode changes colour, never layout.
- Timings and curves are tokens or named constants; no `transition: all`. Animate transform and opacity. The exceptions are deliberate and marked `/* mu-ok: why */`: a one-shape morph that grows width only (record button), and frame loops that write a position directly.
- `"use client"` first, `data-mu` on the root, `import "../foundation.css"`.
- The root takes its element's own props and `ref` (`Omit<ComponentProps<"div">, "children" | …>`, `ComponentProps<"button">` for a button root, `ComponentProps<"span">` for a loader): `...rest` is spread first, so `data-mu`, `data-slot` and the item's own role and aria win; `className` merges with `cn`, `style` is spread under the item's own, a handler the item also uses runs before its own, and `ref` reaches the root element (`useImperativeHandle` when the item keeps its own ref).
- Motion blocks read `useStill()`, and CSS animations carry a reduced-motion branch.
- Motion components are `m.*` (`import * as m from "motion/react-m"`) inside `<LazyMotion features={domAnimation} strict>` around the root, never `<motion.*>`: 15 KB less per page. `layout` and `drag` need `domMax`; an item that uses them loads it and says why.
- No `!important`, no `:root`, `html` or `body` styles.
- Hover styles live inside `@media (hover: hover)` (Tailwind's `hover:` already does this), so nothing sticks after a tap; focus rings use `:focus-visible`, so they show for keyboards, not clicks.
- What ships is what the block needs: every package it imports is in its registry dependencies, every file it imports is shipped with it, and nothing framework-only (`next/link`, `next/image`): it renders plain `<a>` and `<img>`, so it runs in Next, Vite or any React app.

**By hand**:
1. **Derive sizes, don't declare them.** If a size follows from another (padding from row height), compute it. Write why each number is that number.
2. **Whole pixels for grid-drawn parts** (dots, ticks, bars), and 1px hairlines in `--mu-edge`.
3. **Targets:** 24px on a pointer, `--mu-hit` (44px) on touch, range inputs included. Grow the hit area with a centred `::after`, never the visible shape.
4. **States never move the box.** Pressed, busy and selected keep the same size; swapped labels share one grid cell.
5. **Nested corners are concentric:** inner radius = outer radius − inset.
6. **Pair surface and ink:** `bg-card` with `text-card-foreground`. Text 4.5:1, control edges 3:1, in every theme. Mix near-greys `in oklab`: `oklch` drops the hue of a near-grey and Chromium paints it pink. A shadow is `--mu-shadow` (black in dark) or `--mu-hw-*-shadow`, never mixed from the foreground. Colour never carries a state alone: a tone is paired with a mark or a word (a tick, a cross, "Recording").
7. **Works from 320px.** Use `@container` when a block responds to its own width.
8. **Every hover has a tap and keyboard path; every drag has a button or key,** and a cancel.
9. **Native elements first** (`button`, `input type=range`, `role="switch"`). Icon-only controls take a `label` prop. A slider or scrubber whose number isn't the words (a time, a percent) sets `aria-valuetext` ("1:05 of 3:42"). An SVG `id` (gradient, filter, clip, mask) comes from `useId()`, or two copies on one page share it. Focus ring is 2px `--ring` at `--mu-focus-offset`, following the part's own radius, and never inside an `overflow: hidden` parent (ring the parent with `:has(> :focus-visible)` instead).
   **Buttons:** heights from `--mu-control-sm|md|lg` only; `type="button"` and a name; `cursor: pointer`; press is `--mu-press-scale` over `--mu-press` ease-out on the visible body, and a big physical part (64px and up) may go twice as deep, `calc(1 - 2 * (1 - var(--mu-press-scale)))`, never a literal; hover moves a child (ink to `--mu-hw-ink`, or `--mu-inlay` behind the key); a part that opens a popup (`aria-haspopup`, `aria-expanded`) never scales on press, it answers with colour, so the popup measures its real size; the focus ring is the host's `--ring`, keyboard only; disabled is 50% opacity, a not-allowed cursor and no press or hover; a control that is busy, or out of use for a moment while focused, stays focusable (`aria-disabled="true"`, its click ignored), or focus moves to what replaced it on purpose; icons don't take pointer events.
10. **Numbers that change** use `tabular-nums`.
11. **Copy is sentence case,** short, and in a `labels` prop when a buyer would want to translate it.

## Tokens

1. **The host's shadcn roles, by name:** `--background` `--foreground` `--card` `--card-foreground` `--muted` `--muted-foreground` `--primary` `--primary-foreground` `--border` `--input` `--ring` `--radius` `--chart-1…5`. We never define them; the block wears the host's theme.
2. **Our `--mu-*` names,** only for what shadcn lacks:

| Name | For |
| --- | --- |
| `--mu-signal`, `--mu-on-signal` | the accent: it tints only light (a crest, a key light, a glow) where an item's `accent` asks for it, never a button fill |
| `--mu-inlay` | recessed tracks and grilles |
| `--mu-edge` | hairlines |
| `--mu-ink-faint` | unlit ticks, empty dots |
| `--mu-shadow` | shadows and scrims; mixed from the foreground by day, black in dark |
| `--mu-hit` | 44px touch target |
| `--mu-control-sm` `--mu-control-md` `--mu-control-lg` | control heights 36 / 44 / 50px, shared by every button, switch, field and segment so composed blocks line up |
| `--mu-font-sans` `--mu-font-mono` | the faces: Geist, then Inter, then the system face; Geist Mono for readouts. Declared on `[data-mu]` |
| `--mu-loader-xs` `--mu-loader-sm` `--mu-loader-md` `--mu-loader-lg` `--mu-loader-xl` | the loader scale: 1em snapped to 4px (inline), 16 (button), 32 (card), 64 (section), 160px (hero); see FOUNDATION `### loaders` |
| `--mu-leading-display` `--mu-tracking-display` | 1.1 and −0.03em for big numbers and display lines |
| `--mu-press` `--mu-fast` `--mu-enter` `--mu-morph` | timing (MOTION) |
| `--mu-ease-out` `--mu-ease-in-out` | curves |
| `--mu-press-scale` | 0.97 |
| `--mu-focus-offset` | 2px, the gap between a part and its focus ring |
| `--mu-hw-*` | the hardware material (pearl by day, graphite under `.dark`): `body`, `well`, `groove`, `fill`, `dome`, `dimple`, `bezel` (each with `-shadow`), `ink`, `ink-2`, `emboss`, `key`, `key-emboss`, `dome-ink`, `sep`, and the amber `light`, `light-glow`, `spill`, `highlight` (reserved: no item uses them now) |

In classes, use Tailwind's variable syntax: `bg-(--mu-signal)`, `duration-(--mu-morph)`, `ease-(--mu-ease-out)`, `shadow-(color:--mu-shadow)`.

## Motion

1. **One idea.** Pick the single physical consequence of the action. A switch whose grille thumps once like a speaker cone says "on" better than a glow, a bounce and a sparkle together.
2. **Motion explains a real event:** a press, a value change, an arrival, a result. Otherwise cut it. **How often decides how much:** a part used many times a minute (a stepper, arrow keys on a slider, a held key) answers within `--mu-press` or not at all, and a key press never runs longer than `--mu-fast` before the state is readable. Long, expressive motion is for what a person does once or rarely (opening, sending, finishing).
3. **Cause before effect, on one clock.** The thing touched answers first, and its consequence follows on the same spring. The second part's window starts about 12% in. Never run two timers you have to keep in sync. A sound is part of the effect: it lands on the frame its event lands (the flip, the latch, the arm), not on pointer-down, unless the press itself is the event.
4. **Direction carries meaning.** Up rolls in from below, down from above. Only the digits that change move.
5. **Rest is still.** No idle loops. A block may play its idea once when half in view, then wait. Loaders and players may loop **while their job runs**, pause when off-screen, and stop when done. An illustrated scene may keep quiet ambient loops (a glint, a passing shooting star, drifting clouds), paused off-screen and with reduced motion.
6. **Interrupt, don't queue.** A second press reverses from where it is. One-shot effects (bursts, ripples) are keyed on a press counter, so each press gets a fresh one. A part that moves continuously stays mounted and is retargeted; only one-shots are keyed, because a new key restarts the motion.
7. **Never block input** while anything plays.
8. **Stagger** with `stagger.item` (35ms); stagger only the first few items, and keep the cascade under 0.4s.
9. **Physical limits.** Never start from `scale(0)`; start from 0.9–0.97 plus fade. Press is `--mu-press-scale` (0.97), up to 0.94 for a big physical part. Depth follows travel, not percentage: a wide plate (over about 160px) presses to about 0.985, so its edges move a couple of pixels. A part that opens a popup never scales; it answers with colour, so the popup measures its real size. Speed stretch keeps its area and stays under 1.12. Overshoot moves a body, never a measure: a key, a knob, a parked head or a digit wheel may land with up to 0.1 bounce, but a stroke being drawn, a corner radius, a fill or bar that shows a value, and a number's final position land exactly. A drawn line past its end is a glitch, and a corner that dips reads as wobble.
10. **Cheap properties only:** `transform`, `opacity`, `clip-path`, and blur under 8px. For a shadow, fade a pseudo-element that carries it. A CSS variable written every frame goes on the smallest element that reads it, never the root: inherited, it restyles everything under it.
11. **Hover moves a child,** so the target never slides out from under the pointer.
12. **Swap, don't overlap.** When one thing replaces another (a title, a cover, a label) the old leaves in `--mu-fast` ease-out and the new arrives 60–80ms later (`swap.leave`, `swap.arrive`), so two are never legible at once. Layers are keyed on a press counter, so fast presses never stack more than two. When still, exits are instant.
13. **Colour runs slower than motion, all at once.** A colour that changes with state is registered with `@property` (`syntax: "<color>"`) and transitioned over `--mu-morph` ease-out, so every part that wears it turns together. Never snap a colour, never fade one linearly.
14. **Off-screen is paused.** Every loop (rAF, canvas, CSS) stops when the item leaves the view (`useInView` from motion/react) and when its part is hidden.
15. **Reduced motion keeps the result and drops the travel.** Jump or fade to the final state. User-driven input such as drag or scrub still tracks, but snaps on release. Particles, glows and shimmer go.

### Timing

| Token | ms | Use |
| --- | --- | --- |
| `--mu-press` | 100 | pointer goes down |
| `--mu-fast` | 150 | hover, and anything leaving |
| `--mu-enter` | 200 | something arrives |
| `--mu-morph` | 300 | a thing changes shape or value in place |

Curves: `--mu-ease-out` (the default, for arrive and settle) and `--mu-ease-in-out` (for moving across the screen). In motion/react use the twins, `time.fast` and `ease.out`, never the numbers. Use `linear` only for progress and for something passing at constant speed. Never use ease-in, not even for an exit.

A block's expressive sequence keeps its own timings as named constants with a comment, for example `const POP = 0.52 // long enough to read the overshoot`. If something feels slow, shorten it before you change the curve.

## Before you call it done

- [ ] right in light and dark on phone and desktop, and with another brand colour and radius
- [ ] reduced motion shows the final state
- [ ] keyboard: reachable, visible focus, keys work
- [ ] press twice fast: it reverses cleanly
