[ Feedback ]
Toast
Short, temporary notifications that appear in a corner of the screen. Wrap your app once in ToastProvider, then call the toast() function from useToast() anywhere. Toasts pause while hovered and dismiss themselves.
Import#
import { ToastProvider, useToast } from 'reusify-ui';When to use#
- Confirming an action the user just took: "Saved", "Copied", "Invite sent".
- Non-blocking background events: "Upload complete".
- Not for critical errors the user must act on. Use an Alert or Modal for those.
Examples#
Setup#
Add the provider once, near the root of your app. position, duration and max are configured here.
This docs site is already wrapped in a ToastProvider. Try the examples below.
import { ToastProvider } from 'reusify-ui';
export default function Root() {
return (
<ToastProvider position="bottom-right" duration={4000} max={5}>
<App />
</ToastProvider>
);
}Variants#
Call toast() with a string or an options object, or use the success / error / warning / info shortcuts.
const toast = useToast();
toast('Event has been created');
toast.success('Profile saved', { description: 'Your changes are live.' });
toast.error('Upload failed', { description: 'File is larger than 10 MB.' });
toast.warning('Low balance');
toast.info('New update available');Appearances & animations#
Click a spot on the mini screen to fire a toast there, then mix an appearance, an animation and a progress bar. slide comes in from the nearest edge. With accent, the whole border is the progress bar and drains around the toast. Hover a toast to pause it. Set any of these per toast, or as defaults on ToastProvider.
toast.success('Changes saved', {
description: 'Your profile is up to date.',
appearance: 'soft',
animation: 'pop',
position: 'bottom-right',
progress: true,
});<ToastProvider appearance="soft" animation="pop" progress>
<App />
</ToastProvider>
// …or per toast:
toast.success('Saved', { appearance: 'glass', animation: 'bounce', position: 'top-center' });With an action (Undo)#
toast() returns an id. Use it with toast.dismiss(id) to close the toast from your action.
const id = toast({
title: 'Message deleted',
duration: 6000,
action: (
<Button size="sm" variant="secondary" onClick={() => {
toast.dismiss(id);
restoreMessage();
}}>
Undo
</Button>
),
});Persistent & updating#
duration: Infinity keeps a toast open. Calling toast again with the same id replaces it, which is handy for progress → done. toast.dismiss() with no id clears everything.
toast.info('Uploading 3 files…', { id: 'upload', duration: Infinity });
// later…
toast.success('Upload complete', { id: 'upload' });
toast.dismiss(); // close allAPI reference#
<ToastProvider />
| Prop | Type | Default | Description |
|---|---|---|---|
position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 'bottom-right' | Where toasts stack. |
duration | number | 4000 | Default auto-dismiss time in ms. |
max | number | 5 | Maximum visible toasts; the oldest are dropped. |
appearance | 'default' | 'soft' | 'solid' | 'accent' | 'glass' | 'pill' | 'default' | Default look for every toast. |
animation | 'slide' | 'pop' | 'bounce' | 'flip' | 'blur' | 'fade' | 'slide' | Default enter animation. |
progress | boolean | false | Show time left: a bar along the bottom, or the whole border with accent. |
pattern | boolean | 'dots' | 'grid' | 'lines' | 'diagonal' | 'checker' | 'cross' | 'waves' | false | A pattern in the top left corner, tinted to the toast color. true means dots. Set on the provider or per toast. |
position'bottom-right''top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'Where toasts stack.
duration4000numberDefault auto-dismiss time in ms.
max5numberMaximum visible toasts; the oldest are dropped.
appearance'default''default' | 'soft' | 'solid' | 'accent' | 'glass' | 'pill'Default look for every toast.
animation'slide''slide' | 'pop' | 'bounce' | 'flip' | 'blur' | 'fade'Default enter animation.
progressfalsebooleanShow time left: a bar along the bottom, or the whole border with accent.
patternfalseboolean | 'dots' | 'grid' | 'lines' | 'diagonal' | 'checker' | 'cross' | 'waves'A pattern in the top left corner, tinted to the toast color. true means dots. Set on the provider or per toast.
toast(options)
Pass a string as a shortcut for { title }. Returns the toast id. Shortcuts toast.success / error / warning / info(title, options) set the variant for you.
| Prop | Type | Default | Description |
|---|---|---|---|
title | ReactNode | - | Main line. |
description | ReactNode | - | Secondary text. |
variant | 'default' | 'success' | 'danger' | 'warning' | 'info' | 'default' | Icon and color. |
duration | number | - | Override the provider duration. Infinity keeps it open. |
action | ReactNode | - | Extra content such as an Undo button. |
position | string | - | Overrides the provider position. |
appearance | string | - | Overrides the provider appearance. |
animation | string | - | Overrides the provider animation. |
progress | boolean | - | Overrides the provider progress bar. |
id | string | - | Reuse an id to replace an existing toast. |
title-ReactNodeMain line.
description-ReactNodeSecondary text.
variant'default''default' | 'success' | 'danger' | 'warning' | 'info'Icon and color.
duration-numberOverride the provider duration. Infinity keeps it open.
action-ReactNodeExtra content such as an Undo button.
position-stringOverrides the provider position.
appearance-stringOverrides the provider appearance.
animation-stringOverrides the provider animation.
progress-booleanOverrides the provider progress bar.
id-stringReuse an id to replace an existing toast.
toast.dismiss(id?)
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | - | Toast to close. Omit it to close all toasts. |
id-stringToast to close. Omit it to close all toasts.
Accessibility#
- Toasts render in an aria-live="polite" region, so screen readers announce them without interrupting.
- Hovering pauses the timer, so slower readers have time to read.
- Every toast has a dismiss button labelled "Dismiss notification".