GistTree.Com
Entertainment at it's peak. The news is by your side.

Installing and Using Chakra UI with React

0

Introduction

When in contrast to assorted React UI libraries enjoy React Bootstrap, Cloth UI, and a lot others., Chakra UI provides you worthy extra vitality to style and customize formulation.

Whenever you happen to are partial to Tailwind CSS, you can admire Chakra UI because it follows the identical minimalistic and modular plan as Tailwind CSS.

In this files, we can focus on about how to set up, import, and employ formulation from Chakra UI. We are in a position to employ Chakra UI version 0.8 since version 1.0 is yet to be released officially.

Installation

To set up chakra-ui, high-tail the next tell on your project’s root directory.

1
npm set up @chakra-ui/core @emotion/core @emotion/styled emotion-theming

bash

Chakra UI makes employ of Emotion for handling aspect styles. As that you would possibly gape, you bask in got to set up ogle dependencies your self as they make now no longer arrive with chakra-ui by default.

Next, wrap your React app in a ThemeProvider. Adjust your index.js enjoy this.

1
2
3
4
5
6
7
8
9
10
11
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { ThemeProvider } from "@chakra-ui/core";

ReactDOM.render(
  <ThemeProvider>
    <App />
  ThemeProvider>,
  file.getElementById("root")
);

JSX

Importing Box Element

In this share, we can import the Box aspect from chakra-ui.

One among the coolest aspects of chakra-ui is that that you would possibly employ shorthand for props. As an instance, as one more of width, that you would possibly employ w, which is ready to work the identical.

First, import Box from chakra-ui in App.js and employ all of it the plan throughout the App() feature.

1
2
3
4
5
6
7
8
9
10
11
12
import React from "react";
import { Box } from "@chakra-ui/core";

export default feature App() {
  return (
    <div>
      <Box w="400px" h="400px">
        Hi there World!
      Box>
    div>
  );
}

JSX

Within the above code, w and h are shorthand for width and peak respectively.

Here is how this could well perchance likely also honest seek.

initial box

Styling Box Element

The Box aspect in the above instance doesn’t seek enjoy a box, precise some text in some nook of the app. Add some background colour using bg shorthand prop.

1
2
3
<Box bg="lavender" w="400px" h="400px">
  Hi there World!
Box>

JSX

Here is how this could well perchance likely also honest seek.

lavender backgorund

Potentialities are you’ll well perchance likely now differentiate the 400×400 box from the background. Next, style the text and horizontally align it in the heart of the box.

1
2
3
4
5
6
7
8
9
10
11
<Box
  bg="lavender"
  color="#2d383c"
  fontSize="2rem"
  textAlign="center"
  fontFamily="Consolas"
  w="400px"
  h="400px"
>
  Hi there World!
Box>

JSX

You passed colour, textAlign, fontSize and fontFamily prop to style the text all the plan throughout the box. Here 1rem is the identical as 16px.

Here is how this could well perchance likely also honest seek.

styled text

Now, add a border to the box using the border prop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<Box
  border="1px"
  rounded="10px"
  borderColor="gray.300"
  boxShadow="md"
  bg="lavender"
  color="#2d383c"
  fontSize="2rem"
  textAlign="center"
  fontFamily="Consolas"
  w="400px"
  h="400px"
>
  Hi there World!
Box>

JSX

Here rounded is shorthand for border-radius, and boxSahdow is shorthand for box-shadow. Potentialities are you’ll well perchance likely bask in frail md as the label of boxShadow prop, which is an connected to 16px.

Here is how this could well perchance likely also honest seek.

border

To add some margin and padding, employ the shorthand prop p and m. Potentialities are you’ll well perchance likely furthermore employ px and py the attach px sets the padding for left and factual. In an identical style, py sets it for high and bottom.

Also, commerce the text Hi there World! to Lorem Ipsum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Box
  m="8px"
  p="8px"
  border="1px"
  rounded="10px"
  borderColor="gray.300"
  boxShadow="md"
  bg="lavender"
  color="#2d383c"
  fontSize="2rem"
  textAlign="center"
  fontFamily="Consolas"
  w="400px"
  h="400px"
>
  Lorem ipsum dolor sit down amet, consectetur adipisicing elit. Molestias aperiam
  doloremque exercitationem saepe, sed modi odit officia illum iste vel? Rerum
  dignissimos pariatur, odit impedit iste aperiam facere atque iure!{" "}
Box>

JSX

Lorem Ipsum

As that you would possibly gape, the text is overflowing the Box aspect; that you would possibly fix this by passing overflow="hidden" prop to Box.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<Box
  m="8px"
  p="8px"
  border="1px"
  rounded="10px"
  borderColor="gray.300"
  boxShadow="md"
  bg="lavender"
  color="#2d383c"
  fontSize="2rem"
  textAlign="center"
  fontFamily="Consolas"
  w="400px"
  h="400px"
  overflow="hidden"
>
  Lorem ipsum...
Box>

JSX

Here is how this could well perchance likely also honest seek.
Overflow Hidden

isTruncated is one other cool prop that you would possibly pass to truncate long texts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<Box
  m="8px"
  p="8px"
  border="1px"
  rounded="10px"
  borderColor="gray.300"
  boxShadow="md"
  bg="lavender"
  color="#2d383c"
  fontSize="2rem"
  textAlign="center"
  fontFamily="Consolas"
  w="400px"
  h="400px"
  isTruncated
>
  Lorem ipsum ....
Box>

JSX

Here is how this could well perchance likely also honest seek.
isTruncated

Adding Image to Box

In this share, you can add an image to the Box aspect in the above instance. For this, you can employ the Image and Textual protest formulation.

Import Image and Textual protest on your App.js.

1
import { Box, Image, Textual protest } from "@chakra-ui/core";

JSX

Now add these formulation all the plan throughout the Box aspect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<Box
  m="8px"
  p="8px"
  border="1px"
  rounded="10px"
  borderColor="gray.300"
  boxShadow="md"
  bg="lavender"
  color="#2d383c"
  fontSize="2rem"
  textAlign="center"
  fontFamily="Consolas"
  w="400px"
  h="400px"
>
  <Image
    rounded="0.5rem"
    src="https://finalspaceapi.com/img/gary_goodspeed.webp"
    alt="Gary Goodspeed"
  />
  <Text>Gary GoodspeedText>
Box>

JSX

Here is how this could well perchance likely also honest seek.
image

What if there are a pair of containers one after one other?

Copy and paste this box a pair of events.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<div>
  <Box>
    <Image
      rounded="0.5rem"
      src="https://finalspaceapi.com/img/gary_goodspeed.webp"
      alt="Gary Goodspeed"
    />
    <Text>Gary GoodspeedText>
  Box>

  <Box>
    <Image
      rounded="0.5rem"
      src="https://finalspaceapi.com/img/gary_goodspeed.webp"
      alt="Gary Goodspeed"
    />
    <Text>Gary GoodspeedText>
  Box>

  <Box>
    <Image
      rounded="0.5rem"
      src="https://finalspaceapi.com/img/gary_goodspeed.webp"
      alt="Gary Goodspeed"
    />
    <Text>Gary GoodspeedText>
  Box>
div>

JSX

Here is how this could well perchance likely also honest seek.

multiple boxes

Potentialities are you’ll well perchance likely construct the layout of this card machine responsive by using the SimpleGrid aspect. First, import the SimpleGrid aspect on your App.js.

1
import { Box, Image, Textual protest, SimpleGrid } from "@chakra-ui/core";

JSX

Now wrap your total Box formulation interior this SimpleGrid aspect.

Scoot the minChildWidth prop to this SimpleGrid aspect, which is ready to alter the association of the containers in step with minChildWidth.

Here the containers are of 400x400px dimension, so that that you would possibly pass 410px as minChildWidth.

1
2
3
4
5
6
7
8
9
10
11
<SimpleGrid minChildWidth="410px">
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
  <Box> ...Box>
SimpleGrid>

JSX

Here is how this could well perchance likely also honest seek.

simple grid

There is light so worthy extra that you would possibly well perchance make to customize and magnificence formulation in Chakra UI.

Conclusion

In this files, we talked about how to set up chakra-ui in a React app. We furthermore lined how to import and customize assorted formulation from chakra-ui.

Chakra UI is the supreme selection for your React app whenever you ought to customize formulation and favor your app to bask in a special seek, which is troublesome to halt in assorted React UI libraries enjoy React Bootstrap, Cloth UI, and a lot others.

Here are some additional resources that would possibly well perchance likely also be priceless.

    Read More

    Leave A Reply

    Your email address will not be published.