apps/www/content/docs/components/button.mdx
import { Button, ButtonGroup } from "@chakra-ui/react"
<Button>Click me</Button>
Use the size prop to change the size of the button.
Use the variant prop to change the visual style of the Button.
Use icons within a button
<ExampleTabs name="button-with-icons" />Use the colorPalette prop to change the color of the button
Use the disabled prop to disable the button.
When using the disabled prop with a link, you need to prevent the default
behavior of the link and add the data-disabled attribute.
Pass the loading and loadingText props to the Button component to show a
loading spinner and add a loading text.
Here's an example of how to toggle the loading state of a button while keeping the width of the button the same.
<ExampleTabs name="button-with-loading-toggle" />Use the spinnerPlacement prop to change the placement of the spinner.
Use the spinner prop to change the spinner.
Use the ButtonGroup component to group buttons together. This component allows
you pass common recipe properties to inner buttons.
To flush the buttons, pass the attached prop.
Combine a Button and Menu with an attached Group to create a split button
with a dropdown menu.
Use the object syntax on the size prop to make the button size responsive.
Use the rounded prop to change the radius of the button.
Use the asChild prop to render a button as a link.
Here's how to access the underlying element reference
const Demo = () => {
const ref = useRef<HTMLButtonElement | null>(null)
return <Button ref={ref}>Click me</Button>
}
:::info
After customizing the recipe, run the CLI typegen command to regenerate the types. See the CLI docs for how to run typegen in postinstall, CI, and monorepos.
npx @chakra-ui/cli typegen
:::
Use the defineRecipe function to add a new variant to the button. In this
example, we add a pill variant that respects the colorPalette prop.
import { createSystem, defaultConfig, defineRecipe } from "@chakra-ui/react"
const buttonRecipe = defineRecipe({
variants: {
variant: {
pill: {
borderRadius: "full",
px: "6",
bg: "colorPalette.solid",
color: "colorPalette.contrast",
_hover: {
bg: "colorPalette.solid/90",
},
},
},
},
})
const system = createSystem(defaultConfig, {
theme: {
recipes: { button: buttonRecipe },
},
})
Then use the pill variant in your button:
<Button variant="pill" colorPalette="blue">
Pill Button
</Button>
Use the defineRecipe function to add a new size to the button.
import { createSystem, defaultConfig, defineRecipe } from "@chakra-ui/react"
const buttonRecipe = defineRecipe({
variants: {
size: {
xxl: {
h: "16",
minW: "16",
textStyle: "xl",
px: "8",
},
},
},
})
const system = createSystem(defaultConfig, {
theme: {
recipes: { button: buttonRecipe },
},
})
Then use the xxl size in your button:
<Button size="xxl">XXL Button</Button>
Use the defaultVariants property to change the default size or variant.
import { createSystem, defaultConfig, defineRecipe } from "@chakra-ui/react"
const buttonRecipe = defineRecipe({
defaultVariants: {
size: "lg",
variant: "outline",
},
})
const system = createSystem(defaultConfig, {
theme: {
recipes: { button: buttonRecipe },
},
})