Sonner
The toast system. Mount the Toaster once, then fire short, non-blocking messages with the toast helpers after an action.
Select a button to fire a toast. It appears at the bottom of the page and closes by itself.
When to use
- To confirm that an action worked and nothing else needs to happen: “Contestação enviada para revisão.”
- To report a failure of a background action when no field or panel can show it: “Não foi possível salvar a tarefa.”
- To follow an async operation from start to end with
toast.promise. - To offer a quick way back right after an action, such as “Desfazer”.
When not to use
- A decision or a message that must be read. Use Dialog. A toast closes after a few seconds and is easy to miss.
- An error next to a form field. Show it inline with the field, near its cause.
- A message that stays on the page. Use Alert.
- Loading of a page or a list. Use Skeleton or Loader.
- State kept in the data, such as “Em revisão”. Use Status badge.
How it works
There are two parts, in two imports.
| Part | Import | Role |
|---|---|---|
Toaster |
@repo/ui/sonner |
The host. Mount it once at the app root, for example in the root layout. |
toast |
@repo/ui/toast |
The helpers. Call them from any event handler or mutation callback. |
Every Toaster shows every toast. Mounting two of them shows each message twice.
Types
| Call | Use for |
|---|---|
toast(message) |
Neutral confirmation: “Rascunho salvo.” |
toast.success |
The action finished. |
toast.error |
The action failed. |
toast.warning |
Something needs attention soon. |
toast.info |
Neutral news that is not a result of an action. |
toast.loading |
An operation is running. Dismiss it yourself. |
toast.promise |
Follows a promise: loading, then success or error. |
Examples
Pass description for a second line and action for a button. The button closes the toast when pressed.
toast.success("Tarefa concluída", {
description: "O revisor Marina Souza foi avisado.",
});
toast("Tarefa arquivada.", {
action: { label: "Desfazer", onClick: undoArchive },
});
toast.promise(sendContestation(), {
error: "Falha ao enviar a contestação.",
loading: "Enviando contestação…",
success: "Contestação enviada.",
});
Content
- One sentence, ending with a period: “Contestação enviada para revisão.”
- Say what happened to which object. Avoid “Sucesso!” and “Erro”.
- For errors, add what the user can do: “Tente novamente.”
- Put the longer explanation in
description, not in the title. - Name actions with a verb: “Desfazer”, “Ver tarefa”.
- In the apps, take the text from the translation files and pass it in.
Accessibility
- Sonner renders toasts inside a live region (
aria-live="polite") labelled “Notifications”, so screen readers announce them without moving focus. Change the label withcontainerAriaLabel. - Pressing
Alt+Tfocuses the toast region. Change it withhotkey. - Toasts pause their timer while the pointer is over them.
- Toasts made with
toast,toast.success,toast.error,toast.warning,toast.infoandtoast.messageget a close button. Keyboard users can reach it and pressEnter. - A toast closes after 4 seconds by default. Do not put information there that the user must still have afterwards.
Code
// app/layout.tsx: once
import { Toaster } from "@repo/ui/sonner";
<Toaster />;
// anywhere else
import { toast } from "@repo/ui/toast";
toast.success("Contestação enviada para revisão.");
Props
Toaster. It forwards every prop of the Sonner Toaster. The ones used most:
Toaster| Prop | Type | Default | Description |
|---|---|---|---|
position | "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" | "bottom-center" | Where toasts stack on the page. |
richColors | boolean | true | Colors success, error, warning and info toasts. |
theme | "light" | "dark" | "system" | "system" | Color scheme. Follows the next-themes theme when there is a ThemeProvider. |
duration | number | 4000 | Milliseconds before a toast closes by itself. |
closeButton | boolean | false | Close button on every toast. The `toast` helpers already add it, except for `loading`. |
visibleToasts | number | 3 | How many toasts show at once. |
containerAriaLabel | string | "Notifications" | Accessible name of the toast region. Pass translated copy from the app. |
Toast helpers. toast(message, options) and its variants take a message (text or element) and these options:
toast options| Prop | Type | Default | Description |
|---|---|---|---|
description | ReactNode | — | Second line under the message. |
action | { label: ReactNode; onClick: (event) => void } | — | A button inside the toast. |
cancel | { label: ReactNode; onClick?: (event) => void } | — | A second, quieter button. |
duration | number | — | Overrides the Toaster duration for this toast. Use `Infinity` to keep it open. |
id | string | number | — | Identifier. Reuse it to update a toast, or pass it to `toast.dismiss(id)`. |
closeButton | boolean | true | The helpers turn it on. Pass `false` to hide it. |
onDismiss / onAutoClose | (toast) => void | — | Called when the user closes the toast, or when the timer does. |
Other helpers: toast.promise(promise, { loading, success, error }), toast.dismiss(id?) (all toasts when no id), toast.custom(render), toast.getToasts() and toast.getHistory().
Known issues
| Issue | Effect |
|---|---|
toast.promise, toast.loading and toast.custom are passed through without the close button the other helpers add. |
Those toasts cannot be dismissed with a close button. Keep a way to dismiss them in the caller. |
| The close button label is “Close toast” and the Toaster types do not expose a way to translate it. | Screen reader users hear English copy in the Portuguese interface. |
The action button uses group-[.toast]:text-primary-foreground, which is not a token of the Tailwind preset (it defines old-primary). |
The class generates no style, so the action text color may not follow the design. Check the preview. |
| Toasts close after 4 seconds, including errors. | A user who looks away misses the error. Show failures that need action inline as well. |