React Toast Component

Push notifications to your visitors with a toast, a lightweight and easily customizable alert message.

React toasts are lightweight notifications designed to mimic the push notifications that have been popularized by mobile and desktop operating systems. They’re built with flexbox, so they’re easy to align and position.

Overview#

Things to know when using the toast plugin:

  • Toasts are opt-in for performance reasons, so you must initialize them yourself.
  • Toasts will automatically hide if you do not specify autohide: false.

Examples#

Basic#

To encourage extensible and predictable toasts, we recommend a header and body. Toast headers use display: flex, allowing easy alignment of content thanks to our margin and flexbox utilities.

Toasts are as flexible as you need and have very little required markup. At a minimum, we require a single element to contain your "toasted" content and strongly encourage a dismiss button.

1<CToast title="CoreUI for React.js" autohide={false} visible={true}>
2 <CToastHeader close>
3 <svg
4 className="rounded me-2"
5 width="20"
6 height="20"
7 xmlns="http://www.w3.org/2000/svg"
8 preserveAspectRatio="xMidYMid slice"
9 focusable="false"
10 role="img"
11 >
12 <rect width="100%" height="100%" fill="#007aff"></rect>
13 </svg>
14 <strong className="me-auto">CoreUI for React.js</strong>
15 <small>7 min ago</small>
16 </CToastHeader>
17 <CToastBody>Hello, world! This is a toast message.</CToastBody>
18</CToast>
1const [toast, addToast] = useState(0)
2const toaster = useRef()
3const exampleToast = (
4 <CToast title="CoreUI for React.js">
5 <CToastHeader close>
6 <svg
7 className="rounded me-2"
8 width="20"
9 height="20"
10 xmlns="http://www.w3.org/2000/svg"
11 preserveAspectRatio="xMidYMid slice"
12 focusable="false"
13 role="img"
14 >
15 <rect width="100%" height="100%" fill="#007aff"></rect>
16 </svg>
17 <strong className="me-auto">CoreUI for React.js</strong>
18 <small>7 min ago</small>
19 </CToastHeader>
20 <CToastBody>Hello, world! This is a toast message.</CToastBody>
21 </CToast>
22)
23return (
24 <>
25 <CButton onClick={() => addToast(exampleToast)}>Send a toast</CButton>
26 <CToaster ref={toaster} push={toast} placement="top-end" />
27 </>
28)

Translucent#

Toasts are slightly translucent to blend in with what's below them.

1<CToast title="CoreUI for React.js" autohide={false} visible={true}>
2 <CToastHeader close>
3 <svg
4 className="rounded me-2"
5 width="20"
6 height="20"
7 xmlns="http://www.w3.org/2000/svg"
8 preserveAspectRatio="xMidYMid slice"
9 focusable="false"
10 role="img"
11 >
12 <rect width="100%" height="100%" fill="#007aff"></rect>
13 </svg>
14 <strong className="me-auto">CoreUI for React.js</strong>
15 <small>7 min ago</small>
16 </CToastHeader>
17 <CToastBody>Hello, world! This is a toast message.</CToastBody>
18</CToast>

Stacking#

You can stack toasts by wrapping them in a toast container, which will vertically add some spacing.

1<CToaster>
2 <CToast title="CoreUI for React.js" autohide={false} visible={true}>
3 <CToastHeader close>
4 <svg
5 className="rounded me-2"
6 width="20"
7 height="20"
8 xmlns="http://www.w3.org/2000/svg"
9 preserveAspectRatio="xMidYMid slice"
10 focusable="false"
11 role="img"
12 >
13 <rect width="100%" height="100%" fill="#007aff"></rect>
14 </svg>
15 <strong className="me-auto">CoreUI for React.js</strong>
16 <small>7 min ago</small>
17 </CToastHeader>
18 <CToastBody>Hello, world! This is a toast message.</CToastBody>
19 </CToast>
20 <CToast title="CoreUI for React.js" autohide={false} visible={true}>
21 <CToastHeader close>
22 <svg
23 className="rounded me-2"
24 width="20"
25 height="20"
26 xmlns="http://www.w3.org/2000/svg"
27 preserveAspectRatio="xMidYMid slice"
28 focusable="false"
29 role="img"
30 >
31 <rect width="100%" height="100%" fill="#007aff"></rect>
32 </svg>
33 <strong className="me-auto">CoreUI for React.js</strong>
34 <small>7 min ago</small>
35 </CToastHeader>
36 <CToastBody>Hello, world! This is a toast message.</CToastBody>
37 </CToast>
38</CToaster>

Custom content#

Customize your toasts by removing sub-components, tweaking them with utilities, or by adding your own markup. Here we've created a simpler toast by removing the default <CToastHeader>, adding a custom hide icon from CoreUI Icons, and using some flexbox utilities to adjust the layout.

1<CToast autohide={false} visible={true} className="align-items-center">
2 <div className="d-flex">
3 <CToastBody>Hello, world! This is a toast message.</CToastBody>
4 <CToastClose className="me-2 m-auto" />
5 </div>
6</CToast>

Alternatively, you can also add additional controls and components to toasts.

1<CToast autohide={false} visible={true}>
2 <CToastBody>
3 Hello, world! This is a toast message.
4 <div className="mt-2 pt-2 border-top">
5 <CButton type="button" color="primary" size="sm">
6 Take action
7 </CButton>
8 <CToastClose component={CButton} color="secondary" size="sm" className="ms-1">
9 Close
10 </CToastClose>
11 </div>
12 </CToastBody>
13</CToast>

Color schemes#

Building on the above example, you can create different toast color schemes with our color and background utilities. Here we've set color="primary" and added .text-white class to the <Ctoast>, and then set white property to our close button. For a crisp edge, we remove the default border with .border-0.

1<CToast autohide={false} visible={true} color="primary" className="text-white align-items-center">
2 <div className="d-flex">
3 <CToastBody>Hello, world! This is a toast message.</CToastBody>
4 <CToastClose className="me-2 m-auto" white />
5 </div>
6</CToast>

API#

CToast#

1import { CToast } from '@coreui/react'
2// or
3import CToast from '@coreui/react/src/components/toast/CToast'
PropertyDescriptionTypeDefault
animationApply a CSS fade transition to the toast.booleantrue
autohideAuto hide the toast.booleantrue
classNameA string of all className you want applied to the base component.string-
colorSets the color context of the component to one of CoreUI’s themed colors.'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string-
delayDelay hiding the toast (ms).number5000
onCloseCallback fired when the component requests to be closed.(index: number) => void-
onShowCallback fired when the component requests to be shown.(index: number) => void-
visibleToggle the visibility of component.booleanfalse

CToastHeader#

1import { CToastHeader } from '@coreui/react'
2// or
3import CToastHeader from '@coreui/react/src/components/toast/CToastHeader'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the base component.string-
closeButtonAutomatically add a close button to the header.boolean-

CToastBody#

1import { CToastBody } from '@coreui/react'
2// or
3import CToastBody from '@coreui/react/src/components/toast/CToastBody'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the base component.string-

CToastClose#

1import { CToastClose } from '@coreui/react'
2// or
3import CToastClose from '@coreui/react/src/components/toast/CToastClose'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the base component.string-
componentComponent used for the root node. Either a string to use a HTML element or a component.string | ComponentClass<any, any> | FunctionComponent<any>-
disabledToggle the disabled state for the component.boolean-
whiteChange the default color to white.boolean-

CToaster#

1import { CToaster } from '@coreui/react'
2// or
3import CToaster from '@coreui/react/src/components/toast/CToaster'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the base component.string-
placementDescribes the placement of your component.'top-start' | 'top' | 'top-end' | 'middle-start' | 'middle' | 'middle-end' | 'bottom-start' | 'bottom' | 'bottom-end' | string-
pushAdds new CToast to CToaster.ReactElement-