React Toast Component

Toast

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

Other frameworks

CoreUI components are available as native Angular, Bootstrap (Vanilla JS), and Vue components. To learn more please visit the following pages.

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.

<CToast animation={false} autohide={false} visible={true}>
<CToastHeader closeButton>
<svg
className="rounded me-2"
width="20"
height="20"
xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="xMidYMid slice"
focusable="false"
role="img"
>
<rect width="100%" height="100%" fill="#007aff"></rect>
</svg>
<div className="fw-bold me-auto">CoreUI for React.js</div>
<small>7 min ago</small>
</CToastHeader>
<CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>
const [toast, addToast] = useState(0)
const toaster = useRef()
const exampleToast = (
<CToast>
<CToastHeader closeButton>
<svg
className="rounded me-2"
width="20"
height="20"
xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="xMidYMid slice"
focusable="false"
role="img"
>
<rect width="100%" height="100%" fill="#007aff"></rect>
</svg>
<div className="fw-bold me-auto">CoreUI for React.js</div>
<small>7 min ago</small>
</CToastHeader>
<CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>
)
return (
<>
<CButton color="primary" onClick={() => addToast(exampleToast)}>Send a toast</CButton>
<CToaster className="p-3" placement="top-end" push={toast} ref={toaster} />
</>
)

Translucent#

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

<CToast autohide={false} visible={true}>
<CToastHeader closeButton>
<svg
className="rounded me-2"
width="20"
height="20"
xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="xMidYMid slice"
focusable="false"
role="img"
>
<rect width="100%" height="100%" fill="#007aff"></rect>
</svg>
<div className="fw-bold me-auto">CoreUI for React.js</div>
<small>7 min ago</small>
</CToastHeader>
<CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>

Stacking#

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

<CToaster className="position-static">
<CToast autohide={false} visible={true}>
<CToastHeader closeButton>
<svg
className="rounded me-2"
width="20"
height="20"
xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="xMidYMid slice"
focusable="false"
role="img"
>
<rect width="100%" height="100%" fill="#007aff"></rect>
</svg>
<div className="fw-bold me-auto">CoreUI for React.js</div>
<small>7 min ago</small>
</CToastHeader>
<CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>
<CToast autohide={false} visible={true}>
<CToastHeader closeButton>
<svg
className="rounded me-2"
width="20"
height="20"
xmlns="http://www.w3.org/2000/svg"
preserveAspectRatio="xMidYMid slice"
focusable="false"
role="img"
>
<rect width="100%" height="100%" fill="#007aff"></rect>
</svg>
<div className="fw-bold me-auto">CoreUI for React.js</div>
<small>7 min ago</small>
</CToastHeader>
<CToastBody>Hello, world! This is a toast message.</CToastBody>
</CToast>
</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.

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

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

<CToast autohide={false} visible={true}>
<CToastBody>
Hello, world! This is a toast message.
<div className="mt-2 pt-2 border-top">
<CButton type="button" color="primary" size="sm">
Take action
</CButton>
<CToastClose as={CButton} color="secondary" size="sm" className="ms-1">
Close
</CToastClose>
</div>
</CToastBody>
</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.

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

Customizing#

CSS variables#

React toasts use local CSS variables on .toast for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

--cui-toast-zindex: #{$zindex-toast};
--cui-toast-padding-x: #{$toast-padding-x};
--cui-toast-padding-y: #{$toast-padding-y};
--cui-toast-spacing: #{$toast-spacing};
--cui-toast-max-width: #{$toast-max-width};
@include rfs($toast-font-size, --cui-toast-font-size);
--cui-toast-color: #{$toast-color};
--cui-toast-bg: #{$toast-background-color};
--cui-toast-border-width: #{$toast-border-width};
--cui-toast-border-color: #{$toast-border-color};
--cui-toast-border-radius: #{$toast-border-radius};
--cui-toast-box-shadow: #{$toast-box-shadow};
--cui-toast-header-color: #{$toast-header-color};
--cui-toast-header-bg: #{$toast-header-background-color};
--cui-toast-header-border-color: #{$toast-header-border-color};

How to use CSS variables#

const vars = {
'--my-css-var': 10,
'--my-another-css-var': "red"
}
return <CToast style={vars}>...</CToast>

SASS variables#

$toast-max-width: 350px !default;
$toast-padding-x: .75rem !default;
$toast-padding-y: .5rem !default;
$toast-font-size: .875rem !default;
$toast-color: null !default;
$toast-background-color: rgba(var(--cui-body-bg-rgb), .85) !default;
$toast-border-width: var(--cui-border-width) !default;
$toast-border-color: var(--cui-border-color-translucent) !default;
$toast-border-radius: var(--cui-border-radius) !default;
$toast-box-shadow: var(--cui-box-shadow) !default;
$toast-spacing: $container-padding-x !default;
$toast-header-color: var(--cui-secondary-color) !default;
$toast-header-background-color: rgba(var(--cui-body-bg-rgb), .85) !default;
$toast-header-border-color: $toast-border-color !default;

API#

CToast#

import { CToast } from '@coreui/react'
// or
import 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#

import { CToastHeader } from '@coreui/react'
// or
import 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#

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

CToastClose#

import { CToastClose } from '@coreui/react'
// or
import CToastClose from '@coreui/react/src/components/toast/CToastClose'
PropertyDescriptionTypeDefault
activeToggle the active state for the component.boolean-
asComponent used for the root node. Either a string to use a HTML element or a component.(ElementType & string) | (ElementType & ComponentClass<any, any>) | (ElementType & FunctionComponent<any>)-
classNameA string of all className you want applied to the base component.string-
darkInvert the default color.boolean-
disabledToggle the disabled state for the component.boolean-
hrefThe href attribute specifies the URL of the page the link goes to.string-
shapeSelect the shape of the component.'rounded' | 'rounded-top' | 'rounded-end' | 'rounded-bottom' | 'rounded-start' | 'rounded-circle' | 'rounded-pill' | 'rounded-0' | 'rounded-1' | 'rounded-2' | 'rounded-3' | string-
sizeSize the component small or large.'sm' | 'lg'-
typeSpecifies the type of button. Always specify the type attribute for the <button> element.
Different browsers may use different default types for the <button> element.
'button' | 'submit' | 'reset'-
variantSet the button variant to an outlined button or a ghost button.'outline' | 'ghost'-
white
Deprecated 5.0.0+
Change the default color to white.boolean-

CToaster#

import { CToaster } from '@coreui/react'
// or
import 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-