"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
Button
The 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#
<DarkMode><Button borderRadius="12px" colorScheme="blue">Button</Button></DarkMode>
<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
.
<DarkMode><Stack spacing={4} direction="row" align="center"><Button borderRadius="12px" colorScheme="teal" size="xs">Button</Button><Button borderRadius="12px" colorScheme="teal" size="sm">Button</Button><Button borderRadius="12px" colorScheme="teal" size="md">Button</Button><Button borderRadius="12px" colorScheme="teal" size="lg">Button</Button></Stack></DarkMode>
<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
, ghost
, outline
, or link
.
<DarkMode><Stack direction="row" spacing={4} align="center"><Button borderRadius="12px" colorScheme="teal" variant="solid">Button</Button><Button borderRadius="12px" colorScheme="teal" variant="outline">Button</Button><Button borderRadius="12px" colorScheme="teal" variant="ghost">Button</Button><Button borderRadius="12px" colorScheme="teal" variant="link">Button</Button></Stack></DarkMode>
<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 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.
<DarkMode><Stack direction="row" spacing={4}><ButtonborderRadius="12px"leftIcon={<EmailIcon />}colorScheme="teal"variant="solid"></Button><ButtonborderRadius="12px"rightIcon={<ArrowForwardIcon />}colorScheme="teal"variant="outline">Call us</Button></Stack></DarkMode>
<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.
<DarkMode><Stack direction="row" spacing={4}><ButtonborderRadius="12px"leftIcon={<MdBuild />}colorScheme="pink"variant="solid">Settings</Button><ButtonborderRadius="12px"rightIcon={<MdCall />}colorScheme="blue"variant="outline">Call us</Button></Stack></DarkMode>
// 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.
<DarkMode><Stack direction="row" spacing={4}><Button borderRadius="12px" isLoading colorScheme="teal" variant="solid"></Button><ButtonborderRadius="12px"isLoadingloadingText="Submitting"colorScheme="teal"variant="outline">Submit</Button></Stack></DarkMode>
<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.
<DarkMode><ButtonborderRadius="12px"isLoadingcolorScheme="blue"spinner={<BeatLoader size={8} color="white" />}>Click me</Button></DarkMode>
<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.
<DarkMode><Stack direction="row" spacing={4} align="center"><ButtonborderRadius="12px"isLoadingloadingText="Loading"colorScheme="red"variant="outline"spinnerPlacement="start">Submit</Button><ButtonborderRadius="12px"isLoadingloadingText="Loading"colorScheme="teal"variant="outline"spinnerPlacement="end">Continue</Button></Stack></DarkMode>
<Stack direction="row" spacing={4} align="center"><ButtonisLoadingloadingText="Loading"colorScheme="red"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.
<DarkMode><HStack><ButtonborderRadius="12px"colorScheme="facebook"leftIcon={<FaFacebook />}></Button><Button borderRadius="12px" colorScheme="twitter" leftIcon={<FaTwitter />}></Button></HStack></DarkMode>
<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 the its children as needed.
<DarkMode><ButtonGroup borderRadius="12px" variant="outline" spacing="6"><Button borderRadius="12px" colorScheme="blue">Save</Button><Button borderRadius="12px">Cancel</Button></ButtonGroup></DarkMode>
<ButtonGroup variant="outline" spacing="6"><Button colorScheme="blue">Save</Button><Button>Cancel</Button></ButtonGroup>
To flush the buttons, pass the isAttached
prop.
<DarkMode><ButtonGroup borderRadius="12px" size="sm" isAttached variant="outline"><Button borderRadius="12px" mr="-px">Save</Button><IconButtonborderRadius="12px"aria-label="Add to friends"icon={<AddIcon />}/></ButtonGroup></DarkMode>
<ButtonGroup size="sm" isAttached variant="outline"><Button mr="-px">Save</Button><IconButton aria-label="Add to friends" icon={<AddIcon />} /></ButtonGroup>
Variants#
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 the its children as needed.
<DarkMode><ButtonGroup borderRadius="12px" variant="outline" spacing="6"><Button borderRadius="12px" colorScheme="blue">Save</Button><Button borderRadius="12px">Cancel</Button></ButtonGroup></DarkMode>
<ButtonGroup variant="outline" spacing="6"><Button colorScheme="blue">Save</Button><Button>Cancel</Button></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.
Custom variants:#
"transparent-with-icon"
function Example() {let mainTeal = useColorModeValue("teal.300", "teal.300")let inputBg = useColorModeValue("white", "gray.800")let mainText = useColorModeValue("gray.700", "gray.200")let navbarIcon = useColorModeValue("gray.500", "gray.200")let searchIcon = useColorModeValue("gray.700", "gray.200")return (<DarkMode><Buttonbg="transparent"fontWeight="bold"borderRadius="inherit"cursor="pointer"_hover="none"_active={{bg: "transparent",transform: "none",borderColor: "transparent",}}_focus={{ boxShadow: "none" }}_hover={{ boxShadow: "none" }}borderRadius="12px"colorScheme="blue"ms="0px"px="0px"me={{ sm: "2px", md: "16px" }}color={navbarIcon}variant="transparent-with-icon"leftIcon={<EmailIcon color={navbarIcon} w="22px" h="22px" me="0px" />}></Button></DarkMode>)}
<Button variant="transparent-with-icon" colorScheme="blue"></Button>
"no-hover"
function Example() {let bgButton = "linear-gradient(81.62deg, #313860 2.25%, #151928 79.87%)"let colorButton = "white"return (<DarkMode><Button_hover={{ boxShadow: "none" }}bg={bgButton}color={colorButton}fontSize="xs"borderRadius="35px"px="30px"_active="none">Free Download</Button></DarkMode>)}
let bgButton = "linear-gradient(81.62deg, #313860 2.25%, #151928 79.87%)",let colorButton = "white";<Buttonbg={bgButton}color={colorButton}fontSize="xs"variant="no-hover"borderRadius="35px"px="30px">Free Download</Button>
"brand"
function Example() {return (<DarkMode><Buttonbg="brand.500"color="#fff"_hover={{bg: "brand.600",}}_active={{bg: "brand.700",}}_focus="none"fontSize="xs"borderRadius="12px"px="20px"_active="none">Buy Now</Button></DarkMode>)}
<Button fontSize="xs" variant="brand" px="20px">Buy Now</Button>
"outlineWhite"
function Example() {return (<DarkMode><Buttonbg="transparent"color="#fff"borderColor="white"borderWidth="1px"_hover={{bg: "whiteAlpha.100",}}_active={{bg: "whiteAlpha.200",}}_focus="none"fontSize="xs"borderRadius="35px"px="30px">Free Download</Button></DarkMode>)}
<Button fontSize="xs" variant="outlineWhite" borderRadius="35px" px="30px">Free Download</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<DarkMode><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></DarkMode>
// 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
"gray"
iconSpacing
iconSpacing
The space between the button icon and label.
SystemProps["marginRight"]
isActive
isActive
If true
, the button will be styled in its active state.
boolean
isDisabled
isDisabled
If true
, the button will be disabled.
boolean
isFullWidth
isFullWidth
If true
, the button will take up the full width of its container.
boolean
isLoading
isLoading
If true
, the button will show a spinner.
boolean
leftIcon
leftIcon
If added, the button will show an icon before the button's label.
React.ReactElement
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.
React.ReactElement
size
size
"sm" | "md" | "lg" | "xs"
"md"
spinner
spinner
Replace the spinner component when isLoading
is set to true
React.ReactElement
spinnerPlacement
spinnerPlacement
It determines the placement of the spinner when isLoading is true
"end" | "start"
"start"
variant
variant
"link" | "outline" | "solid" | "ghost" | "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:
colorScheme
colorScheme
Color Schemes for ButtonGroup
are not implemented in the default theme. You can extend the theme to implement them.
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
isAttached
isAttached
If true
, the borderRadius of button that are direct children will be altered
to look flushed together
boolean
isDisabled
isDisabled
If true
, all wrapped button will be disabled
boolean
size
size
"sm" | "md" | "lg" | "xs"
spacing
spacing
The spacing between the buttons
SystemProps["marginRight"]
'0.5rem'
variant
variant
"link" | "outline" | "solid" | "ghost" | "unstyled"
© 2021, Made with ❤️ by Creative Tim & Simmmple for a better web