Toasts are used to show alerts on top of an overlay. The toast will close
itself when the close button is clicked, or after a timeout—the default is 5
seconds.
Toasts are usually used to give feedback to users after an action
has taken place.
By default the toast will be shown for 5 seconds, but we can change it by
passing the duration prop.
live
() => {
const toast = useToast();
return (
<Stack direction="row" spacing="400">
<Button
onClick={() =>
toast({
description: 'I will be shown for 2 seconds',
duration: 2000,
})
}
>
⏱ 2 seconds
</Button>
<Button
onClick={() =>
toast({
description: 'I will be shown for 10 seconds',
duration: 10000,
})
}
>
⏱ 10 seconds
</Button>
</Stack>
);
};
States
A toast can be in a loading or error state.
live
() => {
const toast = useToast();
return (
<Stack direction="row" spacing="400">
<Button
onClick={() =>
toast({
description: 'I am loading',
status: 'loading',
props: {
action: { callback: () => {}, label: 'Loading' },
},
})
}
>
⏱ Loading...
</Button>
<Button
onClick={() =>
toast({
description: 'I have an error',
status: 'error',
})
}
>
❌ Error
</Button>
</Stack>
);
};
On close complete
A function can run when the toast is closed.
live
() => {
const toast = useToast();
return (
<Button
onClick={() =>
toast({
description: 'Close me!',
onCloseComplete: () => {
alert('toast closed!');
},
})
}
>
Click me
</Button>
);
};
Custom Content
To display custom content instead of the default description message and
dismiss or action button, use the content variant and pass the custom content
as the children prop.
The content variant will display an icon-based close button in the top right
of the toast container.
Warning
The content variant is only supported in the Bauhaus theme. Pass a custom
render function to render custom content in the legacy theme.