DEV Community

Cover image for How to create a beautiful progress bar using bootstrap and Contrast
Speedwares for Devwares

Posted on • Originally published at devwares.com on

How to create a beautiful progress bar using bootstrap and Contrast

How to create linear and circular progress bars in react.

Progress bar is a component which displays progress of a process in which user are involved. Their color, shape, and animation can be customized with the contrast progress bar component.
Contrast also known as CDBReact is a react library which is an Elegant UI kit with full bootstrap support that has reusable components for building mobile-first, responsive websites, and web apps.

Prerequisites

The progress bars would be built using React, Bootstrap, and CDBReact. You don’t need to have any previous knowledge of CDBReact but the following are necessary:

  • JavaScript knowledge
  • Basic React Knowledge
  • Basic Bootstrap knowledge
  • NPM installed

The circular progress bar should look like the image below

Alt Text

Setup

First Check that you have node installed. To do this, run the following command in your terminal.

Code

node-v
Enter fullscreen mode Exit fullscreen mode

This should show you the current version of node you have installed on your machine.

If you don’t have nodejs installed, download it here.

Installing node also installs npm on your PC but you can still confirm using npm-v. Now that we have node installed, we can start up our React project by going to the directory of our choice and running

Code:

npx create-react-app progressbar-app
Enter fullscreen mode Exit fullscreen mode

I named the project progressbar-app but you can use any name of your choice.

Installing CDBReact-pro

In order to use the date-picker you need to use the pro version. And you can Install the cdbreact-pro package in your project (we recommend adding the file to the root of the project.) by running

npm install --save ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

yarn add ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Circular progress bar

Let us go ahead to create a file named circularpro.js which would contain our progress bar component. Import the various progress bar components that we’ll be using.

Code:

import React from "react";
import { CDBCircularProgress, CDBContainer } from "cdbreact-pro";
Enter fullscreen mode Exit fullscreen mode

in the code above, we import React from react and two components from the CDBReact which are:

  • CDBContainer
  • CDBCircularProgress.

Code:


export const Progress = () => {
  return (
    <CDBContainer>
            <CDBCircularProgress
              value={25}
              max={100}
              min={0}
              text={`${25}%`}
            />
            <CDBCircularProgress
              value={50}
              max={100}
              min={0}
              text={`${50}%`}
              color="primary"
            />
            <CDBCircularProgress
              value={95}
              max={100}
              min={0}
              color="secondary"
              text={`${95}%`}
            />
            <CDBCircularProgress
              value={35}
              max={100}
              min={0}
              color="danger"
              text={`${35}%`}
            />
            <CDBCircularProgress
              value={47}
              max={100}
              min={0}
              color="info"
              text={`${47}%`}
            />
            <CDBCircularProgress
              value={100}
              max={100}
              min={0}
              color="success"
              text={`${100}%`}
            />
    </CDBContainer>
  );
};

export default Progress;
Enter fullscreen mode Exit fullscreen mode

In the code above, we added some styling to the circular progress bar to make it look appealing.

Now, we can render the component in the app.js.
Code:

import './App.css';
import tab from './ circularpro.js ';
import { BrowserRouter as Router } from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="App">
       <Progress  />
      </div>
    </Router>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

The page will look like the image below.

Alt Text

Linear progress bar

For the linear progress bar we will create a file named linearpro.js which would contain our progress bar component.

Installing CDBReact-pro

In order to use the date-picker you need to use the pro version. And you can Install the cdbreact-pro package in your project (we recommend adding the file to the root of the project.) by running

npm install --save ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

yarn add ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Our linear progress bar should look like this image below when we are done

Alt Text

Import the various progress bar components that we’ll be using.

Code:

import React from "react";
import { CDBProgress, CDBContainer } from "cdbreact-pro";
Enter fullscreen mode Exit fullscreen mode

the code above imported React from react, the CDBProgress and the CDBContainer from cdbreact

Code:

export const Progress = () => {
  return (
    <CDBContainer>
              <CDBProgress
                value={10}
                text={`${10}%`}
                colors="primary"
              />
              <CDBProgress
                value={20}
                text={`${20}%`
                colors="secondary"
              />
              <CDBProgress
                value={70}
                text={`${70}%`}
                colors="success"
              />
              <CDBProgress
                value={40}
                text={`${40}%`}
                colors="danger"
              />
              <CDBProgress
                value={90}
                text={`${90}%`}
                colors="info"
              />
              <CDBProgress
                value={60}
                text={`${60}%`}
                colors="warning"
              />
    </CDBContainer>
  );
};
export default Progress;
Enter fullscreen mode Exit fullscreen mode

This code adds features to the linear progress bar and is enclosed in a CDBContainer component from contrast.

Code:

import './App.css';
import tab from './ linearpro.js ';
import { BrowserRouter as Router } from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="App">
       < Progress />
      </div>
    </Router>
  );
}
export default App;
Enter fullscreen mode Exit fullscreen mode

in this code we rendered our circular progress bar in our app.js file.

Your page should look like the image below

Alt Text

Discussion (0)