# Stagger List
A list whose items arrive one after another, in reading order.
Source page: https://dinachi.dev/docs/components/stagger-list

## Installation
```bash
npx @dinachi/cli@latest add stagger-list
```
Adds to your project:
- `components/ui/stagger-list/stagger-list.tsx`
- `components/ui/stagger-list/index.ts`
Installs:
- `motion` ^12.23.6

## Usage
```tsx
import { StaggerList, StaggerListItem } from "@/components/ui/stagger-list"

const results = ["Frankfurt", "London", "Tokyo"]

export function Results() {
  return (
    <StaggerList>
      {results.map((city) => (
        <StaggerListItem key={city}>{city}</StaggerListItem>
      ))}
    </StaggerList>
  )
}
```

## Anatomy
```tsx
<StaggerList>
  <StaggerListItem />
</StaggerList>
```
- `StaggerList`: The `ul` that sequences its items.
- `StaggerListItem`: One `li`. Throws outside a `StaggerList`.

## Examples

### Default
Items arriving one after another.
```tsx
import { StaggerList, StaggerListItem } from '@/components/ui/stagger-list';

export function Example() {
  return (
    <StaggerList className="w-56 space-y-2">
      {items.map((item) => (
        <StaggerListItem key={item.id}>{item.label}</StaggerListItem>
      ))}
    </StaggerList>
  );
}
```

### Variants
Rise, scale, or pull into focus.
```tsx
import { StaggerList, StaggerListItem } from '@/components/ui/stagger-list';

export function Example() {
  return (
    <StaggerList variant="blur" className="w-32 space-y-2">
      {items.map((item) => (
        <StaggerListItem key={item.id}>{item.label}</StaggerListItem>
      ))}
    </StaggerList>
  );
}
```

### Grid
A card wall, where a rise has no direction to follow.
```tsx
import { StaggerList, StaggerListItem } from '@/components/ui/stagger-list';

export function Example() {
  return (
    <StaggerList
      variant="scale"
      stagger={0.04}
      className="grid w-64 grid-cols-2 gap-2"
    >
      {cards.map((card) => (
        <StaggerListItem key={card.id}>{card.label}</StaggerListItem>
      ))}
    </StaggerList>
  );
}
```

## Notes

### Motion tier
Needs [motion](https://motion.dev). `add --all` skips it; install with `add --motion` or by name.

### Variants
- `rise` lifts items in from below. Use it for top-to-bottom lists.
- `scale` grows items in place. Use it for grids and card walls.
- `blur` brings items into focus as they rise. Use it sparingly: it animates a filter, so its cost grows with item count.

### When to use it
- A list seen for the first time: search results, a dashboard's first paint, a feed's first page.
- Skip it for lists that re-render on every keystroke, or content scrolled back to.

### Behaviour
- `blur` leaves `filter: blur(0px)` on each item, which makes it a containing block for fixed-position descendants. Use `rise` or `scale` if items contain one.
- `startOnView` triggers 100px inside the viewport edge, not at a visible fraction.

## API reference

### StaggerList
The `ul` that sequences its items.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"rise" \| "scale" \| "blur"` | `"rise"` | How each item enters. |
| `stagger` | `number` | `0.05` | Seconds between items. Keep it between 0.03 and 0.08. |
| `delay` | `number` | `0` | Seconds before the first item. |
| `duration` | `number` | `0.3` | Seconds for each item's own animation. |
| `distance` | `number` | `8` | Travel in px. Ignored by `scale`. |
| `startOnView` | `boolean` | `true` | Starts on scroll into view instead of mount. Runs once. |

### StaggerListItem
One `li`. Throws outside a `StaggerList`.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | - | The item's content. |

## Accessibility
- Keeps `ul` and `li` semantics.
- Reduced motion keeps the fade and stagger; drops travel, scale and blur.

## Source
`components/ui/stagger-list/stagger-list.tsx`
```tsx
"use client"

import * as React from "react"
import { motion, MotionConfigContext, useReducedMotion, type Variants } from "motion/react"
import { cn } from "@/lib/utils"

/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
 *  app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
 *  reaches this component. */
function useReducedMotionConfig() {
  const config = React.useContext(MotionConfigContext)
  const prefersReduced = useReducedMotion()
  if (config.reducedMotion === "always") return true
  if (config.reducedMotion === "never") return false
  return prefersReduced
}

const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]

export type StaggerListVariant = "rise" | "scale" | "blur"

/** One duration for every variant. A list entrance is UI, and UI motion stays under
 *  300ms whichever channel it animates — a variant that needs longer to read is a
 *  variant that is doing too much. */
const DEFAULT_DURATION = 0.3

/** Reduced motion drops to a bare fade, which has less to say and so needs less time. */
const REDUCED_DURATION = 0.2

/** Blur is the one channel here the compositor cannot do for free, and its cost scales
 *  with the radius rather than with the fact of blurring. Small enough to stay cheap on
 *  a long list, large enough to still read as focus arriving. */
const BLUR_RADIUS = 2

interface StaggerContextValue {
  itemVariants: Variants
}

const StaggerContext = React.createContext<StaggerContextValue | null>(null)

export interface StaggerListProps
  extends React.ComponentProps<typeof motion.ul> {
  /** How each item enters. See each variant's note on the component. */
  variant?: StaggerListVariant
  /** Seconds between each item. Keep it in the 0.03–0.08 range — beyond that the
   *  last item is late enough to feel like a wait. */
  stagger?: number
  /** Seconds before the first item. */
  delay?: number
  /** Seconds for each item's own animation. */
  duration?: number
  /** Item travel distance in px. Ignored by `scale`, which does not travel. */
  distance?: number
  /** Start when scrolled into view rather than on mount. */
  startOnView?: boolean
}

/**
 * A list whose items arrive one after another, drawn three ways:
 *
 * - `rise` — items lift into place from below. The default, and the right one for
 *   anything read top to bottom, because the motion runs along the reading direction.
 * - `scale` — items settle in place from slightly small. Use it for grids and
 *   card walls, where there is no single reading direction for a rise to follow.
 * - `blur` — items pull into focus as they rise. The only variant that animates a
 *   filter, so it is the only one whose cost grows with the item count. Two things keep
 *   that in hand: the radius is small, and the item travels, which puts it on its own
 *   compositing layer so the filter is a layer operation rather than a repaint. The
 *   residue is a `filter: blur(0px)` left on each item after it lands, which makes that
 *   item a containing block for any fixed-position descendant.
 *
 * Every variant fades in, so a reduced-motion preference drops the second channel and
 * keeps the fade and the stagger — sequencing is not movement, and the sequence is the
 * information this component carries.
 */
const StaggerList = React.forwardRef<HTMLUListElement, StaggerListProps>(
  (
    {
      variant = "rise",
      stagger = 0.05,
      delay = 0,
      duration,
      distance = 8,
      startOnView = true,
      className,
      children,
      ...props
    },
    ref
  ) => {
    const reducedMotion = useReducedMotionConfig()
    const seconds = duration ?? DEFAULT_DURATION

    // The stagger survives a reduced-motion preference. What that preference asks for is
    // less movement, and a sequence of fades has none — collapsing it would delete the
    // component's whole point rather than soften it.
    const containerVariants: Variants = React.useMemo(
      () => ({
        hidden: {},
        show: {
          transition: {
            staggerChildren: stagger,
            delayChildren: delay,
          },
        },
      }),
      [stagger, delay]
    )

    const itemVariants: Variants = React.useMemo(() => {
      const transition = {
        duration: reducedMotion ? REDUCED_DURATION : seconds,
        ease: EASE_OUT,
      }

      if (reducedMotion) {
        return { hidden: { opacity: 0 }, show: { opacity: 1, transition } }
      }

      // Full transform strings rather than motion's `x`/`y`/`scale` shorthands. The
      // shorthands are driven from the main thread, which is exactly where the contention
      // is when a stagger runs: during a scroll, or while the page is still painting the
      // content the list is made of.
      switch (variant) {
        case "scale":
          return {
            hidden: { opacity: 0, transform: "scale(0.96)" },
            show: { opacity: 1, transform: "scale(1)", transition },
          }
        case "blur":
          return {
            hidden: {
              opacity: 0,
              filter: `blur(${BLUR_RADIUS}px)`,
              transform: `translateY(${distance}px)`,
            },
            show: {
              opacity: 1,
              filter: "blur(0px)",
              transform: "translateY(0px)",
              transition,
            },
          }
        default:
          return {
            hidden: { opacity: 0, transform: `translateY(${distance}px)` },
            show: { opacity: 1, transform: "translateY(0px)", transition },
          }
      }
    }, [variant, distance, seconds, reducedMotion])

    const contextValue = React.useMemo(() => ({ itemVariants }), [itemVariants])

    return (
      <StaggerContext.Provider value={contextValue}>
        <motion.ul
          ref={ref}
          data-slot="stagger-list"
          className={cn("list-none", className)}
          variants={containerVariants}
          initial="hidden"
          {...(
            // A margin rather than a visible fraction: a fraction of a tall list is a lot
            // of scrolling, so a grid would sit at zero opacity while its first rows were
            // already on screen. Insetting from the edge starts the sequence once the list
            // has properly arrived, rather than the instant it clips the fold.
            startOnView
              ? { whileInView: "show", viewport: { once: true, margin: "-100px" } }
              : { animate: "show" }
          )}
          {...props}
        >
          {children}
        </motion.ul>
      </StaggerContext.Provider>
    )
  }
)
StaggerList.displayName = "StaggerList"

export type StaggerListItemProps = React.ComponentProps<typeof motion.li>

const StaggerListItem = React.forwardRef<HTMLLIElement, StaggerListItemProps>(
  ({ className, children, ...props }, ref) => {
    const context = React.useContext(StaggerContext)

    if (!context) {
      throw new Error("StaggerListItem must be used within a StaggerList")
    }

    return (
      <motion.li
        ref={ref}
        data-slot="stagger-list-item"
        className={cn(className)}
        variants={context.itemVariants}
        {...props}
      >
        {children}
      </motion.li>
    )
  }
)
StaggerListItem.displayName = "StaggerListItem"

export { StaggerList, StaggerListItem }
```
