The visual color appearance of the component
Badge
Badges are used to highlight an item's status for quick recognition.
Import#
import { Badge } from '@chakra-ui/react'
Usage#
<Badge>Default</Badge>
Badge Color#
Pass the colorScheme
prop to customize the color of the Badge. colorScheme
can be any color key that exists in theme.colors
.
Learn more about theming.
<Stack direction='row'><Badge>Default</Badge><Badge colorScheme='green'>Success</Badge><Badge colorScheme='red'>Removed</Badge><Badge colorScheme='purple'>New</Badge></Stack>
Badge Variants#
Pass the variant
prop and set it to either subtle
, solid
, or outline
to
get a different style.
<Stack direction='row'><Badge variant='outline' colorScheme='green'>Default</Badge><Badge variant='solid' colorScheme='green'>Success</Badge><Badge variant='subtle' colorScheme='green'>Removed</Badge></Stack>
Composition#
<Flex><Avatar src='https://bit.ly/sage-adebayo' /><Box ml='3'><Text fontWeight='bold'>Segun Adebayo<Badge ml='1' colorScheme='green'>New</Badge></Text><Text fontSize='sm'>UI Engineer</Text></Box></Flex>
You can also change the size of the badge by passing the fontSize
prop.
<Text fontSize='xl' fontWeight='bold'>Segun Adebayo<Badge ml='1' fontSize='0.8em' colorScheme='green'>New</Badge></Text>
Props#
The Badge
component composes Box
component so you can pass props for Box
.
colorScheme
colorScheme
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink"
gray
size
size
The size of the Badge
string
variant
variant
The variant of the Badge
"solid" | "subtle" | "outline"
subtle
The Badge
component is a single part component. All of the styling is applied
directly to the span
element.
To learn more about styling single part components, visit the Component Style page.
Theming properties#
The properties that affect the theming of the Badge
component are:
variant
: The visual variant of the badge. Defaults tosubtle
.colorScheme
: The color scheme of the badge. Defaults togray
.size
: The size of the badge.
Theming utilities#
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
Customizing the default theme#
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const boxy = defineStyle({border: '1px solid', // change the appearance of the borderborderRadius: 0, // remove the border radius})export const badgeTheme = defineStyleConfig({variants: { boxy },})
After customizing the badge theme, we can import it in our theme file and add it
in the components
property:
import { extendTheme } from '@chakra-ui/react'import { badgeTheme } from './components/badge'export const theme = extendTheme({components: { Badge: badgeTheme },})
This is a crucial step to make sure that any changes that we make to the badge theme are applied.
Adding a custom size#
Let's assume we want to include an extra large badge size. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const xl = defineStyle({fontSize: 'xl',})export const badgeTheme = defineStyleConfig({sizes: { xl },})// Now we can use the new `xl` size<Badge size='xl'>XL Badge</Badge>
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 branded variant. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const brandPrimary = defineStyle({background: 'orange.500',color: 'white',fontFamily: 'serif',fontWeight: 'normal',// let's also provide dark mode alternatives_dark: {background: 'orange.300',color: 'orange.800',}})export const badgeTheme = defineStyleConfig({variants: { brandPrimary }})// Now we can use the new `brandPrimary` variant<Badge variant='brandPrimary'>...</Badge>
Using a custom color scheme#
Let's assume we want to use our own custom color scale based on our brand. We'd need to define the color scale first in the main theme file:
import { extendTheme } from '@chakra-ui/react'export const theme = extendTheme({colors: {brand: {100: '#9520e4',...200: '#33006d',...800: '#1a0044',}}})
Then, we can use the custom color scale as the color scheme for the badge:
<Badge colorScheme='brand'>...</Badge>
Changing the default properties#
Let's assume we want to change the default size, variant or color scheme of every badge in our app. Here's how we can do that:
import { defineStyleConfig } from '@chakra-ui/react'export const badgeTheme = defineStyleConfig({defaultProps: {size: 'xl',variant: 'outline',colorScheme: 'brand',},})// This saves you time, instead of manually setting the size,// variant and color scheme every time you use a badge:<Badge size="xl" variant="outline" colorScheme="brand">...</Badge>
Showcase#
import { Badge, Box, HStack, IconButton, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <HStack spacing={8} p={12}> <Badge >Themed subtle badge</Badge> <Badge variant='solid'>Themed solid badge</Badge> <Badge variant='outline'>Themed outline badge</Badge> </HStack> <HStack spacing={8} p={12}> <Badge variant='custom'>Themed custom badge</Badge> <Badge size='xl'>XL Badge</Badge> </HStack> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ) }