Theming components with Chakra UI
📅 2023-03-06
🕑 3 min read
What is Chakra UI?
#
Chakra UI is a component library that allows you to build accessible, reusable, and themeable components.
How to theme components with Chakra UI
#
There are two types of components in Chakra UI.
Single-part components are components that have a single part, such as a Button
.
Multi-part components are components that have multiple parts, such as a Input
.
Single-part components
#
Single part components are those that can be styled as a whole, without the need to target individual parts. Examples of these include buttons, badges, and progress bars. To style these components, you can use the defineStyle
and defineStyleConfig
functions imported from @chakra-ui/react
.
1// src/theme/components/button.ts2import { defineStyle, defineStyleConfig } from '@chakra-ui/react'34const outline = defineStyle({5 border: '2px dashed', // change the appearance of the border6 borderRadius: 0, // remove the border radius7 fontWeight: 'semibold', // change the font weight8})910export const buttonTheme = defineStyleConfig({11 variants: { outline },12})
This code defines a Button
component with two variants, primary
and secondary
. You can customize the styles of each variant by adding or modifying the properties in the variants
object.
Multi-part components
#
Multipart components are those that have different parts that need to be styled separately. Examples of these include inputs, checkboxes, and radio buttons. To style these components, you can use the inputAnatomy object imported from @chakra-ui/anatomy
and the createMultiStyleConfigHelpers
function imported from @chakra-ui/react
.
For instance, to style an input, you can use the following code:
1// src/theme/components/input.ts2import { inputAnatomy } from '@chakra-ui/anatomy'3import { createMultiStyleConfigHelpers } from '@chakra-ui/react'45const { definePartsStyle, defineMultiStyleConfig } =6 createMultiStyleConfigHelpers(inputAnatomy.keys)78const baseStyle = definePartsStyle({9 // define the part you're going to style10 field: {11 fontFamily: 'mono', // change the font family12 color: 'teal.500', // change the input text color13 },14 addon: {15 bg: 'gray.100', // change the background color of the addon16 },17})1819export const inputTheme = defineMultiStyleConfig({ baseStyle })
This code defines an Input
component with two parts, field
and addon
. You can customize the styles of each part by adding or modifying the properties in said part. You can also define variants and default props in the same way as for single part components.
1// src/theme/index.ts2import { extendTheme } from '@chakra-ui/react'3import { inputTheme } from './components/input'4import { buttonTheme } from './components/button'56export const theme = extendTheme({7 components: {8 Input: inputTheme9 Button: buttonTheme10 },11})
And last but not least, you need to add these custom to your theme
object. You can do this by importing the extendTheme
function from @chakra-ui/react
and adding the components
object to the theme
object.
Conclusion
#
Are you feeling inspired and want to step into the open source world? If so, you can contribute to Chakra UI by adding theming documentation for one of the components.