Toast
A toast is a passive, non-modal notification that informs the user of changes or updates as a result of an action.
#Basic usage
A Toast
is displayed at the top of the page and remains there until it is dismissed by the user.
There are four types of Toast
: success, info, warning, and error. Using different types of Toast
helps users understand what type of information is being communicated and how they should respond.
Success
Let the user know their action was completed as expected. If the user just created something, allow them to view it. For example, “Policy template updated.” or “Email template updated.” Follow the writing guideline for Progress and congratulations.
Information
Provide the user with neutral and additional information. Make sure Toast has a clear purpose and supports (not impedes) the user. Be brief and provide only the information relevant to the task. For example, “The initial crawlers may take a few days to complete. Read scan process”. Follow the writing guideline for Help content for complex workflows.
Warning
Make the user aware of a condition or potential problem with a warning message. For example, “Renew your subscription” Avoid writing something that has already occurred. Instead, use an error toast. Follow the writing guideline for Errors and warnings.
Error
Inform the user of an error or failure and possibly prevent the user from continuing until the problem is resolved. Point out the exact location of the problem - where the user should go and what steps are required to resolve the problem. For example, “Cannot add the file. The file already existed. Please upload again.” Follow the writing guideline for Issues and issue descriptions.
const showToast = useShowToast();
return (
<>
<button
aria-haspopup="dialog"
onClick={() => showToast({ message: "Success", type: "success" })}
>
Success
</button>
<button aria-haspopup="dialog" onClick={() => showToast({ message: "Info", type: "info" })}>
Info
</button>
<button
aria-haspopup="dialog"
onClick={() => showToast({ message: "Warning", type: "warning" })}
>
Warning
</button>
<button aria-haspopup="dialog" onClick={() => showToast({ message: "Error", type: "error" })}>
Error
</button>
</>
);
#Clear toasts
useClearToast
is not a feature for end users. The user can clear the Toast
by clicking the close button, or pressing escape. The Toast
can also be cleared programmatically if you i.e. have a single-page application, where you want to clear the Toast
on page navigation. To clear the Toast
from anywhere use the useClearToast
hook from anywhere in your code. Could be your routing framework, or in this case, an arbitrary button.
const clearToast = useClearToast();
return <button onClick={clearToast}>Clear toasts programatically</button>;
#Examples
To provide users with a consistent experience, the Toast
type has a corresponding color and icon. By default, Toast
contains an icon, inline text, an optional link, and a close button.
- Icon: the colour and icon give the user a visual indication of the message at a glance.
- Inline text: provide short and clear text that directly relates to the task at hand. See the writing guideline below.
- Link: an optional link that takes the user to a page with more details or an action that the user can perform.
- Close button: closes the toast.
<Toast type="success" message="Some success message" />
<Toast type="info" message="Some neutral message" />
<Toast type="warning" message="Some warning message" />
<Toast type="error" message="Some error message" />
#Examples with links
A Toast
with a link allows the user to interact with the content and perform an action through a shortcut. This link should be short and navigate the user to a page or modal where the user can take action to resolve the issue or find more information. See Link guideline. Use a Toast
with a link as follows:
- Use only one link per toast.
- Be clear about what action the user can take.
- A link should only be a shortcut for the user.
- Make sure that the user can reach the link in the message in other ways.
Support Undo
Remember that the user often takes an action by mistake. Allow the user to correct mistakes by displaying an “Undo” action. This promotes a sense of freedom and confidence for the user.
<Toast
type="success"
message={
<>
Some success message with a <a href="#examples-with-links">link</a>
</>
}
/>
#Examples with external html
If the user will open a new tab or window with the link, use the style to match the user behavior. Follow Example with links.
<Toast
type="success"
message={
<ExternalHtml>
{"Some success message with a <a href='#examples-with-external-html'>link</a>"}
</ExternalHtml>
}
/>
#Toast dropzone
ToastDropzone
is not a feature for end users. It is a structural component that allows developers to control where the Toast
component will appear in the Document Object Model (DOM).
By default toasts are appended to the DOM in the bottom of <body>. ToastDropzone lets you to control where in the DOM the Toasts are rendered, allowing it to inherit styles, if you don't use a global stylesheet.
<ToastDropzone />
#Properties
Property | Description | Defined | Value |
---|---|---|---|
typeRequired | "error" | "info" | "success" | "warning" | ||
messageOptional | element | ||
onCloseOptional | function | ||
aria-labelOptional | string | ||
childrenOptional | element |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications