The visual color appearance of the component
Button
Button component is used to trigger an action or event, such as submitting a form, opening a Dialog, canceling an action, or performing a delete operation.
Import#
import { Button, ButtonGroup } from '@chakra-ui/react'
- Button: The button component with support for custom icons, spinners, etc.
- ButtonGroup: Used to group buttons whose actions are related, with an option to flush them together.
Usage#
<Button colorScheme='blue'>Button</Button>
Button Sizes#
Use the size
prop to change the size of the button. You can set the value to
xs
, sm
, md
, or lg
.
<Stack spacing={4} direction='row' align='center'><Button colorScheme='teal' size='xs'>Button</Button><Button colorScheme='teal' size='sm'>Button</Button><Button colorScheme='teal' size='md'>Button</Button><Button colorScheme='teal' size='lg'>Button</Button></Stack>
Button variants#
Use the variant
prop to change the visual style of the Button. You can set the
value to solid
, outline
, ghost
, or link
.
<Stack direction='row' spacing={4} align='center'><Button colorScheme='teal' variant='solid'>Button</Button><Button colorScheme='teal' variant='outline'>Button</Button><Button colorScheme='teal' variant='ghost'>Button</Button><Button colorScheme='teal' variant='link'>Button</Button></Stack>
Button Colors#
Use the colorScheme prop to change the color scheme of the Button. You can set
the value to any of the color scales from your design system, like whiteAlpha
,
blackAlpha
, gray
, red
, blue
, or your custom color scale.
<Stack direction='column'><Boxdisplay='flex'alignItems='center'justifyContent='center'width='100%'py={12}bgImage="url('https://bit.ly/2Z4KKcF')"bgPosition='center'bgRepeat='no-repeat'mb={2}><ButtonGroup gap='4'><Button colorScheme='whiteAlpha'>WhiteAlpha</Button><Button colorScheme='blackAlpha'>BlackAlpha</Button></ButtonGroup></Box><Wrap spacing={4}><WrapItem><Button colorScheme='gray'>Gray</Button></WrapItem><WrapItem><Button colorScheme='red'>Red</Button></WrapItem><WrapItem><Button colorScheme='orange'>Orange</Button></WrapItem><WrapItem><Button colorScheme='yellow'>Yellow</Button></WrapItem><WrapItem><Button colorScheme='green'>Green</Button></WrapItem><WrapItem><Button colorScheme='teal'>Teal</Button></WrapItem><WrapItem><Button colorScheme='blue'>Blue</Button></WrapItem><WrapItem><Button colorScheme='cyan'>Cyan</Button></WrapItem><WrapItem><Button colorScheme='purple'>Purple</Button></WrapItem><WrapItem><Button colorScheme='pink'>Pink</Button></WrapItem><WrapItem><Button colorScheme='linkedin'>Linkedin</Button></WrapItem><WrapItem><Button colorScheme='facebook'>Facebook</Button></WrapItem><WrapItem><Button colorScheme='messenger'>Messenger</Button></WrapItem><WrapItem><Button colorScheme='whatsapp'>Whatsapp</Button></WrapItem><WrapItem><Button colorScheme='twitter'>Twitter</Button></WrapItem><WrapItem><Button colorScheme='telegram'>Telegram</Button></WrapItem></Wrap></Stack>
Button with icon#
You can add left and right icons to the Button component using the leftIcon
and rightIcon
props respectively.
Note: The
leftIcon
andrightIcon
prop values should be react elements NOT strings.
<Stack direction='row' spacing={4}><Button leftIcon={<EmailIcon />} colorScheme='teal' variant='solid'></Button><Button rightIcon={<ArrowForwardIcon />} colorScheme='teal' variant='outline'>Call us</Button></Stack>
You can also use icons from popular libraries like react-icons and pass it into the button.
// import { MdBuild , MdCall } from "react-icons/md"<Stack direction='row' spacing={4}><Button leftIcon={<MdBuild />} colorScheme='pink' variant='solid'>Settings</Button><Button rightIcon={<MdCall />} colorScheme='blue' variant='outline'>Call us</Button></Stack>
Button loading state#
Pass the isLoading
prop to show its loading state. By default, the button will
show a spinner and leave the button's width unchanged.
You can also pass the loadingText
prop to show a spinner and the loading text.
<Stack direction='row' spacing={4}><Button isLoading colorScheme='teal' variant='solid'></Button><ButtonisLoadingloadingText='Submitting'colorScheme='teal'variant='outline'>Submit</Button></Stack>
You can change the spinner element to use custom loaders as per your design
requirements. Pass the spinner
prop and set it to a custom react element.
<ButtonisLoadingcolorScheme='blue'spinner={<BeatLoader size={8} color='white' />}>Click me</Button>
When a loadingText
is present, you can change the placement of the spinner
element to either start
or end
. It is start
by default.
<Stack direction='row' spacing={4} align='center'><ButtonisLoadingloadingText='Loading'colorScheme='teal'variant='outline'spinnerPlacement='start'>Submit</Button><ButtonisLoadingloadingText='Loading'colorScheme='teal'variant='outline'spinnerPlacement='end'>Continue</Button></Stack>
Social Buttons#
We've included the colors for common social media platforms, and you can simply
use their buttons via the colorScheme
prop.
<HStack><Button colorScheme='facebook' leftIcon={<FaFacebook />}></Button><Button colorScheme='twitter' leftIcon={<FaTwitter />}></Button></HStack>
The Facebook and Twitter icons in the above example are available from
react-icons
as FaFacebook
and
FaTwitter
, found in the react-icons/fa
import.
Grouping Buttons#
You can use the Stack
or ButtonGroup
component to group buttons. When you
use the ButtonGroup
component, it allows you to:
- Set the
size
andvariant
of all buttons within it. - Add
spacing
between the buttons. - Flush the buttons together by removing the border radius of their children as needed.
<ButtonGroup variant='outline' spacing='6'><Button colorScheme='blue'>Save</Button><Button>Cancel</Button></ButtonGroup>
To flush the buttons, pass the isAttached
prop.
<ButtonGroup size='sm' isAttached variant='outline'><Button>Save</Button><IconButton aria-label='Add to friends' icon={<AddIcon />} /></ButtonGroup>
Accessibility#
- Button has
role
ofbutton
. - When Button has focus, Space or Enter activates it.
Composition#
All props you pass (variant
, colorScheme
, etc.) are converted to style
props. This means you can override any style of the Button via props.
// The size prop affects the height of the button// It can still be overridden by passing a custom height<Buttonsize='md'height='48px'width='200px'border='2px'borderColor='green.500'>Button</Button>
Custom Button#
In the event that you need to make your own custom button, you can leverage the
Box
component. It provides the hover
, focus
, active
and disabled
style
props to style the button.
// Button from facebook.com<Boxas='button'height='24px'lineHeight='1.2'transition='all 0.2s cubic-bezier(.08,.52,.52,1)'border='1px'px='8px'borderRadius='2px'fontSize='14px'fontWeight='semibold'bg='#f5f6f7'borderColor='#ccd0d5'color='#4b4f56'_hover={{ bg: '#ebedf0' }}_active={{bg: '#dddfe2',transform: 'scale(0.98)',borderColor: '#bec3c9',}}_focus={{boxShadow:'0 0 1px 2px rgba(88, 144, 255, .75), 0 1px 1px rgba(0, 0, 0, .15)',}}>Join Group</Box>
Props#
Button Props#
Button
composes the Box
component, so you can pass all its props. These are
props specific to the Button
component:
colorScheme
colorScheme
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
gray
iconSpacing
iconSpacing
The space between the button icon and label.
ResponsiveValue<string | number | (string & {})>
isActive
isActive
If true
, the button will be styled in its active state.
boolean
false
isDisabled
isDisabled
If true
, the button will be disabled.
boolean
false
isLoading
isLoading
If true
, the button will show a spinner.
boolean
false
leftIcon
leftIcon
If added, the button will show an icon before the button's label.
ReactElement<any, string | JSXElementConstructor<any>>
loadingText
loadingText
The label to show in the button when isLoading
is true
If no text is passed, it only shows the spinner
string
rightIcon
rightIcon
If added, the button will show an icon after the button's label.
ReactElement<any, string | JSXElementConstructor<any>>
size
size
The size of the Button
"lg" | "md" | "sm" | "xs"
md
spinner
spinner
Replace the spinner component when isLoading
is set to true
ReactElement<any, string | JSXElementConstructor<any>>
spinnerPlacement
spinnerPlacement
It determines the placement of the spinner when isLoading is true
"start" | "end"
start
variant
variant
The variant of the Button
"ghost" | "outline" | "solid" | "link" | "unstyled"
solid
Button Group Props#
ButtonGroup
composes the Box
component, so you can pass all its props. These
are props specific to the ButtonGroup
component:
isAttached
isAttached
If true
, the borderRadius of button that are direct children will be altered
to look flushed together
boolean
false
isDisabled
isDisabled
If true
, all wrapped button will be disabled
boolean
false
spacing
spacing
The spacing between the buttons
ResponsiveValue<string | number | (string & {})>
'0.5rem'
The Button
component is a single part component. All of the styling is applied
directly to the button
element.
To learn more about styling single part components, visit the Component Style page.
Theming properties#
The properties that affect the theming of the Button
component are:
variant
: The visual variant of the button. Defaults tosolid
.colorScheme
: The color scheme of the button. Defaults togray
.size
: The size of the button. Defaults tomd
.
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 outline = defineStyle({border: '2px dashed', // change the appearance of the borderborderRadius: 0, // remove the border radiusfontWeight: 'semibold', // change the font weight})export const buttonTheme = defineStyleConfig({variants: { outline },})
After customizing the button theme, we can import it in our theme file and add
it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { buttonTheme } from './components/button'export const theme = extendTheme({components: { Button: buttonTheme },})
This is a crucial step to make sure that any changes that we make to the button theme are applied.
Adding a custom size#
Let's assume we want to include an extra large button size. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const xl = defineStyle({fontSize: 'xl',px: '6',h: '16',borderRadius: 'md',})export const buttonTheme = defineStyleConfig({sizes: { xl },})// Now we can use the new `xl` size<Button size="xl">...</Button>
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 buttonTheme = defineStyleConfig({variants: { brandPrimary },})// Now we can use the new `brandPrimary` variant<Button variant="brandPrimary">...</Button>
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: {50: '#f7fafc',...500: '#718096',...900: '#171923',}}})
Then, we can use the custom color scale as the color scheme for the button:
<Button colorScheme='brand'>...</Button>
Changing the default properties#
Let's assume we want to change the default size, variant or color scheme of every button in our app. Here's how we can do that:
import { defineStyleConfig } from '@chakra-ui/react'export const buttonTheme = defineStyleConfig({defaultProps: {size: 'lg',variant: 'outline',colorScheme: 'brand',},})// This saves you time, instead of manually setting the size,// variant and color scheme every time you use a button:<Button size="lg" variant="outline" colorScheme="brand">...</Button>
Showcase#
import { Box, SimpleGrid, IconButton, Button, 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"> <SimpleGrid gap={12} p={12} columns={2}> <Button> Themed solid button </Button> <Button variant="custom"> Themed custom button </Button> <Button variant="ghost"> Themed ghost button </Button> <Button variant="outline"> Themed outline button </Button> <Button variant="link"> Themed link button </Button> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }