A toast notification is a small popup that appears at either side of the screen, and disappears after a short time. The toast notification is used to notify the user of something that has happened, but it is not intended to be used as a permanent message.
Import
import{
Toast,
ToastProvider,
useToast,
}from'@trendmicro/react-styled-ui';
Usage
Add ToastProvider to the root.
<ToastProviderplacement="bottom-right">
<App/>
</ToastProvider>
For the Hook version, use the useToast Hook to get all the methods, properties, and state. See the useToast Hook section for detailed usage.
functionExample(){
const toast =useToast();
consthandleClick=()=>{
toast(({ onClose, placement })=>(
<Box>toast</Box>
));
};
return(
<ButtononClick={handleClick}>
Trigger Toast
</Button>
);
}
Layout
Increase the vertical padding space to 4x (or 1rem) for a multiline paragraph.
Set the minimum horizontal margin space to 4x (or 1rem) before the close button.
Set the minimum horizontal margin space to 4x (or 1rem) between the icon and the content.
Apply vertical margin space with 2x (or .5rem) between the title and its content.
Apply vertical margin space with 6x (or 1.5rem) between the end of the content and the action button (or action link).
If true, a close button will appear on the right side.
onClose
function
A callback called when the close button is clicked.
appearance
string
'none'
One of: 'none', 'success', 'info', 'warning', 'error'
icon
string | ReactNode | false
Override the icon displayed before the children. Unless provided, the icon is mapped to the value of the appearance prop.
ToastProvider
Name
Type
Default
Description
children
ReactNode
container
DOM element
placement
string
'bottom-right'
Set the default placement to place toasts. One of: 'top', 'top-right', 'top-left', 'bottom', 'bottom-left', 'bottom-right'
useToast Hook
The useToast hook has the following signature:
const{
// Methods
close,
closeAll,
find,
findIndex,
notify,
update,
// Properties
placement,
// State
state,
}=useToast();
toast.close(id)
Close a toast at its placement.
Arguments
id(string): The id to close the toast.
Returns
This method returns undefined.
toast.closeAll([options={}])
Close all toasts at once with the given placements, including top, top-left, top-right, bottom, bottom-left, bottom-right.
Arguments
[options={}](Object): The options object.
[options.placements=[]](Array): An array of placements to close toasts.
Returns
This method returns undefined.
toast.find(id)
Find the first toast in the array that matches the provided toast id. Otherwise, undefined is returned if not found.
If no values satisfy the testing function, undefined is returned.
Arguments
id(string): The id to find the toast.
Returns
(Object): Returns the toast object.
toast.findIndex(id)
Find the first toast in the array that matches the provided toast id. Otherwise, -1 is returned if not found.
Arguments
id(string): The id to find the toast.
Returns
(number): Returns the array index.
toast.notify(message, [options={}])
Create a toast at the specified placement and return the toast id.
Arguments
message(Function|string): The toast message to render.
[options={}](Object): The options object.
[options.duration=null](number): The duration (in milliseconds) that the toast should remain on the screen. If set to null, toast will never dismiss.
[options.id](string): A unique ID of the toast.
[options.placement](string): The placement of the toast.
Aliases
toast(message, [options={}])
Returns
(string): Returns the toast id.
toast.update(id, [options={}])
Update a specific toast with new options based on the given toast id.
Arguments
id(string): The id to update the toast.
[options={}](Object): The options object.
[options.duration=null](number): The duration (in milliseconds) that the toast should remain on the screen. If set to null, toast will never dismiss.
[options.message](Function|string): The toast message to render.
Returns
Returns true if the toast exists, else false.
toast.placement
Specify the placement to place the toast. The default placement will be used if the placement option is not explicitly specified.
toast.state
The toast state is a placement object, each placement contains an array of objects representing the current toasts.
{
'top':[
{
id:'1',// A unique identifier that represents the toast message
message:({ id, onClose, placement })=><Toast/>,// The toast message to render
placement:'top',// The placement of the toast
duration:null,// The duration (in milliseconds) that the toast should remain on the screen. If set to null, toast will never dismiss.
onClose:()=> toast.close(id, placement),// The function to close the toast