boolean
Alert
Alerts are used to communicate a state that affects a system, feature or page.
The Alert
component is a multipart component. The styling needs to be applied
to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
container
- B:
title
- C:
description
- D:
icon
- E:
spinner
Theming properties#
The properties that affect the theming of the Alert
component are:
variant
: The visual variant of the alert. Defaults tosubtle
.colorScheme
: The color combination of the alert. Defaults toblue
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)
Customizing the default theme#
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to styletitle: {fontFamily: 'mono', // change the font familycolor: 'teal.500', // change the input text color},})export const alertTheme = defineMultiStyleConfig({ baseStyle })
After customizing the alert theme, we can import it in our theme file and add it
in the components
property:
import { extendTheme } from '@chakra-ui/react'import { alertTheme } from './components/alert'export const theme = extendTheme({components: { Alert: alertTheme },})
This is a crucial step to make sure that any changes that we make to the alert theme are applied.
Adding a custom size#
Let's assume we want to include an extra large input size. Here's how we can do that:
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)const xl = defineStyle({fontSize: 'lg',px: '4',h: '12',})const sizes = {xl: definePartsStyle({ title: xl, description: xl }),}export const alertTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size<Alert size="xl" ... />
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom variant. Here's how we can do that:
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)const customVariant = definePartsStyle({container: {border: '1px solid',borderColor: 'gray.200',background: 'gray.50',// Let's also provide dark mode alternatives_dark: {borderColor: 'gray.600',background: 'gray.800',},},title: {color: 'teal.500',_dark: {color: 'cyan.400',},},})export const alertTheme = defineMultiStyleConfig({variants: { custom: customVariant },})// Now we can use the new `custom` variant<Alert variant="custom" ... />
Changing the default properties#
Let's assume we want to change the default size and variant of every input in our app. Here's how we can do that:
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(alertAnatomy.keys,)export const alertTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'custom',},})// This saves you time, instead of manually setting the size and variant every time you use an input:<Alert size="xl" variant="custom" ... />
Showcase#
import { Box, useColorMode, IconButton, Alert, AlertDescription, AlertIcon, AlertTitle, } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box pt={12} position="relative" h="100vh"> <Alert size="md" status="error"> <AlertIcon /> <AlertTitle>Your browser is outdated!</AlertTitle> <AlertDescription> Your Chakra experience may be degraded. </AlertDescription> </Alert> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }
Props#
Alert Props#
Alert
is the wrapper for Alert
component. It composes the Flex
component.
addRole
addRole
false
colorScheme
colorScheme
The visual color appearance of the component
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
blue
size
size
The size of the Alert
string
status
status
The status of the alert
"info" | "warning" | "success" | "error" | "loading"
info
variant
variant
The variant of the Alert
"subtle" | "left-accent" | "top-accent" | "solid"
subtle
AlertIcon Props#
AlertIcon
composes Icon
and changes the icon based on the status
prop.
AlertTitle Props#
AlertTitle
composes the Box
component.
AlertDescription Props#
AlertDescription
composes the Box
component.
The Alert
component is a multipart component. The styling needs to be applied
to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
container
- B:
title
- C:
description
- D:
icon
- E:
spinner
Theming properties#
The properties that affect the theming of the Alert
component are:
variant
: The visual variant of the alert. Defaults tosubtle
.colorScheme
: The color combination of the alert. Defaults toblue
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)
Customizing the default theme#
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to styletitle: {fontFamily: 'mono', // change the font familycolor: 'teal.500', // change the input text color},})export const alertTheme = defineMultiStyleConfig({ baseStyle })
After customizing the alert theme, we can import it in our theme file and add it
in the components
property:
import { extendTheme } from '@chakra-ui/react'import { alertTheme } from './components/alert'export const theme = extendTheme({components: { Alert: alertTheme },})
This is a crucial step to make sure that any changes that we make to the alert theme are applied.
Adding a custom size#
Let's assume we want to include an extra large input size. Here's how we can do that:
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)const xl = defineStyle({fontSize: 'lg',px: '4',h: '12',})const sizes = {xl: definePartsStyle({ title: xl, description: xl }),}export const alertTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size<Alert size="xl" ... />
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom variant. Here's how we can do that:
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(alertAnatomy.keys)const customVariant = definePartsStyle({container: {border: '1px solid',borderColor: 'gray.200',background: 'gray.50',// Let's also provide dark mode alternatives_dark: {borderColor: 'gray.600',background: 'gray.800',},},title: {color: 'teal.500',_dark: {color: 'cyan.400',},},})export const alertTheme = defineMultiStyleConfig({variants: { custom: customVariant },})// Now we can use the new `custom` variant<Alert variant="custom" ... />
Changing the default properties#
Let's assume we want to change the default size and variant of every input in our app. Here's how we can do that:
import { alertAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(alertAnatomy.keys,)export const alertTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'custom',},})// This saves you time, instead of manually setting the size and variant every time you use an input:<Alert size="xl" variant="custom" ... />
Showcase#
import { Box, useColorMode, IconButton, Alert, AlertDescription, AlertIcon, AlertTitle, } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box pt={12} position="relative" h="100vh"> <Alert size="md" status="error"> <AlertIcon /> <AlertTitle>Your browser is outdated!</AlertTitle> <AlertDescription> Your Chakra experience may be degraded. </AlertDescription> </Alert> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }