Skip to content

Toast / useToast

Toast notification system with auto-dismiss capability. useToast() returns functions to create and manage notifications. ToastContainer renders the notification stack.

Import

tsx
import { useToast, ToastContainer, ToastItem } from "silvery"

useToast Hook

ts
interface UseToastResult {
  toast: (options: ToastOptions) => string // Show a new toast, returns ID
  toasts: ToastData[] // Currently visible toasts
  dismiss: (id: string) => void // Dismiss by ID
  dismissAll: () => void // Dismiss all
}

interface ToastOptions {
  title: string // Toast title text
  description?: string // Optional description
  variant?: ToastVariant // Visual variant (default: "default")
  duration?: number // Auto-dismiss in ms (default: 3000, 0 = no auto-dismiss)
}

type ToastVariant = "default" | "success" | "error" | "warning" | "info"

ToastContainer Props

PropTypeDefaultDescription
toastsToastData[]requiredToasts to render
maxVisiblenumber5Maximum visible toasts

ToastItem Props

PropTypeDefaultDescription
toastToastDatarequiredToast data to render

Variant Icons

VariantIconColor Token
default[i]$fg
success[+]$success
error[x]$error
warning[!]$warning
info[i]$info

Usage

tsx
function App() {
  const { toast, toasts } = useToast()

  return (
    <Box flexDirection="column">
      <Button
        label="Save"
        onPress={() => {
          toast({ title: "Saved", variant: "success", duration: 3000 })
        }}
      />
      <ToastContainer toasts={toasts} />
    </Box>
  )
}

See Also

  • Badge -- inline status label

Released under the MIT License.