Async State
NewLoading, empty, error and timeout states in one place, with retries and screen reader announcements.
Installation
$ pnpm dlx shadcn@latest add @synccn/async-stateFirst 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
TThe loaded data, passed to `children`.
- error
unknownError message (or the thrown value) for the error state.
- onRetry
() => voidCalled by the "Try again" button in the error and timeout states.
- loading
React.ReactNodeShown while loading. Defaults to three shimmering lines.
- empty
React.ReactNodeShown when the result is empty.
- errorFallback
(error: unknown, retry?: () => void) => React.ReactNodeShown when loading fails. Defaults to the error message with a retry button.
- label
stringDefault
"content"What loading is about, for screen readers, e.g. "invoices".
- childrenrequired
(data: T) => React.ReactNodeRenders the data.
- className
stringExtra 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
- Button for
Button
Helpers
- lib/utilscn (merges class names)
npm packages
- clsx for cn()
- tailwind-merge for cn()
- lucide-react for AlertCircle, Clock, Inbox, RotateCcw
Used by
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