React Alert Component

React alert component gives contextual feedback information for common user operations. The alert component is delivered with a bunch of usable and adjustable alert messages.

Other frameworks

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

How to use React Alert Component.#

React Alert is prepared for any length of text, as well as an optional close button. For a styling, use one of the required contextual color props (e.g., primary). For inline dismissal, use the dismissing prop.

<CAlert color="primary">
A simple primary alert—check it out!
</CAlert>
<CAlert color="secondary">
A simple secondary alert—check it out!
</CAlert>
<CAlert color="success">
A simple success alert—check it out!
</CAlert>
<CAlert color="danger">
A simple danger alert—check it out!
</CAlert>
<CAlert color="warning">
A simple warning alert—check it out!
</CAlert>
<CAlert color="info">
A simple info alert—check it out!
</CAlert>
<CAlert color="light">
A simple light alert—check it out!
</CAlert>
<CAlert color="dark">
A simple dark alert—check it out!
</CAlert>
Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Live example#

Click the button below to show an alert (hidden with inline styles to start), then dismiss (and destroy) it with the built-in close button.

const [visible, setVisible] = useState(false)
return (
<>
<CAlert color="primary" dismissible visible={visible} onClose={() => setVisible(false)}>
A simple primary alert—check it out!
</CAlert>
<CButton color="primary" onClick={() => setVisible(true)}>
Show live alert
</CButton>
</>
)

Use the <CAlertLink> component to immediately give matching colored links inside any react alert component.

<CAlert color="primary">
A simple primary alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="secondary">
A simple secondary alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="success">
A simple success alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="danger">
A simple danger alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="warning">
A simple warning alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="info">
A simple info alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="light">
A simple light alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>
<CAlert color="dark">
A simple dark alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.
</CAlert>

Additional content#

React Alert can also incorporate supplementary components & elements like heading, paragraph, and divider.

<CAlert color="success">
<CAlertHeading as="h4">Well done!</CAlertHeading>
<p>
Aww yeah, you successfully read this important alert message. This example text is going to run
a bit longer so that you can see how spacing within an alert works with this kind of content.
</p>
<hr />
<p className="mb-0">
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
</p>
</CAlert>

Icons#

Similarly, you can use flexbox utilities and CoreUI Icons to create react alerts with icons. Depending on your icons and content, you may want to add more utilities or custom styles.

<CAlert color="primary" className="d-flex align-items-center">
<svg className="flex-shrink-0 me-2" width="24" height="24" viewBox="0 0 512 512">
<rect
width="32"
height="176"
x="240"
y="176"
fill="var(--ci-primary-color, currentColor)"
className="ci-primary"
></rect>
<rect
width="32"
height="32"
x="240"
y="384"
fill="var(--ci-primary-color, currentColor)"
className="ci-primary"
></rect>
<path
fill="var(--ci-primary-color, currentColor)"
d="M274.014,16H237.986L16,445.174V496H496V445.174ZM464,464H48V452.959L256,50.826,464,452.959Z"
className="ci-primary"
></path>
</svg>
<div>An example alert with an icon</div>
</CAlert>

Need more than one icon for your react alerts? Consider using CoreUI Icons.

<CAlert color="primary" className="d-flex align-items-center">
<CIcon icon={cilInfo} className="flex-shrink-0 me-2" width={24} height={24} />
<div>An example alert with an icon</div>
</CAlert>
<CAlert color="success" className="d-flex align-items-center">
<CIcon icon={cilCheckCircle} className="flex-shrink-0 me-2" width={24} height={24} />
<div>An example success alert with an icon</div>
</CAlert>
<CAlert color="warning" className="d-flex align-items-center">
<CIcon icon={cilWarning} className="flex-shrink-0 me-2" width={24} height={24} />
<div>An example warning alert with an icon</div>
</CAlert>
<CAlert color="danger" className="d-flex align-items-center">
<CIcon icon={cilBurn} className="flex-shrink-0 me-2" width={24} height={24} />
<div>An example danger alert with an icon</div>
</CAlert>

Solid#

Use variant="solid" to change contextual colors to solid.

<CAlert color="primary" variant="solid">A simple solid primary alert—check it out!</CAlert>
<CAlert color="secondary" variant="solid">A simple solid secondary alert—check it out!</CAlert>
<CAlert color="success" variant="solid">A simple solid success alert—check it out!</CAlert>
<CAlert color="danger" variant="solid">A simple solid danger alert—check it out!</CAlert>
<CAlert color="warning" variant="solid">A simple solid warning alert—check it out!</CAlert>
<CAlert color="info" variant="solid">A simple solid info alert—check it out!</CAlert>
<CAlert color="light" variant="solid" className="text-high-emphasis">A simple solid light alert—check it out!</CAlert>
<CAlert color="dark" variant="solid">A simple solid dark alert—check it out!</CAlert>

Dismissing#

React Alert component can also be easily dismissed. Just add the dismissible prop.

<CAlert
color="warning"
dismissible
onClose={() => {
alert('👋 Well, hi there! Thanks for dismissing me.')
}}
>
<strong>Go right ahead</strong> and click that dimiss over there on the right.
</CAlert>

When an alert is dismissed, the element is completely removed from the page structure. If a keyboard user dismisses the alert using the close button, their focus will suddenly be lost and, depending on the browser, reset to the start of the page/document.

Customizing#

CSS variables#

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

--cui-alert-bg: transparent;
--cui-alert-padding-x: #{$alert-padding-x};
--cui-alert-padding-y: #{$alert-padding-y};
--cui-alert-margin-bottom: #{$alert-margin-bottom};
--cui-alert-color: inherit;
--cui-alert-border-color: transparent;
--cui-alert-border: #{$alert-border-width} solid var(--cui-alert-border-color);
--cui-alert-border-radius: #{$alert-border-radius};
--cui-alert-link-color: inherit;

How to use CSS variables#

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

SASS variables#

$alert-padding-y: $spacer !default;
$alert-padding-x: $spacer !default;
$alert-margin-bottom: 1rem !default;
$alert-border-radius: var(--cui-border-radius) !default;
$alert-link-font-weight: $font-weight-bold !default;
$alert-border-width: var(--cui-border-width) !default;
$alert-dismissible-padding-r: $alert-padding-x * 3 !default; // 3x covers width of x plus default padding on either side

API#

CAlert#

import { CAlert } from '@coreui/react'
// or
import CAlert from '@coreui/react/src/components/alert/CAlert'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the component.string-
colorSets the color context of the component to one of CoreUI’s themed colors.'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | stringprimary
dismissibleOptionally add a close button to alert and allow it to self dismiss.boolean-
onCloseCallback fired when the component requests to be closed.() => void-
variantSet the alert variant to a solid.string-
visibleToggle the visibility of component.booleantrue

CAlertHeading#

import { CAlertHeading } from '@coreui/react'
// or
import CAlertHeading from '@coreui/react/src/components/alert/CAlertHeading'
PropertyDescriptionTypeDefault
asComponent used for the root node. Either a string to use a HTML element or a component.(ElementType & 'symbol') | (ElementType & 'object') | (ElementType & 'h4') | (ElementType & 'slot') | (ElementType & 'style') | ... 173 more ... | (ElementType & FunctionComponent<...>)-
classNameA string of all className you want applied to the base component.string-
import { CAlertLink } from '@coreui/react'
// or
import CAlertLink from '@coreui/react/src/components/alert/CAlertLink'
PropertyDescriptionTypeDefault
classNameA string of all className you want applied to the base component.string-