> A guide to [@fabiocv/ui](https://ui.fabio.cv/). Every guide is listed at https://ui.fabio.cv/llms.txt.

# Motion

Everything moves on springs, given in Apple's terms: a response and a damping ratio. `response` is how many seconds one swing takes with no damping, so lower is quicker. A `dampingRatio` of `1` settles without overshooting, below `1` gives a little, and above `1` moves off quickly and eases into place.

## Rules

- Take one of the seven springs by its job. Do not write a duration or a cubic-bezier.
- Move with transforms, clips and opacity. Text stays put while the box around it springs. The exceptions are a highlight, one empty box that changes size, and a box that takes room from what is beside it, such as a `Button` revealing its icon, which springs its width.
- Let an edge land without overshooting. A sheet, a box changing size, and what shows and hides take a damping ratio of `1`. So does a cut that grows from an edge, as past its end it would only stall. The trace of give, `0.86`, is for what follows the pointer or slides from place to place, along with its fade and its resize.
- What is held stays under the pointer, with no spring between the two. A spring is for what trails it, and for what moves once it is let go. [skills.fabio.cv](https://skills.fabio.cv/) has this for a divider.
- Start from where things are. A movement cut short goes back from wherever it had got to, and never restarts.
- Things that move as one share one spring and one frame: a cut and the part in it, a row's text and its highlight, a sound and the motion it belongs to.
- Keep the distance short. A highlight drifts up to `8px`, a submenu slides in `8px`, a tooltip `4px`, and a highlight grows from a scale of `0.9`.
- Bring a thing out of what it came from. A menu opens out of its trigger, a tooltip grows away from its aligned side, and a drawer rises from the bottom of the window.
- Answer the pointer at once. A highlight fades in on `TRACK`, so it shows as soon as an item is hovered, and leaves on the slower `APPEAR`.

## Springs

| Spring    | Response | Damping ratio | Use for                                                                                                                            |
| --------- | -------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `TRACK`   | `0.15s`  | `0.86`        | What follows the pointer: a tooltip, a menu's highlight, a submenu and the rows beside it, ink on hover, and a highlight's fade in |
| `SNAP`    | `0.25s`  | `0.86`        | A small control sliding in and out: a switch's thumb and its track's fill                                                          |
| `APPEAR`  | `0.25s`  | `1`           | Showing and hiding: a tooltip, a revealed icon, a highlight's fade out and its scale                                               |
| `SLIDE`   | `0.35s`  | `0.86`        | Sliding and resizing from item to item: a list's highlight, a toggle group's thumb, and an icon's two states                       |
| `SETTLE`  | `0.4s`   | `1`           | A box changing size: a tooltip's width, a button growing round its icon, and the focus ring                                        |
| `PRESENT` | `0.45s`  | `1`           | A sheet over the page: a drawer, the page stepping back and its dimming                                                            |
| `UNFOLD`  | `0.05s`  | `2`           | A box opening out of what it came from: a menu. It covers most of the way in two frames, then eases in                             |

## Reduced motion

With `prefers-reduced-motion: reduce`, what moves lands at once and most of what fades still fades. New motion has to do the same.

| Component            | What is left                                                                       |
| -------------------- | ---------------------------------------------------------------------------------- |
| Highlight            | It only fades, and changes item at once                                            |
| `Button`, `Tag`      | They grow at once, and the icon fades in                                           |
| `Menu`               | It appears and goes at once                                                        |
| `Tooltip`            | It only fades, follows the pointer at once, and long text stays still              |
| `Drawer`             | The sheet only fades, and the page does not step back                              |
| `ToggleGroup`, icons | The thumb and the glyphs change at once                                            |
| `Switch`             | Its thumb moves at once, and its track still fades                                 |
| `ImageCard`          | The parts and their cuts land at once, and their content fades in. A film plays on |
| `FlipName`           | The letters are still                                                              |
| `FlowGradient`       | One still frame                                                                    |
| `useGravity`         | Nothing leans                                                                      |
| The focus ring       | It appears at once                                                                 |

## Using a spring

```tsx
import { motion, useSpring } from 'framer-motion';

import { APPEAR, physics, SLIDE, TRACK, transition } from '@fabiocv/ui/spring';

function Panel({ open }: { open: boolean }) {
  // With Motion, as stiffness and damping.
  const x = useSpring(0, physics(SLIDE));

  return (
    <motion.div
      style={{ x }}
      animate={{ opacity: open ? 1 : 0 }}
      transition={{ type: 'spring', ...physics(APPEAR) }}
    >
      {/* In CSS, as a duration and a `linear()` easing. */}
      <a style={{ transition: `color ${transition(TRACK)}` }}>Work</a>
    </motion.div>
  );
}
```

The package moves with `framer-motion` 11.18.2. An app that writes motion of its own installs the same one, so both share a frame.

- `physics(spring)` gives the spring to Motion, on a unit mass.
- `transition(spring)` gives it to CSS. Put the property before it.
- `progress(spring, t)` is how far it has come after `t` seconds, and `sample(spring)` is that at every frame until it settles, for a canvas, a sound or a drawing of its curve.

## Where it is not one of the seven

- `ImageCard` moves its parts and their cuts on one spring of its own, a response of `0.216s` and a damping ratio of `1`, which Motion writes as `visualDuration: 0.18`. With reduced motion their content fades in `0.15s` on a curve.
- `useGravity` leans on its own spring, `0.55s` and `0.825`, looser because it follows from a distance.
- `FlipName` turns on CSS transitions and keyframes, as it has to work in static HTML with no script.
- `FlowGradient` runs on time, as it never arrives anywhere.

## Types of `@fabiocv/ui/spring`

```ts
/**
 * A spring in Apple's terms. `response` is how many seconds one swing takes
 * with no damping, so lower is quicker. `dampingRatio` runs from `0`, which
 * swings forever, to `1`, which settles without overshooting. Above `1` it
 * moves off quickly and eases into place.
 */
export type Spring = {
    response: number;
    dampingRatio: number;
};

/** Follows the pointer closely, with a trace of give. */
export declare const TRACK: Spring;

/** Slides and resizes from item to item quickly, with a trace of give. */
export declare const SLIDE: Spring;

/** Slides a small control in and out, a tad quicker than `SLIDE`, with the same give. */
export declare const SNAP: Spring;

/** Resizes the box and settles without overshooting. */
export declare const SETTLE: Spring;

/** Shows and hides quickly, without overshooting. */
export declare const APPEAR: Spring;

/** Opens a box out of what it came from, quick off the mark, easing in. */
export declare const UNFOLD: Spring;

/** Brings a sheet up over the page and takes it away, without overshooting. */
export declare const PRESENT: Spring;

/** The spring as stiffness and damping on a unit mass, for Motion. */
export declare function physics(spring: Spring): {
    stiffness: number;
    damping: number;
    mass: number;
};

/** How much of the way from rest to target the spring has come at `t`. */
export declare function progress(spring: Spring, t: number): number;

/**
 * The spring's progress, one point a frame, until it stays within a
 * thousandth of the target, and the seconds that takes.
 */
export declare function sample(spring: Spring): {
    points: number[];
    duration: number;
};

/** The spring as a CSS transition's duration and `linear()` easing. */
export declare function transition(spring: Spring): string;
```

See it at https://ui.fabio.cv/motion/.
