Toast
Toasts display important information that informs the user about the consequence of an action – e.g. "Chart deleted". They are semantically categorized and colored to reflect the status.
Example
Usage
To set up the Toast component you need to wrap your component tree with the ToastProvider
.
// App.js
import { ToastProvider } from '@adjust/components';
export default function() {
return (
<ToastProvider>
{...}
</ToastProvider>
);
}
Through the ToastProvider
your components will have access to a helper function for showing Toast items.
Access this function via ToastContext
like this:
// MyComponent.js
import { useContext } from 'react';
import { ToastContext, Button } from '@adjust/components';
export default function YourComponent() {
const { showToast } = useContext(ToastContext);
return (
<Button
label="Show Toast"
onClick={() => {
showToast({
title: 'Item deleted',
action: { label: 'Undo', onClick: () => {} }
});
}}
/>
);
}
The showToast
function accepts a title, an optional message, an optional action, an optional delay, an optional autoClose, an optional closeButton and an optional severity (type
).
Variants
Close Callback
onClose
is a callback which is invoked if the Toast is closed by icon.
Dismiss Toast
To dismiss a Toast you need to provide the toastId to the dismissToast
handler. Each toast has a unique toastId by default which will be returned in showToast
handler. You can also set a custom id to each toast.
Please note that in case no id is provided for dismissToast handler it will dismiss all the toasts.
You can see both approaches in these examples:
and with custom toastId:
Type
The type
prop lets you set the severity of toast messages and give it a semantic meaning.
Four different options are available: neutral
, positive
, warning
and negative
.
Props
The object structure for individual Toasts passed into showToast
:
Name | Type | Default |
---|---|---|
title * A short headline |
| — |
offsetTop Adjust the distance to the top of the screen |
| 16 |
children Children of the Toast Provider |
| — |
message Optional prop to show more info underneath the title |
| — |
position Optional prop for Toast position |
| "top-right" |
autoClose Optional prop to set delays in ms to close the toast. If set to false, the notification needs to be closed manually |
| 5000 |
closeButton Optional prop to show close button |
| false |
delay Optional prop to have delay in ms before showing Toast |
| — |
action Optional action button to be shown in the Toast |
| — |
onClose A close callback if the Toast is closed by icon |
| — |
* - the prop is required. |