Timelines are often used in user interfaces to illustrate a step-by-step procedure. It can describe to the user which stage of the process they currently are and what the further tasks are. Material UI labs module provide a Timeline component along with some other utility components to make this very easy to include in our React app.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app gfg
-
Step 2: After creating your project folder i.e. gfg, move to it using the following command:
cd gfg
-
Step 3: After creating the ReactJS application, Install the material-ui modules using the following command:
npm install @material-ui/core npm install @material-ui/lab
As an example, we’ll create a Stages component that illustrates the different stages an Article at neveropen goes through in form of a Timeline. Create a file stages.js in the src folder where we’ll define this component.
Project Structure: It will look like the following.
Timeline component in Material-UI
The Timeline component in Material UI labs displays items in chronological order and gives the developers freedom to alter how it’s displayed up to some extent. It has some useful props:
- align: The textual content can be posted at the left, right, or alternating to the timeline.
- color: Used to denote the color of the timeline dot at that stage. It’s the prop of the TimelineDot component which we use inside the Timeline component.
stages.js
import React from 'react' ; import Timeline from '@material-ui/lab/Timeline' ; import TimelineItem from '@material-ui/lab/TimelineItem' ; import TimelineSeparator from '@material-ui/lab/TimelineSeparator' ; import TimelineConnector from '@material-ui/lab/TimelineConnector' ; import TimelineContent from '@material-ui/lab/TimelineContent' ; import TimelineDot from '@material-ui/lab/TimelineDot' ; import { Paper } from '@material-ui/core' ; const paperstyle={ padding: '8px 1px' , textAlign: 'center' , } export default function Stages() { return ( <Timeline align= "alternate" > <TimelineItem> <TimelineSeparator> <TimelineDot color= "primary" /> <TimelineConnector /> </TimelineSeparator> <TimelineContent> <Paper elevation={3} style={paperstyle}>Step 1 : Write </Paper> </TimelineContent> </TimelineItem> <TimelineItem> <TimelineSeparator> <TimelineDot color= "primary" /> <TimelineConnector /> </TimelineSeparator> <TimelineContent> <Paper elevation={3} style={paperstyle}>Step 2 : Draft </Paper> </TimelineContent> </TimelineItem> <TimelineItem> <TimelineSeparator> <TimelineDot color= "primary" /> <TimelineConnector /> </TimelineSeparator> <TimelineContent> <Paper elevation={3} style={paperstyle}>Step 3 : Pending </Paper> </TimelineContent> </TimelineItem> <TimelineItem> <TimelineSeparator> <TimelineDot /> </TimelineSeparator> <TimelineContent> <Paper elevation={3} style={paperstyle}>Step 4 : Publish </Paper> </TimelineContent> </TimelineItem> </Timeline> ); } |
Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React, { Component } from 'react' ; import CssBaseline from '@material-ui/core/CssBaseline' ; import Container from '@material-ui/core/Container' ; import Typography from '@material-ui/core/Typography' ; import Stages from './stages' ; class GFG extends Component { render() { return ( <React.Fragment> <CssBaseline /> <br></br> <Container maxWidth= "sm" > <Typography component= "h1" variant= "h1" align= "center" gutterBottom> Geeks for Geeks </Typography> <br /> <Typography component= "h3" variant= "h3" align= "center" gutterBottom> Timeline of an Article </Typography> </Container> <Container maxWidth= "sm" > <Stages></Stages> </Container> </React.Fragment> ); } } export default GFG; |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output: