Toast
A short message that comes and goes: toast() from anywhere, one Toaster per app.
Web et mobile — le même composant pour React et React Native. Stable : son API ne change qu’à une version majeure.
Quand l’utiliser
- To confirm the result of an action: saved, sent, payment received.
- For an event the user did not trigger but should know: a new follower, a live auction.
- With an action (“Retry”, “Undo”) when there is one obvious next step.
Quand ne pas l’utiliser
Installation
Installer
npm install @krizaka/ui@beta @krizaka/tailwind@beta @krizaka/tokens@beta tailwindcssStyles
@import "tailwindcss";
@import "@krizaka/tailwind";
@import "@krizaka/ui/tailwind.css";Importer
import { toast, Toaster } from "@krizaka/ui/toast";Exemples
Tones
Default, success, warning and error with an action.
"use client";
import { Button } from "@krizaka/ui/button";
import { toast, Toaster } from "@krizaka/ui/toast";
import { useEffect } from "react";
// Each toaster has its own `id`: the toasts of this demo stay in this demo's toaster.
const toasterId = "tones";
const show = () => {
toast("Draft saved", { toasterId, description: "Autosaved a moment ago." });
toast.success("Payment received", { toasterId, description: "€12.00 from @maya." });
toast.warning("Your balance is low", { toasterId, description: "Two tips left before a top-up." });
toast.error("Upload failed", { toasterId, description: "The connection dropped at 64 %.", action: { label: "Retry", onClick: () => {} } });
};
/** `defaultOpen`: the toasts show at once and stay (the screenshot); otherwise the button shows them. */
export default function ToastTones({ defaultOpen = false }: { defaultOpen?: boolean }) {
useEffect(() => {
if (!defaultOpen) return;
toast.dismiss();
show();
}, [defaultOpen]);
return (
<>
<Toaster id={toasterId} label="Notifications" closeLabel="Dismiss" position="bottom-right" expand visibleToasts={4} duration={defaultOpen ? Number.POSITIVE_INFINITY : undefined} />
<Button variant="outline" onClick={show}>
Show again
</Button>
</>
);
}Custom
toast.custom: your JSX in the platform's shell.
"use client";
import { Avatar } from "@krizaka/ui/avatar";
import { Button } from "@krizaka/ui/button";
import { toast, Toaster } from "@krizaka/ui/toast";
import { useEffect } from "react";
// `toast.custom`: your JSX in the platform's shell — a live notification. One `Toaster` per app, in the root layout.
const show = () =>
toast.custom(
() => (
<div className="flex items-center gap-3">
<Avatar fallback="MA" size="sm" />
<div className="flex flex-col">
<span className="text-sm font-semibold text-fg">Maya started following you</span>
<span className="text-xs font-normal text-fg-secondary">Just now</span>
</div>
</div>
),
{ toasterId: "custom" },
);
export default function ToastCustom({ defaultOpen = false }: { defaultOpen?: boolean }) {
useEffect(() => {
if (defaultOpen) show();
}, [defaultOpen]);
return (
<>
<Toaster id="custom" label="Notifications" closeLabel="Dismiss" position="bottom-right" duration={defaultOpen ? Number.POSITIVE_INFINITY : undefined} />
<Button variant="outline" onClick={show}>
Show a notification
</Button>
</>
);
}Props
Mount once, in the root layout. Then call toast(), toast.success/warning/error/info, toast.custom(…).
| Prop | Type | Défaut | Description |
|---|---|---|---|
closeLabelrequis | string | — | The accessible name of each toast's close button — passed translated. |
labelrequis | string | — | The accessible name of the notifications region (sonner's containerAriaLabel) — passed translated. |
toastOptions | Omit<ToastOptions, "unstyled" | "closeButtonAriaLabel"> | — | Extra options for every toast (duration, a className); the class names are merged with the platform's. |
Accessibilité
- The region is a landmark named by
label; toasts are announced politely. - Toasts pause while hovered or focused.
Clavier
| Touches | Action |
|---|---|
| F8 | Moves the focus to the toasts region (sonner's hotkey). |
| Tab / Enter | Reach and press a toast's action or close button. |
Bonnes pratiques
- Mount one
Toasterin the root layout, with itslabelandcloseLabeltranslated. - A title in a few words, a description only if it adds something.
- Never put the only way to do something in a toast: it goes away.
Composants liés
Générée depuis le code de @krizaka/ui 2.0.0-beta.6 : son meta.ts, ses exemples et ses types.Modifier cette documentation sur GitHub