A configurable, composable, toast notification system for react.
Originally fork from https://jossmac.github.io/react-toast-notifications
Add the below line to your dependencies in package.json
"react-toast": "git://github.com/taskworld/react-toast.git"
Wrap your app in the ToastProvider, which provides context for the Toast descendants.
import { ToastProvider, useToasts, DefaultToast, DefaultToastContainer } from 'react-toast';
const FormWithToasts = () => {
const { addToast } = useToasts();
const onSubmit = async value => {
const { error } = await dataPersistenceLayer(value);
if (error) {
addToast(error.message, { appearance: 'error' });
} else {
addToast('Saved Successfully', { appearance: 'success' });
}
};
return <form onSubmit={onSubmit}>...</form>;
};
const App = () => (
<ToastProvider components={{ Toast: DefaultToast, ToastContainer: DefaultToastContainer }}>
<FormWithToasts />
</ToastProvider>
);For brevity:
PlacementTypeis equal to'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right'.TransitionStateis equal to'entering' | 'entered' | 'exiting' | 'exited'.
| Property | Description |
|---|---|
autoDismissTimeout number |
Default 5000. The time until a toast will be dismissed automatically, in milliseconds. |
autoDismiss boolean |
Default: false. Whether or not to dismiss the toast automatically after a timeout. |
children ReactNode |
Required. Your app content. |
components { ToastContainer, Toast } |
The underlying components. |
newestOnTop boolean |
Default false. When true, insert new toasts at the top of the stack. |
placement PlacementType |
Default top-right. Where, in relation to the viewport, to place the toasts. |
portalTargetSelector string |
Which element to attach the container's portal to. Uses document.body when not provided. |
transitionDuration number |
Default 220. The duration of the CSS transition on the Toast component. |
| Property | Description |
|---|---|
| appearance | Used by the default toast. One of success, error, warning, info. |
| children | Required. The content of the toast notification. |
autoDismiss boolean |
Inherited from ToastProvider if not provided. |
autoDismissTimeout number |
Inherited from ToastProvider. |
onDismiss: Id => void |
Passed in dynamically. Can be called in a custom toast to dismiss it. |
placement PlacementType |
Inherited from ToastProvider. |
transitionDuration number |
Inherited from ToastProvider. |
transitionState: TransitionState |
Passed in dynamically. |
The useToast hook has the following signature:
const {
addToast,
removeToast,
removeAllToasts,
updateToast,
toastStack,
} = useToasts();The addToast method has three arguments:
- The first is the content of the toast, which can be any renderable
ReactNode. - The second is the
Optionsobject, which can take any shape you like.Options.appearanceis required when using theDefaultToast. When departing from the default shape, you must provide an alternative, compliantToastcomponent. - The third is an optional callback, which is passed the added toast
ID.
The removeToast method has two arguments:
- The first is the
IDof the toast to remove. - The second is an optional callback.
The removeAllToasts method has no arguments.
The updateToast method has three arguments:
- The first is the
IDof the toast to update. - The second is the
Optionsobject, which differs slightly from the add method because it accepts acontentproperty. - The third is an optional callback, which is passed the updated toast
ID.
The toastStack is an array of objects representing the current toasts, e.g.
[
{
content: 'Something went wrong',
id: 'generated-string',
appearance: 'error',
},
{ content: 'Item saved', id: 'generated-string', appearance: 'success' },
];