Chakra UI Intro
Chakra UI is a simple, modular and accessible component library that gives you the building blocks you need to build your React applications.
Installation
For Vite Project, refer to Chakra UI Vite for installtion instructions. You need to install the ncecessary dependencies, add snippets, and also update vite.config.js.
Remember to add Provider at the root of your app. It can be added in main.jsx.
1 | import { Provider } from "@/components/ui/provider" |
after that you can use Chakra components in your app.
1 | import { Button, HStack } from "@chakra-ui/react" |
Box Component
The most abstract styling component in Chakra UI on top of which all other Chakra UI components are built. see Box Component for more details.
1 | import { Box } from "@chakra-ui/react" |
Container Component
Used to constrain a content’s width to the current breakpoint, while keeping it fluid. see Container Component for more details.
The default maxWidth is 8xl which maps to 90rem (1440px). You can change the maxWidth by using the maxWidth
prop. The default maxWidth is 8xl which maps to 90rem (1440px). You can change the maxWidth by using the maxWidth
prop.
1 | <Container maxW="8xl" bgColor="blue.200" p="1em" borderRadius="md" > |
Use the fluid
prop to make the container stretch to fill the width of its parent.
1 | <Container maxW="8xl" bgColor="blue.200" p="1em" borderRadius="md" fluid> |
Flex Component
Flex is a layout component that allows you to create flexible and responsive layouts using CSS flexbox. see Flex Component for more details.
1 | <Flex direction="row" align="baseline" justify="center" gap="4" > |
Grid/SimpleGrid Component
Grid is a layout component that allows you to create flexible and responsive layouts using CSS grid. see Grid Component for more details.
1 | <Grid templateColumns="repeat(3, 1fr)" gap={4}> |
SimpleGrid provides a friendly interface to create responsive grid layouts with ease.
1 | <SimpleGrid m="1em" columns="3" gap="4px"> |
Stack Component
Stack is a layout component that allows you to create flexible and responsive layouts using CSS flexbox. see Stack Component for more details. You can use HStack
for horizontal stack. You can use VStack
for vertical stacks.
1 | <Stack spacing={4} direction="row" align="baseline"> |
Text Component
The Text component is used to display text in your application. It is a simple component that allows you to style text using props. see Text Component for more details.
1 | <Text fontSize="2xl" fontWeight="bold" color="blue.500"> |
Button Component
The Button component is used to display a button in your application. It is a simple component that allows you to style buttons using props. see Button Component for more details.
1 | import { Button } from "@chakra-ui/react" |
Card Component
The Card component is used to display a card in your application. It is a simple component that allows you to style cards using props. see Card Component for more details.
1 | import { Card, CardHeader, CardBody, CardFooter } from "@chakra-ui/react" |
Icons Component
Used to display an svg icon. see Icons for more details.
Chakra doesn’t provide any icons out of the box. Use popular icon libraries like react-icons
1 | import { Icon } from "@chakra-ui/react" |
Responsive Design
See Responsive Design for more details on responsive design.
Chakra defines 5 breakpoints
1 | const breakpoints = { |
1 | <Text fontSize="2em" fontWeight={{ base: "medium", lg: "bold" }}>Responsive Text</Text> |
A responsive text that is 2em on all screen sizes, and bold on large screens.
Padding and Margin
Chakra use properties to style components.
See Spacing for more details on padding and margin.
use p
or padding
for padding. px
or paddingX
for horizontal padding. py
or paddingY
for vertical padding.
m
or margin
for margin. mx
or marginX
for horizontal margin. my
or marginY
for vertical margin.
1 | import { Box } from "@chakra-ui/react" |
Sizing
see Sizing for more details on sizing.
use w
or width
for width. h
or height
for height. minW
or minWidth
for minimum width. maxW
or maxWidth
for maximum width. minH
or minHeight
for minimum height. maxH
or maxHeight
for maximum height.
1 | import { Box } from "@chakra-ui/react" |
Background
See Background for more details on background.
use bg
or background
for background color. bgImage
for background image. bgSize
for background size. bgPosition
for background position. bgRepeat
for background repeat.
1 | import { Box } from "@chakra-ui/react" |
Virtual Color
Create color placeholders for better theming and customization. see Virtual Color for more details.
The colorPalette
property is how you create virtual color.
1 | <Button colorPalette="blue"> |
You can also use bg to set the background color of the button. The colorPalette property will be used as the default color for the button, and the bg property will override it.
1 | <Button colorPalette="blue" bg={{ base: "colorPalette.500", _hover: "colorPalette.800" }}> |
Design Token
Design tokens are the platform-agnostic way to manage design decisions in your application or website. It is a collection of attributes that describe any fundamental/atomic visual style. Each attribute is a key-value pair.
Chakra UI provides a set of color tokens that can be used to style your components. The color tokens are defined in the theme file. You can use the color tokens to style your components using the color
prop. see Color Token for more details.
Color token - here “blue.500” and “gray.100” are color tokens. “md” is a design token that is used to set the border radius of the box.
1 | import { Box } from "@chakra-ui/react" |
Semantic token - there are many semantic tokens. It is recommended to use semantic tokens for styling the components.
For color semantic tokens. see Color Semantic Tokens for more details.
1 | <Container color="fg" bg="bg.muted"> |
Theme
Dark Mode
see dark mode
run the following command to add the dark mode snippet to your project.
1 | npx @chakra-ui/cli snippet add color-mode |
main.tsx
1 | import { ColorModeProvider } from './components/ui/color-mode' |
Adding darkmode toggle
1 | import { ColorModeButton } from "@/components/ui/color-mode" |
Add Sematic Tokens
src/theme.ts
1 | import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react" |
After defining semantic tokens, use the Chakra CLI to generate theme typings for your tokens.
1 | npx @chakra-ui/cli typegen ./src/theme.ts |
To use the theme in main.jsx
1 | import theme from './theme.ts' |