# Loading State

> Show that long-running work is in progress, with a label and the time elapsed.

Source: https://www.honestui.com/docs/ai/loading-state

```tsx
import { LoadingState } from "@/components/ai/loading-state"

/**
 * A reply in progress, shown where it usually sits: under the reader's own
 * request. The bubble is sample copy; nothing is building an export here.
 */
export function LoadingStateDemo() {
  return (
    <div className="flex w-full max-w-[32rem] min-w-0 flex-col gap-[var(--hui-space-6)]">
      <p className="m-0 max-w-[80%] self-end rounded-[var(--hui-radius-4)] bg-[var(--hui-color-background-base-secondary)] px-[var(--hui-space-5)] py-[var(--hui-space-3)] text-[var(--hui-color-foreground-base-primary)] [font-size:var(--hui-font-size-regular)] [line-height:20px]">
        Export last quarter&apos;s invoices as a CSV, grouped by region.
      </p>
      <LoadingState label="Building the export" />
    </div>
  )
}

```

## Overview [#overview]

Loading State is one line: a small animated mark, a label that says what is in progress, and how long it has been running. Use it when work takes long enough that a person may wonder whether anything is happening.

Render it while the work runs and remove it when the work ends. Loading State does not know when your work finishes and reports no progress of its own. The elapsed time is real: it counts from `startedAt`, or from when the component mounted.

Use [Thinking State](/docs/ai/thinking-state) instead when you have steps to show.

## Anatomy [#anatomy]

<Anatomy title="LoadingState">
  <AnatomyItem name="Mark" description="A compact decorative pattern of pulsing cells" />

  <AnatomyItem name="Label" description="What is in progress" />

  <AnatomyItem name="Elapsed" description="Seconds, then minutes and seconds, since the work began" />
</Anatomy>

Installing `loading-state` brings [Text Shimmer](/docs/animated/text-shimmer) along as a registry dependency.

## Behavior [#behavior]

### Patterns [#patterns]

All patterns occupy the same space and use the same foreground color. The pattern is decorative and reports no progress.

Pick one or two patterns for a product and use them consistently, so the same kind of work always looks the same:

* `drive` for general work, such as building, writing, or running a tool. It is the default.
* `orbit` for waiting on something outside the product, such as a connection, a queue, or another service.
* `pulse` where space is tight, such as inline in a row or a button.

The other patterns are alternatives to these for interfaces that need a different form.

| Pattern    | Form and motion                                                          |
| ---------- | ------------------------------------------------------------------------ |
| `drive`    | Nine rounded squares with a wave across the grid.                        |
| `dots`     | The same wave, with round cells.                                         |
| `orbit`    | A pulse around the grid's edge, with a dim center.                       |
| `diagonal` | Nine rounded squares with a diagonal brightness sweep.                   |
| `diamond`  | Five rounded squares: a pulse around four points and through the center. |
| `square`   | Eight rounded squares around an empty center, with a traveling pulse.    |
| `pulse`    | Three rounded squares that brighten and gently scale in sequence.        |

### Elapsed time [#elapsed-time]

The time updates once a second and reads as `42s`, then `1m 12s`. Pass `startedAt` when the work began before the component mounted, for example after a page reload. Set `showElapsed` to `false` to leave it out.

## Accessibility [#accessibility]

The label is a polite status, so it is announced when the component appears and when the label changes. The elapsed time is hidden from screen readers, because a status that changes every second would be read every second. The mark is decorative.

With reduced motion, the cells stay dim and the label stops shimmering. The elapsed time still updates.

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;@honestai/loading-state&#x22;]" />
  

  
    
      
        Add the components it is built from.
      

      <CliBlock commands="[&#x22;text-shimmer&#x22;]" />

      
        Copy and paste this file into your project.
      

      ### components/ai/loading-state.tsx

```tsx
"use client"

import * as React from "react"

import { cn } from "@/lib/utils"
import { TextShimmer } from "@/registry/default/animated/text-shimmer"

export type LoadingPattern =
  | "drive"
  | "dots"
  | "orbit"
  | "diagonal"
  | "diamond"
  | "square"
  | "pulse"

// Each cell's delay in the cycle. `null` leaves a cell unlit.
const waveDelays = Array.from({ length: 9 }, (_, index) => {
  const row = Math.floor(index / 3)
  const column = index % 3
  return (column + Math.abs(row - 1)) * 90
})

const perimeter = [0, 1, 2, 5, 8, 7, 6, 3]
const orbitDelays = Array.from({ length: 9 }, (_, index) => {
  const position = perimeter.indexOf(index)
  return position === -1 ? null : position * 110
})

const patterns: Record<
  LoadingPattern,
  {
    delays: (number | null)[]
    durationMs: number
    isRound?: boolean
    hideEmpty?: boolean
    animationName?: string
  }
> = {
  drive: { delays: waveDelays, durationMs: 650, isRound: false },
  dots: { delays: waveDelays, durationMs: 650, isRound: true },
  orbit: { delays: orbitDelays, durationMs: 950, isRound: false },
  diagonal: {
    delays: Array.from({ length: 9 }, (_, index) =>
      -1200 + (Math.floor(index / 3) + (index % 3)) * 150
    ),
    durationMs: 1200,
  },
  diamond: {
    // Top, right, bottom, and left travel around a softly pulsing center.
    delays: [null, -1200, null, -300, -750, -900, null, -600, null],
    durationMs: 1200,
    hideEmpty: true,
  },
  square: {
    delays: Array.from({ length: 9 }, (_, index) => {
      const position = perimeter.indexOf(index)
      return position === -1 ? null : -1200 + position * 150
    }),
    durationMs: 1200,
    hideEmpty: true,
  },
  pulse: {
    delays: [-1200, -1000, -800],
    durationMs: 1200,
    animationName: "hui-loading-pulse",
  },
}

const gridStyles = `
@keyframes hui-loading-cell{0%,100%{opacity:.15}40%{opacity:1}}
@keyframes hui-loading-pulse{0%,70%,100%{opacity:.25;transform:scale(.85)}35%{opacity:1;transform:scale(1)}}
@media(prefers-reduced-motion:reduce){.hui-loading-cell{animation:none!important;transform:none!important}.hui-loading-cell[data-lit]{opacity:.7!important}}
@media(forced-colors:active){.hui-loading-cell{background:CanvasText}}
`

function formatElapsed(milliseconds: number) {
  const seconds = Math.max(0, Math.floor(milliseconds / 1000))
  if (seconds < 60) return `${seconds}s`
  return `${Math.floor(seconds / 60)}m ${seconds % 60}s`
}

/** Seconds since `startedAt`, updated once a second. */
function useElapsed(startedAt: number | Date | undefined, isEnabled: boolean) {
  const [mountedAt] = React.useState(() => Date.now())
  const start = startedAt === undefined ? mountedAt : new Date(startedAt).getTime()
  const [now, setNow] = React.useState(() => Date.now())

  React.useEffect(() => {
    if (!isEnabled) return
    const timer = window.setInterval(() => setNow(Date.now()), 1000)
    return () => window.clearInterval(timer)
  }, [isEnabled])

  return formatElapsed(now - start)
}

export type LoadingStateProps = Omit<React.ComponentProps<"div">, "children"> & {
  /** What is in progress, such as "Building the export". */
  label: string
  pattern?: LoadingPattern
  /** Shows the time since `startedAt`. */
  showElapsed?: boolean
  /** When the work began. Defaults to when the component mounted. */
  startedAt?: number | Date
}

/**
 * A small animated mark, a label, and the time elapsed, for work that takes
 * more than a moment. Render it while the work runs and remove it when it ends.
 */
function LoadingState({
  label,
  pattern = "drive",
  showElapsed = true,
  startedAt,
  className,
  ...props
}: LoadingStateProps) {
  const elapsed = useElapsed(startedAt, showElapsed)
  const { delays, durationMs, isRound, hideEmpty, animationName = "hui-loading-cell" } = patterns[pattern]

  return (
    <div
      data-slot="loading-state"
      className={cn(
        "flex w-fit max-w-full items-center gap-[var(--hui-space-3)] [font-size:var(--hui-font-size-regular)] [letter-spacing:var(--hui-letter-spacing-regular)] [line-height:var(--hui-line-height-regular)]",
        className
      )}
      {...props}
    >
      {/* href and precedence let React hoist this once, however many marks render. */}
      <style href="hui-loading-state" precedence="default">
        {gridStyles}
      </style>
      <span
        aria-hidden="true"
        data-slot="loading-state-mark"
        data-pattern={pattern}
        className="grid size-[var(--hui-space-5)] shrink-0 content-center grid-cols-[repeat(3,var(--hui-space-2))] gap-[var(--hui-space-1)]"
      >
        {delays.map((delay, index) => (
          <span
            key={index}
            // Under reduced motion the lit cells hold still at a readable strength.
            data-lit={delay === null ? undefined : ""}
            className={cn(
              "hui-loading-cell size-[var(--hui-space-2)] bg-[var(--hui-color-foreground-base-primary)]",
              isRound ? "rounded-full" : "rounded-[1px]"
            )}
            style={{
              opacity: delay === null ? 0.08 : 0.15,
              visibility: delay === null && hideEmpty ? "hidden" : undefined,
              animation:
                delay === null
                  ? undefined
                  : `${animationName} ${durationMs}ms ease-in-out ${delay}ms infinite`,
            }}
          />
        ))}
      </span>
      {/* Only the label is a status. A ticking timer would be announced every second. */}
      <span role="status" className="min-w-0">
        <TextShimmer
          duration={1.6}
          className="[font-weight:var(--hui-font-weight-medium)]"
        >
          {label}
        </TextShimmer>
      </span>
      {showElapsed && (
        <span
          aria-hidden="true"
          // The server and the browser read the clock at different moments.
          suppressHydrationWarning
          data-slot="loading-state-elapsed"
          className="shrink-0 text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-small)] [letter-spacing:var(--hui-letter-spacing-small)] [line-height:var(--hui-line-height-small)]"
        >
          {/* Tabular figures on the digits only; the tabular feature would space the units too. */}
          {elapsed.split(/(\d+)/).map((part, i) =>
            i % 2 ? (
              <span key={i} className="tabular-nums">
                {part}
              </span>
            ) : (
              part
            )
          )}
        </span>
      )}
    </div>
  )
}

export { LoadingState }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { LoadingState } from "@/components/ai/loading-state"

{run.isPending && <LoadingState label="Building the export" startedAt={run.startedAt} />}
```

Write the label as the work, not as "Loading". "Building the export" tells the person what they are waiting for.

## Examples [#examples]

### Patterns [#patterns-1]

The seven patterns, without the elapsed time.

```tsx
import { LoadingState } from "@/components/ai/loading-state"

export function LoadingStatePatternsExample() {
  return (
    <div className="flex flex-col gap-[var(--hui-space-5)]">
      <LoadingState pattern="drive" label="Reading 3 files" showElapsed={false} />
      <LoadingState pattern="dots" label="Comparing totals" showElapsed={false} />
      <LoadingState pattern="orbit" label="Waiting for the test run" showElapsed={false} />
      <LoadingState pattern="diagonal" label="Scanning the document" showElapsed={false} />
      <LoadingState pattern="diamond" label="Organizing the results" showElapsed={false} />
      <LoadingState pattern="square" label="Preparing the download" showElapsed={false} />
      <LoadingState pattern="pulse" label="Drafting the reply" showElapsed={false} />
    </div>
  )
}

```

## API reference [#api-reference]

### LoadingState [#loadingstate]

Accepts native div props beyond those listed.

| Prop          | Type                                                                             | Default    | Description                       |
| ------------- | -------------------------------------------------------------------------------- | ---------- | --------------------------------- |
| `label`       | string                                                                           | Required   | What is in progress.              |
| `pattern`     | `"drive" \| "dots" \| "orbit" \| "diagonal" \| "diamond" \| "square" \| "pulse"` | `"drive"`  | The mark's form and animation.    |
| `showElapsed` | boolean                                                                          | `true`     | Shows the time since `startedAt`. |
| `startedAt`   | `number \| Date`                                                                 | Mount time | When the work began.              |
