Creating a great user experience is never an easy task. So most of the time we prefer to use existing UI libraries to reduce the time and increase productivity. In this article, we are going to learn about
Chakra UI and also build a react application using
Chakra UI components. We are going to use some of the components of Chakra UI in our application to see how easy it is to build a great UI.
In this article, I am going to cover the below points,
- What is Chakra UI?
- Create a React App
- Setup Chakra UI in React App
- Create a Navbar with Chakra UI Components
- Adding Dark mode functionality
What is Chakra UI?
Chakra UI is a simple, modular and accessible component library that gives you all the building blocks you need to build your React applications.
Chakra UI is another great modular component library for React which uses Emotion and Styled System. It consists of a wide range of reusable React components that can make the life of a developer easy. Chakra UI has some design principals which you can check
here.
Special things about Chakra UI
- Chakra UI has out of the box support for themes. You can customize themes very easily.
- It has out of the box support for Dark Mode.
- It is based on Styled Systems. It has the support of Emotion a CSS-in-JS library.
- Chakra UI consists of lots of components
Create a React App
Prerequisites
- We should have a basic knowledge of HTML and ReactJS.
- Visual Studio Code installed
- Node and NPM installed
Open a command prompt or your favorite terminal and type the below command to create a react app.
- npx create-react-app react-chakarui-app
After successfully creating the app go to the new app.
To see how it look likes type the below command.
and it will start the development server and navigate you to http://localhost:3000/
You can see the default landing page,
Setup Chakra UI in React App
Install Chakra UI in our new react app and its peer dependencies,
- npm install @chakra-ui/core @emotion/core @emotion/styled emotion-theming
Open our new app in Visual Studio code. Chakra UI has great theming support so open index.ts and import ThemeProvider and CSSReset component and apply at the root level. Chakra UI exports a CSSReset that'll remove browser default styles.
- import React from 'react';
- import ReactDOM from 'react-dom';
- import './index.css';
- import App from './App';
- import * as serviceWorker from './serviceWorker';
- import { theme, ThemeProvider, CSSReset } from "@chakra-ui/core";
-
-
- ReactDOM.render(
- <ThemeProvider theme={theme}>
- <CSSReset />
- <React.StrictMode>
- <App />
- </React.StrictMode>
- </ThemeProvider>,
- document.getElementById('root')
- );
-
- serviceWorker.unregister();
Create a Navbar with Chakra UI Components
Now create a new React component called Navbar.js. To create navbar we are using below Chakra UI components,
Open Navbar.js file and add the below code,
- import React from "react";
- import {
- Box,
- Heading,
- Flex,
- Link
- } from "@chakra-ui/core";
-
- const MenuItems = ({ children }) => (
- <Link mt={{ base: 4, md: 0 }} mr={6} display="block">
- {children}
- </Link>
- );
-
- const Navbar = props => {
-
- return (
- <Flex
- as="nav"
- align="center"
- justify="space-between"
- wrap="wrap"
- padding="1.5rem"
- bg="gray.900"
- color="teal.300"
- borderBottom="1px solid black"
- {...props}
- >
- <Flex align="center" mr={5}>
- <Heading as="h1" size="lg" letterSpacing={"-.1rem"}>
- Chakra UI & React
- </Heading>
- </Flex>
-
- <Box
- display="flex"
- width="auto"
- alignItems="center"
- flexGrow={1}
- color="teal.300"
- >
- <MenuItems>Home</MenuItems>
- <MenuItems>Blogs</MenuItems>
- <MenuItems>About</MenuItems>
- <MenuItems>Contact</MenuItems>
- </Box>
- </Flex>
- );
- };
-
- export default Navbar;
Chakra UI provides the below layout utilities,
Box component is a very abstract component on top of which all other Chakra UI components are built. By default, it renders a div element. Flex is nothing but Box with display:flex. So with the help of Flex, we can arrange elements and assign proper spacing in it.
We have created MenuItems components which use Text component of Chakra UI to show text. Now open App.js and import Navbar component in it and also we are showing logos.
- import React from 'react';
- import logo from './logo.svg';
- import chakraui from './chakraui-logo.svg';
- import './App.css';
- import Navbar from './Navbar';
- import {
- Box,
- Heading
- } from "@chakra-ui/core";
-
- function App() {
- return (
- <div>
- <Navbar />
- <div>
- <div className="container" style={{ display: 'flex', height: '200px', marginTop:'100px',alignItems: 'center' }}>
- <div style={{ width: '20%' }}>
- <img src={logo} className="App-logo" alt="react-logo" />
- </div>
- <div style={{ flexGrow: '1' }}>
- <img src={chakraui} className="App-logo" alt="chakarui-logo" />
- </div>
- </div>
- </div>
- <Box color="teal.500">
- <Heading>React JS & Chakra UI App</Heading>
- </Box>
- </div>
- );
- }
-
- export default App;
Now run the app and see the output,
Adding Dark Mode functionality
Chakra UI has out of the box support for Dark mode. So to implement it Chakra UI provides a hook called useColorMode which we will use to changes the application's color mode. It stores the value in local storage and when the page is loaded it uses that value. To enable color mode within our app, we have to wrap our application in a ColorModeProvider.
Open index.js and import ColorModeProvider from the Chakra UI library and wrap our App component.
- import React from 'react';
- import ReactDOM from 'react-dom';
- import './index.css';
- import App from './App';
- import * as serviceWorker from './serviceWorker';
- import { theme, ThemeProvider, CSSReset, ColorModeProvider } from "@chakra-ui/core";
-
-
- ReactDOM.render(
- <ThemeProvider theme={theme}>
- <ColorModeProvider>
- <CSSReset />
- <React.StrictMode>
- <App />
- </React.StrictMode>
- </ColorModeProvider>
- </ThemeProvider>,
- document.getElementById('root')
- );
-
- serviceWorker.unregister();
Now open Navbar.js and import useColorMode hook from the library and also add one IconButton to toggle the theme.
- import React from "react";
- import {
- Box,
- Heading,
- Flex,
- Link,
- useColorMode,
- IconButton,
- } from "@chakra-ui/core";
-
- const MenuItems = ({ children }) => (
- <Link mt={{ base: 4, md: 0 }} mr={6} display="block">
- {children}
- </Link>
- );
-
- const Navbar = props => {
- const { colorMode, toggleColorMode } = useColorMode();
- return (
- <Flex
- as="nav"
- align="center"
- justify="space-between"
- wrap="wrap"
- padding="1.5rem"
- bg={colorMode === "light" ? "gray.900" : "teal.500"}
- color={colorMode === "light" ? "teal.300" : "white"}
- borderBottom="1px solid black"
- {...props}
- >
- <Flex align="center" mr={5}>
- <Heading as="h1" size="lg" letterSpacing={"-.1rem"}>
- Chakra UI & React
- </Heading>
- </Flex>
-
- <Box
- display="flex"
- width="auto"
- alignItems="center"
- flexGrow={1}
- color={colorMode === "light" ? "teal.300" : "white"}
- >
- <MenuItems>Home</MenuItems>
- <MenuItems>Blogs</MenuItems>
- <MenuItems>About</MenuItems>
- <MenuItems>Contact</MenuItems>
- </Box>
- <Box
- display="block"
- mt={{ base: 4, md: 0 }}
- >
- <IconButton
- bg="transparent"
- aria-label="toggle color mode"
- icon={colorMode === "light" ? "moon" : "sun"}
- onClick={toggleColorMode}
- color="white"
- />
- </Box>
- </Flex>
- );
- };
-
- export default Navbar;
Now run the app and check whether we can toggle the color mode.
Conclusion
In this article, I have explained about Chakra UI and how to integrate it in React applications. Also, I demonstrated how to create a simple navbar and added dark mode easily using Chakra UI. I really hope that you enjoyed this article, share it with friends, and please do not hesitate to send me your thoughts or comments.
Happy Coding!!