Bootstrap React Alert
Bootstrap 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.
Examples#
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.
1<CAlert color="primary">2 A simple primary alert—check it out!3</CAlert>4<CAlert color="secondary">5 A simple secondary alert—check it out!6</CAlert>7<CAlert color="success">8 A simple success alert—check it out!9</CAlert>10<CAlert color="danger">11 A simple danger alert—check it out!12</CAlert>13<CAlert color="warning">14 A simple warning alert—check it out!15</CAlert>16<CAlert color="info">17 A simple info alert—check it out!18</CAlert>19<CAlert color="light">20 A simple light alert—check it out!21</CAlert>22<CAlert color="dark">23 A simple dark alert—check it out!24</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.
1const [visible, setVisible] = useState(false)2return (3 <>4 <CAlert color="primary" dismissible visible={visible} onClose={() => setVisible(false)}>A simple primary alert—check it out!</CAlert>5 <CButton color="primary" onClick={() => setVisible(true)}>Show live alert</CButton>6 </>7)
Link color#
Use the <CAlertLink>
component to immediately give matching colored links inside any alert.
1<CAlert color="primary">2 A simple primary alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.3</CAlert>4<CAlert color="secondary">5 A simple secondary alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.6</CAlert>7<CAlert color="success">8 A simple success alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.9</CAlert>10<CAlert color="danger">11 A simple danger alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.12</CAlert>13<CAlert color="warning">14 A simple warning alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.15</CAlert>16<CAlert color="info">17 A simple info alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.18</CAlert>19<CAlert color="light">20 A simple light alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.21</CAlert>22<CAlert color="dark">23 A simple dark alert with <CAlertLink href="#">an example link</CAlertLink>. Give it a click if you like.24</CAlert>
Additional content#
Alert can also incorporate supplementary components & elements like heading, paragraph, and divider.
Well done!
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.
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
1<CAlert color="success">2 <CAlertHeading tag="h4">Well done!</CAlertHeading>3 <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>4 <hr />5 <p className="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>6</CAlert>
Icons#
Similarly, you can use flexbox utilities and CoreUI Icons to create alerts with icons. Depending on your icons and content, you may want to add more utilities or custom styles.
1<CAlert color="primary" className="d-flex align-items-center">2 <svg className="flex-shrink-0 me-2" width="24" height="24" viewBox="0 0 512 512">3 <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>4 </svg>5 <div>6 An example alert with an icon7 </div>8</CAlert>
Need more than one icon for your alerts? Consider using CoreUI Icons.
1<CAlert color="primary" className="d-flex align-items-center">2 <CIcon icon={cilInfo} className="flex-shrink-0 me-2" width={24} height={24} />3 <div>4 An example alert with an icon5 </div>6</CAlert>7<CAlert color="success" className="d-flex align-items-center">8 <CIcon icon={cilCheckCircle} className="flex-shrink-0 me-2" width={24} height={24} />9 <div>10 An example success alert with an icon11 </div>12</CAlert>13<CAlert color="warning" className="d-flex align-items-center">14 <CIcon icon={cilWarning} className="flex-shrink-0 me-2" width={24} height={24} />15 <div>16 An example warning alert with an icon17 </div>18</CAlert>19<CAlert color="danger" className="d-flex align-items-center">20 <CIcon icon={cilBurn} className="flex-shrink-0 me-2" width={24} height={24} />21 <div>22 An example danger alert with an icon23 </div>24</CAlert>
Dismissing#
Alerts can also be easily dismissed. Just add the dismissible
prop.
1<CAlert2 color="warning"3 dismissible4 onClose={() => {5 alert("👋 Well, hi there! Thanks for dismissing me.");6 }}7>8 <strong>Go right ahead</strong> and click that dimiss over there on the right.9</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.
API#
CAlert#
1import { CAlert } from '@coreui/bootstrap-react'2// or3import CAlert from '@coreui/bootstrap-react/src/components/alert/CAlert'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the 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 | primary |
dismissible | Optionally add a close button to alert and allow it to self dismiss. | boolean | - |
onClose | Callback fired when the component requests to be closed. | () => void | - |
variant | Set the alert variant to a solid. | string | - |
visible | Toggle the visibility of component. | boolean | true |
CAlertHeading#
1import { CAlertHeading } from '@coreui/bootstrap-react'2// or3import CAlertHeading from '@coreui/bootstrap-react/src/components/alert/CAlertHeading'
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> | - |
CAlertLink#
1import { CAlertLink } from '@coreui/bootstrap-react'2// or3import CAlertLink from '@coreui/bootstrap-react/src/components/alert/CAlertLink'
Property | Description | Type | Default |
---|---|---|---|
className | A string of all className you want applied to the base component. | string | - |