A11y: A label that describes the button
Icon Button
Icon button renders an icon within in a button.
Props#
aria-label
required
aria-label
required
string
icon
icon
The icon to be used in the button.
ReactElement<any, string | JSXElementConstructor<any>>
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
isRound
isRound
If true
, the button will be perfectly round. Else, it'll be slightly round
boolean
false
spinner
spinner
Replace the spinner component when isLoading
is set to true
ReactElement<any, string | JSXElementConstructor<any>>
Props#
aria-label
required
aria-label
required
A11y: A label that describes the button
string
icon
icon
The icon to be used in the button.
ReactElement<any, string | JSXElementConstructor<any>>
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
isRound
isRound
If true
, the button will be perfectly round. Else, it'll be slightly round
boolean
false
spinner
spinner
Replace the spinner component when isLoading
is set to true
ReactElement<any, string | JSXElementConstructor<any>>
The IconButton
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
IconButton
andButton
components share the same theming configuration. To create theming just for theIconButton
component, you can create a custom variant.
The properties that affect the theming of the IconButton
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})const xl = defineStyle({fontSize: 'xl',px: '6',h: '16',borderRadius: 'md',})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 },})
Adding a custom size#
Let's assume we want to include an extra large icon 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<IconButton size="xl">...</IconButton>
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 create a custom IconButton
variant. Here's how we can
do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const customIconButton = 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: { customIconButton },})// Now we can use the new `brandPrimary` variant// Like it was mentioned above, the `Button` component will share the same// theming configuration as the `IconButton` component.<IconButton variant="customIconButton">...</IconButton>
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 icon button:
<IconButton colorScheme='brand'>...</IconButton>
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:<IconButton size="lg" variant="outline" colorScheme="brand">...</IconButton>
Showcase#
import { ChakraProvider, IconButton, Box, useColorMode, SimpleGrid, Icon } from "@chakra-ui/react"; import * as React from "react"; import { createRoot } from "react-dom/client"; import "./styles.css"; import { FaSun, FaMoon, FaFacebook, FaTwitter, FaWhatsapp, FaTelegram } from "react-icons/fa"; import { theme } from "./theme"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <SimpleGrid gap={12} p={12} columns={2}> <IconButton variant="outline" aria-label="Twitter icon" icon={<Icon as={FaTwitter} />} /> <IconButton variant="solid" aria-label="Facebook icon" icon={<Icon as={FaFacebook} />} /> <IconButton variant="ghost" size="xl" colorScheme="whatsapp" aria-label="WhatsApp icon" icon={<Icon as={FaWhatsapp} />} /> <IconButton size="xl" variant="link" colorScheme="telegram" aria-label="WhatsApp icon" icon={<Icon as={FaTelegram} />} /> <IconButton size="xl" variant="customIconButton" aria-label="Color mode icon" icon={colorMode === "light" ? <FaSun /> : <FaMoon />} /> </SimpleGrid> <IconButton variant="solid" aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }