Toast
A toast that shows temporary notifications. Can be triggered with a programmatic API (toast.success, etc.).
View in Storybook (opens in a new tab)
Installation
import { Toast } from "@ginga-ui/core";Basic usage
Place ToastRegion once at the root of your app and show notifications with toast.show. Call the toast functions from client event handlers.
toast.tsx
import { Button, toast, ToastRegion } from "@ginga-ui/core";
import { X } from "lucide-react";
export function BasicExample() {
return (
<>
<ToastRegion closeIcon={<X />} />
<Button
variant="filled"
onPress={() => toast.show("You have a new notification")}
>
Show toast
</Button>
</>
);
}Variants
Use toast.info, toast.success, toast.warning, and toast.danger to show notifications that match the situation.
toast.tsx
toast.module.css
import { Button, toast } from "@ginga-ui/core";
import styles from "./toast.module.css";
import { CircleAlert, CircleCheck, Info, TriangleAlert } from "lucide-react";
export function VariantsExample() {
return (
<div className={styles.variantList}>
<Button
variant="outline"
onPress={() =>
toast.info("A new version is available", { icon: <Info /> })
}
>
Info
</Button>
<Button
variant="outline"
onPress={() =>
toast.success("Your changes have been saved", {
icon: <CircleCheck />,
})
}
>
Success
</Button>
<Button
variant="outline"
onPress={() =>
toast.warning("You are running low on storage space", {
icon: <TriangleAlert />,
})
}
>
Warning
</Button>
<Button
variant="outline"
onPress={() =>
toast.danger("Failed to load data", {
icon: <CircleAlert />,
})
}
>
Danger
</Button>
</div>
);
}Auto close
Specify the timeout option to close the toast automatically. The timer pauses while it is hovered or focused.
toast.tsx
import { Button, toast } from "@ginga-ui/core";
import { CircleCheck } from "lucide-react";
export function TimeoutExample() {
return (
<Button
variant="filled"
onPress={() =>
toast.success("Upload complete", {
icon: <CircleCheck />,
description: "Closes automatically after 5 seconds.",
timeout: 5000,
})
}
>
Auto-dismissing toast
</Button>
);
}Props
ToastRegion
| Name | Type | Default | Description |
|---|---|---|---|
placement | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "bottom-right" | Corner of the screen where toasts appear. |
queue | ToastQueue<ToastContentValue> | toast's queue | Queue of toasts to display. By default, it shows toasts added with toast. |
closeIcon | ReactNode | - | Icon of the close button. |
closeLabel | string | - | Accessible name of the close button. |
Also accepts the props of React Aria's ToastRegion (opens in a new tab).
toast
Functions that add a toast. Each returns the key of the toast.
| Name | Type | Default | Description |
|---|---|---|---|
show | (title: string, options?: ToastOptions & { variant?: ToastVariant }) => string | - | Adds a toast with any variant. |
info / success / warning / danger | (title: string, options?: ToastOptions) => string | - | Adds a toast with the corresponding variant. |
close | (key: string) => void | - | Closes the toast with the given key. |
ToastOptions
| Name | Type | Default | Description |
|---|---|---|---|
description | string | - | Text shown below the title. |
icon | ReactNode | - | Icon shown before the title. |
timeout | number | - | Milliseconds until the toast closes automatically. When omitted, it stays until closed. |
onClose | () => void | - | Called when the toast closes. |