"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
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.
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> ); }
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
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
"lg" | "md" | "sm" | "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
"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:
colorScheme
colorScheme
Color Schemes for ButtonGroup
are not implemented in the default theme. You can extend the theme to implement them.
string
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
Sizes for ButtonGroup
are not implemented in the default theme. You can extend the theme to implement them.
string
spacing
spacing
The spacing between the buttons
SystemProps["marginRight"]
'0.5rem'
variant
variant
Variants for ButtonGroup
are not implemented in the default theme. You can extend the theme to implement them.
string
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> ); }