Toast

Transient messages that stack and dismiss themselves. A provider plus a useToast() hook rather than a global singleton, so the queue lives in React state and two regions can coexist.

src/components/ui/toast.tsx

About

From the component's own source, where it was written.

Transient messages that stack and dismiss themselves.

A provider plus a useToast() hook, rather than a global singleton: the queue lives in React state, so it participates in normal rendering and testing, and two independent regions can coexist.

The region is aria-live="polite" so a toast is announced without stealing focus — a toast that grabs focus interrupts whatever the user was typing.

Install

npx astralyx-ui add toast

Usage

import { ToastProvider, useToast } from '@/components/ui/toast'

// once, near the root
<ToastProvider position="bottom-end">{children}</ToastProvider>

// anywhere below it
const { toast } = useToast()
toast({ title: 'Saved', description: 'Your changes are live.', color: 'green' })

Composer

Change the props and the source below follows.

Props
tsx
const { toast } = useToast()

toast({
  title: 'Deployment finished',
  description: 'Build 1482 is live on production.',
  color: 'neutral',
  duration: 4000,
})

Examples

Queue and stacking

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

ToastProvider

PropTypeDefaultDescription
childrenReactNoderequired—
maxnumber4Oldest toasts fall off once this many are showing.
position'top-end' | 'top-start' | 'bottom-end' | 'bottom-start''bottom-end'Corner the region is anchored to. Bottom positions stack upward.
id(id: number) => void—Removes a toast early.

Toast

Extends Omit<ComponentProps<'div'>, 'title' | 'color'>.

PropTypeDefaultDescription
titleReactNode——
descriptionReactNode——
colorColorSet'neutral'Tints the title.
durationnumber4000Milliseconds before auto-dismiss. 0 keeps it until dismissed.
actionReactNode——
onDismiss() => voidrequired—
dismissLabelstring'Dismiss'Accessible name for the dismiss button.

Hooks

HookSignatureDescription
useToast() => ToastContextValue—
useToastActions() => ToastActionsThe same two functions without the list. A component that only raises toasts should use this: useToast also returns toasts, so it re-renders every time one appears or leaves.

Types

ToastOptions

Transient messages that stack and dismiss themselves.

FieldTypeDescription
title?ReactNode—
description?ReactNode—
color?ColorSet—
duration?numberMilliseconds before auto-dismiss. 0 keeps it until dismissed.
action?ReactNode—

Behaviour

TopicDetailDescription
toast(options)(options) => numberQueues a toast and returns its id.