Bootstrap React Toast
Push notifications to your visitors with a Bootstrap React 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="Bootstrap React" autohide={false} visible={true}>2 <CToastHeader close>3 <svg4 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">Bootstrap React</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="Bootstrap React">5 <CToastHeader close>6 <svg7 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">Bootstrap React</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="Bootstrap React" autohide={false} visible={true}>2 <CToastHeader close>3 <svg4 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">Bootstrap React</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="Bootstrap React" autohide={false} visible={true}>3 <CToastHeader close>4 <svg5 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">Bootstrap React</strong>16 <small>7 min ago</small>17 </CToastHeader>18 <CToastBody>Hello, world! This is a toast message.</CToastBody>19 </CToast>20 <CToast title="Bootstrap React" autohide={false} visible={true}>21 <CToastHeader close>22 <svg23 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">Bootstrap React</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 action7 </CButton>8 <CToastClose component={CButton} color="secondary" size="sm" className="ms-1">9 Close10 </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/bootstrap-react'2// or3import CToast from '@coreui/bootstrap-react/src/components/toast/CToast'
Property | Description | Type | Default |
---|---|---|---|
animation | Apply a CSS fade transition to the toast. | boolean | true |
autohide | Auto hide the toast. | boolean | true |
className | A string of all className you want applied to the base component. | string | - |
color | Sets the color context of the component to one of Bootstrap React’s themed colors. | 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string | - |
delay | Delay hiding the toast (ms). | number | 5000 |
onClose | Callback fired when the component requests to be closed. | (index: number) => void | - |
onShow | Callback fired when the component requests to be shown. | (index: number) => void | - |
visible | Toggle the visibility of component. | boolean | false |
CToastHeader#
1import { CToastHeader } from '@coreui/bootstrap-react'2// or3import CToastHeader from '@coreui/bootstrap-react/src/components/toast/CToastHeader'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
closeButton | Automatically add a close button to the header. | boolean | - |
CToastBody#
1import { CToastBody } from '@coreui/bootstrap-react'2// or3import CToastBody from '@coreui/bootstrap-react/src/components/toast/CToastBody'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
CToastClose#
1import { CToastClose } from '@coreui/bootstrap-react'2// or3import CToastClose from '@coreui/bootstrap-react/src/components/toast/CToastClose'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
component | Component used for the root node. Either a string to use a HTML element or a component. | string | ComponentClass<any, any> | FunctionComponent<any> | - |
disabled | Toggle the disabled state for the component. | boolean | - |
white | Change the default color to white. | boolean | - |
CToaster#
1import { CToaster } from '@coreui/bootstrap-react'2// or3import CToaster from '@coreui/bootstrap-react/src/components/toast/CToaster'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
placement | Describes the placement of your component. | 'top-start' | 'top' | 'top-end' | 'middle-start' | 'middle' | 'middle-end' | 'bottom-start' | 'bottom' | 'bottom-end' | string | - |
push | Adds new CToast to CToaster . | ReactElement | - |