Skip to content
synccn

Async State

New

Loading, empty, error and timeout states in one place, with retries and screen reader announcements.

Loading invoices

Installation

$ pnpm dlx shadcn@latest add @synccn/async-state

First time in this project? Connect synccn with one command.

Usage

import { AsyncState, useAsync } from "@/components/ui/async-state";const result = useAsync((signal) => fetchInvoices(signal), []);<AsyncState {...result} onRetry={result.retry}>{(rows) => …}</AsyncState>

API reference

Generated from the component's TypeScript types, so it always matches the code.

AsyncState

One consistent way to show loading, empty, error, timeout and loaded states, with skeleton-to-content fades, retry buttons and polite screen reader announcements.

  • statusrequired
    "idle" | "loading" | "success" | "empty" | "error" | "timeout"

    Current status, usually from useAsync.

  • data
    T

    The loaded data, passed to `children`.

  • error
    unknown

    Error message (or the thrown value) for the error state.

  • onRetry
    () => void

    Called by the "Try again" button in the error and timeout states.

  • loading
    React.ReactNode

    Shown while loading. Defaults to three shimmering lines.

  • empty
    React.ReactNode

    Shown when the result is empty.

  • errorFallback
    (error: unknown, retry?: () => void) => React.ReactNode

    Shown when loading fails. Defaults to the error message with a retry button.

  • label
    string

    Default "content"

    What loading is about, for screen readers, e.g. "invoices".

  • childrenrequired
    (data: T) => React.ReactNode

    Renders the data.

  • className
    string

    Extra classes for the root element.

useAsync

Runs an async loader and tracks it as one status: loading, success, empty, error or timeout, with optional automatic retries. Late answers from earlier runs are ignored.

Takes no props.

Dependencies

Read from the component's imports when the site builds. The CLI installs all of these for you.

Components

Helpers

npm packages

Accessibility

  • Each state change is announced in a polite live region (loading, loaded, empty, failed, timed out).
  • The container sets aria-busy while loading.
  • Retry is a real button with a visible label.

From the source

Read from this component's code when the site builds, so it can't drift.

  • ARIA roles: status
  • ARIA attributes: aria-busy, aria-live
  • Includes text only screen readers hear
  • Announces changes to screen readers