Display an important message globally. A toast is not a component you render: call setToast from useToasts anywhere under BolioUIProvider, and the provider shows it. It disappears after delay, and stays while the pointer is over it.
Basic usage. The toast closes after 2 seconds, or after delay milliseconds.
Use type to tell a result from a problem. An error toast is announced right away by a screen reader, the others wait for a pause.
Display multiline or overlong text.
Add a button with actions. Its handler receives the click and a cancel function that closes the toast. Actions show only when text is a string.
Use passive to change the style of the button, for an action that only dismisses.
Show more information by replacing the string with a ReactNode. The content replaces the whole toast, so put any button or link inside it.
Pass a layout to useToasts to choose where toasts appear. The layout applies when the component mounts, so it is set once, not on each toast.
Give a toast an id to close it later with removeToastOneByID, or use removeAll to close every toast. Setting a toast with an id that is still shown throws an error, so check with findToastOneByID first.
const {
// All Toasts currently in DOM
toasts: Array<Toast>
// Set a displayable Toast
setToast: (toast: ToastInput) => void
// Hide all Toast immediately
removeAll: () => void
// Query by id to get an instance of Toast
findToastOneByID: (ident: string) => Toast | undefined
// Remove the specified Toast by id
removeToastOneByID: (ident: string) => void
} = useToasts(layout: ToastLayout)
| Option | Description | Type | Default |
|---|---|---|---|
| padding | space inside each toast | string | 12px 16px |
| margin | space around each toast | string | 8px 0 |
| width | width of each toast | string | 420px |
| maxWidth | largest width of a toast | string | 90vw |
| maxHeight | largest height of a toast | string | 75px |
| placement | the pop-up position of toast | ToastPlacement | bottomRight |
| Option | Description | Type | Default |
|---|---|---|---|
| id | name to find or close the toast. It must not match a toast that is still shown | string | generated |
| text | content displayed in toast | string, ReactNode | - |
| type | the type of toast | ToastTypes | default |
| delay | milliseconds before it closes. 0 uses the default | number | 2000 |
| actions | buttons shown after the text. Only with a string text | ToastAction[] | - |
type ToastPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight'
interface ToastAction {
name: string
handler: (
event: React.MouseEvent<HTMLButtonElement>,
cancel: () => void
) => void
passive?: boolean
}
type ToastTypes =
| 'default'
| 'primary'
| 'secondary'
| 'success'
| 'warning'
| 'error'
| 'info'