What is Chakra-UI?
Chakra UI is a modular component library that contains a simple API which gives the ability to make apps without having to worry about building the components themselves. It uses Emotion for styling and customization which allows developers to style inside the components with style props. The components are dark mode compatible as well and use a Box and Stack layout.
Installation
- I will use nextjs.
yarn create next-app <my-app>
- Install packages:
cd <my-app>
yarn add @chakra-ui/react @emotion/react@^11 @emotion/styled@^11 framer-motion@^6
Usage
You also need to setup ChakraProvider in your app's root:
import * as React from "react"
// 1. import `ChakraProvider` component
import { ChakraProvider } from "@chakra-ui/react"
function App() {
// 2. Use at the root of your app
return (
<ChakraProvider>
<App />
</ChakraProvider>
)
}
Setup Chakra-UI with next
- Wrap the Component component with the ChakraProvider component.
import { ChakraProvider } from '@chakra-ui/react'
function MyApp({ Component, pageProps }) {
return (
<ChakraProvider>
<Component {...pageProps} />
</ChakraProvider>
)
}
export default MyApp
Now we can use chakra-UI components.
Theming Example
Chakra UI provides an extendTheme function that deep merges the default theme with your customizations.
// 1. Import the extendTheme function
import { extendTheme } from "@chakra-ui/react"
// 2. Extend the theme to include custom colors, fonts, etc
const colors = {
brand: {
900: "#1a365d",
800: "#153e75",
700: "#2a69ac",
},
}
const theme = extendTheme({ colors })
// 3. Pass the `theme` prop to the `ChakraProvider`
function App() {
return (
<ChakraProvider theme={theme}>
<App />
</ChakraProvider>
)
}
How to import components
Always import the components and utilities as a named import from the @chakra-ui/react package.
import { Button, Text, Heading, Box, Link, useTheme } from '@chakra-ui/react'
const Index = () => {
return <Heading>Heading 1</Heading>
}
export default Index
Custom styles
There are two ways to customize the styles.
- Style Props: With style props, you can use almost any CSS property as props. See all the available style props.
const Index = () => {
return (
<Heading color='red' fontSize='5rem'>
Heading 1
</Heading>
)
}
- SX prop: With sx prop, you can use any custom style as an object. All the property name has to be camelcase.
const Index = () => {
return (
<Heading
sx={{
color: 'red',
fontSize: '5rem',
}}
>
Heading 1
</Heading>
)
}
Color Mode Example
ChakraProvider automatically uses color mode when used at the root of the app. By default, most of Chakra's component are dark mode compatible. To handle color mode manually in your application, use the useColorMode or useColorModeValue hooks.
useColorMode:
function Example() {
const { colorMode, toggleColorMode } = useColorMode()
return (
<header>
<Button onClick={toggleColorMode}>
Toggle {colorMode === "light" ? "Dark" : "Light"}
</Button>
</header>
)
}
Light mode
Dark mode
Commonly Used Components
Here are some of the components that I use most.
This is just a brief overview of Chakra UI and some of its features. On the official site, you can read more on color modes, gradients, global styles, and more.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in frontend web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project.
Contacts
Twitter: @Dev_suite
Instagram: @Dev_suite
Top comments (0)