WUI logo

Toast

The Toast component is a transient, non-blocking notification that appears briefly to provide feedback to the user. It is typically used to display messages such as success, error, warning, or informational alerts. Toasts appear at a designated area of the screen, often at the bottom or top, and automatically disappear after a few seconds, ensuring a smooth and unobtrusive user experience.

import * as React from 'react'
import { toast } from '@welcome-ui/toast'
import { Box } from '@welcome-ui/box'
import { Button } from '@welcome-ui/button'
const Element = () => (
<Box
backgroundColor="light-900"
borderColor="nude-300"
borderRadius="sm"
borderStyle="solid"
borderWidth="1px"
color="dark-900"
padding="sm"
>
Lorem ipsum dolor sit amet
</Box>
)
const Example = () => {
return <Button onClick={() => toast(<Element />)}>Show Toast</Button>
}
export default Example

Installation

1

Run the following command:

yarn add @welcome-ui/toast
2

Import component:

import { Notifications, Toast, toast } from '@welcome-ui/toast'

Notification component with react-hot-toast and 2 components:

  • Growl
  • Snackbar

Notifications

Before using the Toast component, you must add the Notifications component in your project root. This component will contain all your toasts.

function App() {
return <Notifications pauseOnHover={false} />
}

You can pass a boolean value to pauseOnHover prop (default to true) to pause duration of all notifications on mouse hover.

toast

Use toast function to show the toast element. This function returns a unique identifier that can be used to update or remove the toast later.

Options

The toast function takes a component to be displayed and an options object:

  • position: one of top-left, top-center, top-right, bottom-left, bottom-center or bottom-right
  • duration: before closing the element, in ms, with a default 7000. It can also be set to null to prevent your element from closing automatically.
  • id: a unique identifier for the toast that can be used to update or remove it later and prevent duplicates

Position

Supplied components

You can create your own notification component to display in the Toast or one of our default components: Snackbar and Growl.

In general:

  • Snackbar is used to display a temporary, information-only notification, and
  • Growl is used to display a more important notification with an action (that requires the user to close the notification)

Snackbar

At bottom center by default.

Growl

At top right by default.

Custom close function

You can add your custom function when user click on close button

Without close button on Growl or Snackbar

You can choose not to show a close button on Growl or Snackbar with the hasCloseButton prop (defaults to true).

Close or dismiss a toast

You can close or dismiss a toast programmatically with the dismiss and remove functions. Dismiss will hide the toast with a fade out animation, while remove will hide the toast immediately.

const toastId = toast(<Notification />)
// Remove or dismiss this specific toast
remove(toastId)
dismiss(toastId)
// Remove or dismiss all toasts
remove()
dismiss()