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#

Editable Example

Button Sizes#

Use the size prop to change the size of the button. You can set the value to xs, sm, md, or lg.

Editable Example

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.

Editable Example

Button with icon#

You can add left and right icons to the Button component using the leftIcon and rightIcon props respectively.

Note: The leftIcon and rightIcon prop values should be react elements NOT strings.

Editable Example

You can also use icons from popular libraries like react-icons and pass it into the button.

Editable Example

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.

Editable Example

You can also change the spinner itself to use custom loaders as per your design requirements. Pass the spinner prop and set it to a custom react element.

Editable Example

Social Buttons#

We've included the colors for common social media platforms, and you can simply use their buttons via the colorScheme prop.

Editable Example

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 and variant 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.
Editable Example

To flush the buttons, pass the isAttached prop.

Editable Example

Accessibility#

  • Button has role of button.
  • 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.

Editable Example

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.

Editable Example

Props#

Button Props#

Button composes the Box component, so you can pass all its props. These are props specific to the Button component:

NameTypeDescriptionDefault
colorScheme"blue" | "cyan" | "gray" | "green" | "orange" | "pink" | "purple" | "red" | "teal" | "yellow" | "whiteAlpha" | "blackAlpha" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram""gray"
iconSpacingSystemProps["marginRight"]The space between the button icon and label.-
isActivebooleanIf `true`, the button will be styled in its active state.-
isDisabledbooleanIf `true`, the button will be disabled.-
isFullWidthbooleanIf `true`, the button will take up the full width of its container.-
isLoadingbooleanIf `true`, the button will show a spinner.-
leftIconReact.ReactElementIf added, the button will show an icon before the button's label.-
loadingTextstringThe label to show in the button when `isLoading` is true If no text is passed, it only shows the spinner-
rightIconReact.ReactElementIf added, the button will show an icon after the button's label.-
size"sm" | "md" | "lg" | "xs""md"
spinnerReact.ReactElementReplace the spinner component when `isLoading` is set to `true`-
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:

NameTypeDescriptionDefault
colorScheme"blue" | "cyan" | "gray" | "green" | "orange" | "pink" | "purple" | "red" | "teal" | "yellow" | "whiteAlpha" | "blackAlpha" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"Color Schemes for ButtonGroup are not implemented in the default theme. You can extend the theme to implement them.-
isAttachedbooleanIf `true`, the borderRadius of button that are direct children will be altered to look flushed together-
isDisabledbooleanIf `true`, all wrapped button will be disabled-
size"sm" | "md" | "lg" | "xs"-
spacingSystemProps["marginRight"]The spacing between the buttons'0.5rem'
variant"link" | "outline" | "solid" | "ghost" | "unstyled"-