Skip to main content

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

Live Editor
Result
Loading...

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.

Live Editor
Result
Loading...

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:

Live Editor
Result
Loading...

and with custom toastId:

Live Editor
Result
Loading...

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.

Live Editor
Result
Loading...

Props

The object structure for individual Toasts passed into showToast:

NameTypeDefault
title *
A short headline
string
offsetTop
Adjust the distance to the top of the screen
number
16
children
Children of the Toast Provider
ReactNode
message
Optional prop to show more info underneath the title
string
position
Optional prop for Toast position
'top-right' | 'top-center' | 'top-left' | 'bottom-right' | 'bottom-center' | 'bottom-left'
"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
false | number
5000
closeButton
Optional prop to show close button
boolean
false
delay
Optional prop to have delay in ms before showing Toast
number
action
Optional action button to be shown in the Toast
{ label: string; onClick: () => void; dataTestId?: string; }
onClose
A close callback if the Toast is closed by icon
() => void
* - the prop is required.