Alert

Alerts are used to communicate a state that affects a system, feature or page.

    SourceTheme Source@chakra-ui/alert

Import#

Chakra UI exports 4 alert related components.

  • Alert.Root: The wrapper for alert components.
  • Alert.Icon: The visual icon for the alert that changes based on the status prop.
  • Alert.Title: The title of the alert to be announced by screen readers.
  • Alert.Description: The description of the alert to be announced by screen readers.
import { Alert } from '@chakra-ui/react'

Usage#

<Alert.Root status='error'>
<Alert.Icon />
<Alert.Title>Your browser is outdated!</Alert.Title>
<Alert.Description>Your Chakra experience may be degraded.</Alert.Description>
</Alert.Root>

Status#

Change the status of the alerts by passing the status prop. This affects the color scheme and icon used. Alert supports error, success, warning, and info statuses.

<Stack spacing={3}>
<Alert.Root status='error'>
<Alert.Icon />
There was an error processing your request
</Alert.Root>
<Alert.Root status='success'>
<Alert.Icon />
Data uploaded to the server. Fire on!
</Alert.Root>
<Alert.Root status='warning'>
<Alert.Icon />
Seems your account is about expire, upgrade now
</Alert.Root>
<Alert.Root status='info'>
<Alert.Icon />
Chakra is going live on August 30th. Get ready!
</Alert.Root>
</Stack>

Variant#

Alert has 4 variant styles you can use. Pass the variant prop and use either subtle, solid, left-accent or top-accent.

<Stack spacing={3}>
<Alert.Root status='success' variant='subtle'>
<Alert.Icon />
Data uploaded to the server. Fire on!
</Alert.Root>
<Alert.Root status='success' variant='solid'>
<Alert.Icon />
Data uploaded to the server. Fire on!
</Alert.Root>
<Alert.Root status='success' variant='left-accent'>
<Alert.Icon />
Data uploaded to the server. Fire on!
</Alert.Root>
<Alert.Root status='success' variant='top-accent'>
<Alert.Icon />
Data uploaded to the server. Fire on!
</Alert.Root>
</Stack>

Composition#

Alert ships with smaller components to allow for flexibility in creating all kinds of layouts. Here's an example of a custom alert style and layout:

<Alert.Root
status='success'
variant='subtle'
flexDirection='column'
alignItems='center'
justifyContent='center'
textAlign='center'
height='200px'
>
<Alert.Icon boxSize='40px' mr={0} />
<Alert.Title mt={4} mb={1} fontSize='lg'>
Application submitted!
</Alert.Title>
<Alert.Description maxWidth='sm'>
Thanks for submitting your application. Our team will get back to you soon.
</Alert.Description>
</Alert.Root>

Alert can also incorporate other Chakra components. Here's an example of an alert with wrapping description text and the CloseButton component with simple close functionality:

function CompExample() {
const {
isOpen: isVisible,
onClose,
onOpen,
} = useDisclosure({ defaultIsOpen: true })
return isVisible ? (
<Alert.Root status='success'>
<Alert.Icon />
<Box>
<Alert.Title>Success!</Alert.Title>
<Alert.Description>
Your application has been received. We will review your application
and respond within the next 48 hours.
</Alert.Description>
</Box>
<CloseButton
alignSelf='flex-start'
position='relative'
right={-1}
top={-1}
onClick={onClose}
/>
</Alert.Root>
) : (
<Button onClick={onOpen}>Show Alert</Button>
)
}
Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel