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'// orimport CToast from '@coreui/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 CoreUI’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#
import { CToastHeader } from '@coreui/react'// orimport CToastHeader from '@coreui/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#
import { CToastBody } from '@coreui/react'// orimport CToastBody from '@coreui/react/src/components/toast/CToastBody'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |
CToastClose#
import { CToastClose } from '@coreui/react'// orimport CToastClose from '@coreui/react/src/components/toast/CToastClose'
Property | Description | Type | Default |
---|---|---|---|
active | Toggle the active state for the component. | boolean | - |
as | Component 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>) | - |
className | A string of all className you want applied to the base component. | string | - |
dark | Invert the default color. | boolean | - |
disabled | Toggle the disabled state for the component. | boolean | - |
href | The href attribute specifies the URL of the page the link goes to. | string | - |
shape | Select 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 | - |
size | Size the component small or large. | 'sm' | 'lg' | - |
type | Specifies 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' | - |
variant | Set 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'// orimport CToaster from '@coreui/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 | - |