# motion/use-flip-list (zidane-ui)

Import: `import { … } from 'zidane-ui'`. Source: [ui/src/motion/use-flip-list.ts](https://github.com/Tahul/zidane/blob/main/ui/src/motion/use-flip-list.ts).

## Functions

### `useFlipList`

Function.

[`createFlipList`](https://zidane.si/docs/reference/ui-motion-flip-list.md#createFlipList) on a React list: measures after every commit where `layoutKey` changed
and animates what moved, arrived or left. Give the list element `ref`; a new element (a
remount) starts a fresh controller.

```ts
function useFlipList(layoutKey: unknown, options?: UseFlipListOptions): UseFlipList
```

**Parameters**

- `layoutKey` (`unknown`): Changes whenever the rows may have moved (their ids in order, folded groups).
- `options?` (`UseFlipListOptions`, default `{}`): See [`UseFlipListOptions`](https://zidane.si/docs/reference/ui-motion-use-flip-list.md#UseFlipListOptions).

**Returns** `UseFlipList`

Source: [ui/src/motion/use-flip-list.ts:44](https://github.com/Tahul/zidane/blob/main/ui/src/motion/use-flip-list.ts#L44)

## Classes

### `FlipSnapshot`

Class.

Records the rows right before each commit changes them (`getSnapshotBeforeUpdate`, the one moment
React offers before the DOM moves), so a glide starts where its row really was: a fold that settled,
rows that grew as they painted, an area that resized since the last update all count. Wrap the list's
rows in it, inside the element `useFlipList`'s ref is on.

```ts
class FlipSnapshot extends Component<FlipSnapshotProps>
```

**Constructor**

```ts
new FlipSnapshot(props: FlipSnapshotProps)
```

Creates the wrapper.

**Parameters**

- `props` (`FlipSnapshotProps`): `FlipSnapshotProps`.

**Returns** `FlipSnapshot`

**Methods**

#### `componentDidUpdate()`

```ts
componentDidUpdate(): void
```

Called immediately after updating occurs. Not called for the initial render.

The snapshot is only present if `getSnapshotBeforeUpdate` is present and returns non-null.

#### `getSnapshotBeforeUpdate()`

```ts
getSnapshotBeforeUpdate(prev: Readonly<FlipSnapshotProps>): null
```

Records the rows when the layout key changes, before React moves them.

**Parameters**

- `prev` (`Readonly<FlipSnapshotProps>`): The props of the commit before.

**Returns** `null`: Nothing: React needs a value.

#### `render()`

```ts
render(): ReactNode
```

The rows, as given.

**Returns** `ReactNode`

Source: [ui/src/motion/use-flip-list.ts:103](https://github.com/Tahul/zidane/blob/main/ui/src/motion/use-flip-list.ts#L103)

## Interfaces

### `FlipPlan`

Interface.

How a commit's changes play: [`UseFlipListOptions`](https://zidane.si/docs/reference/ui-motion-use-flip-list.md#UseFlipListOptions)' `animate` and `mode`.

```ts
interface FlipPlan
```

**Properties**

- `animate` (`boolean`): Animate this commit's changes. False records only (a group folding, a search).
- `mode` (`FlipMode`): How this commit's changes play (`fold`: a group folding around rows that stay).

Source: [ui/src/motion/use-flip-list.ts:8](https://github.com/Tahul/zidane/blob/main/ui/src/motion/use-flip-list.ts#L8)

### `UseFlipList`

Interface.

What [`useFlipList`](https://zidane.si/docs/reference/ui-motion-use-flip-list.md#useFlipList) returns.

```ts
interface UseFlipList
```

**Properties**

- `controller` (`{ readonly current: FlipList | null }`): The live controller, to `suspend` / `release` around a drag.
- `ref` (`(el: HTMLElement | null) => void | (() => void)`): The ref callback for the list element.

Source: [ui/src/motion/use-flip-list.ts:29](https://github.com/Tahul/zidane/blob/main/ui/src/motion/use-flip-list.ts#L29)

### `UseFlipListOptions`

Interface.

Options of [`useFlipList`](https://zidane.si/docs/reference/ui-motion-use-flip-list.md#useFlipList).

```ts
interface UseFlipListOptions extends FlipListOptions
```

**Properties**

- `animate?` (`boolean`): Animate this commit's changes. False records only (a group folding, a search). Default true.
- `isSticky?` (`(el: HTMLElement) => boolean`, from `FlipListOptions`): A sticky header, measured at its unstuck place. Default: it carries `data-flip-sticky`.
- `keyOf?` (`(el: HTMLElement) => string | null`, from `FlipListOptions`): A row's key, or null to skip it. Default: its `data-flip-key`.
- `maxChanges?` (`number`, from `FlipListOptions`): Past this many rows entering or leaving in one update, nothing animates. Default 40.
- `maxTravel?` (`number`, from `FlipListOptions`): The most a row travels on enter or exit, in px. Default 40.
- `mode?` (`FlipMode`): How this commit's changes play (`fold`: a group folding around rows that stay). Default `list`.
- `plan?` (`() => FlipPlan`): Decides `animate` and `mode` as the commit moves the rows, for a list that compares with its commit before
  (a ref no render reads). Wins over both. Give [`FlipSnapshot`](https://zidane.si/docs/reference/ui-motion-use-flip-list.md#FlipSnapshot) the same.
- `prefs?` (`() => MotionPrefs`, from `FlipListOptions`): The motion switches. Default: read off `<html>`.
- `selector?` (`string`, from `FlipListOptions`): The rows to track. Default `[data-flip-key]`.

Source: [ui/src/motion/use-flip-list.ts:16](https://github.com/Tahul/zidane/blob/main/ui/src/motion/use-flip-list.ts#L16)
