React Responsive Timeline Component

In the Create a responsive timeline with HTML and CSS topic, we already learn how to create a Responsive Timeline with HTML and CSS.

In this snippet, we will create a React Responsive Timeline Component from what we created in that topic.

Step 1: Create a Timeline Component with the codes below:

 
import React from "react";

import "./styles.css";

function Timeline({ events = [] }) {
  return (
    <div className="timeline">
      {events.map((item, i) => {
        return (
          <div
            key={i}
            className={i % 2 ? "timeline-item left" : "timeline-item right"}
          >
            <div className="content">
              <h2>{item.datetime}</h2>
              <p>{item.event}</p>
            </div>
          </div>
        );
      })}
    </div>
  );
}

export default Timeline;

Copy this code


Step 2: Create style file call Styles.css:

* {
  box-sizing: border-box;
}

.timeline {
  position: relative;
  max-width: 1200px;
  margin: 0 auto;
}

.timeline::after {
  content: "";
  position: absolute;
  width: 3px;
  background-color: white;
  top: 0;
  bottom: 0;
  left: 50%;
  margin-left: -2px;
}

.timeline-item {
  padding: 10px 40px;
  position: relative;
  background-color: inherit;
  width: 50%;
}

.timeline-item::after {
  content: "";
  position: absolute;
  width: 15px;
  height: 15px;
  right: -9px;
  background-color: #4caf50;
  border: 2px solid #fff;
  top: 22px;
  border-radius: 50%;
  z-index: 1;
}

.left {
  left: 0;
}

.right {
  left: 50%;
}

.left::before {
  content: " ";
  height: 0;
  position: absolute;
  top: 22px;
  width: 0;
  z-index: 1;
  right: 30px;
  border: medium solid white;
  border-width: 10px 0 10px 10px;
  border-color: transparent transparent transparent white;
}

.right::before {
  content: " ";
  height: 0;
  position: absolute;
  top: 22px;
  width: 0;
  z-index: 1;
  left: 30px;
  border: medium solid white;
  border-width: 10px 10px 10px 0;
  border-color: transparent white transparent transparent;
}

.right::after {
  left: -10px;
}

.content {
  padding: 20px 30px;
  background-color: white;
  position: relative;
  border-radius: 6px;
}

/* Responsive on screens less than 600px wide */
@media screen and (max-width: 600px) {
  .timeline::after {
    left: 31px;
  }

  .timeline-item {
    width: 100%;
    padding-left: 70px;
    padding-right: 25px;
  }

  .timeline-item::before {
    left: 60px;
    border: medium solid white;
    border-width: 10px 10px 10px 0;
    border-color: transparent white transparent transparent;
  }

  .left::after,
  .right::after {
    left: 21px;
  }

  .right {
    left: 0%;
  }
}

Copy this code


Step 3: Use in your app:

 
import React from "react";
import ReactDOM from "react-dom";

import Timeline from "./Timeline";

const timelineData = [
  {
    datetime: "2019",
    event: "Something happened in 2019"
  },
  {
    datetime: "2018",
    event: "Something happened in 2018"
  },
  {
    datetime: "2017",
    event: "Something happened in 2017"
  },
  {
    datetime: "2016",
    event: "Something happened in 2016"
  },
  {
    datetime: "2015",
    event: "Something happened in 2015"
  }
];

function App() {
  return (
    <div className="App">
      <Timeline events={timelineData} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Copy this code


Note: This example is very basic for easy to learning and easy to customize, you can modify more for your purpose.

© 2019 4codev

Created with love by Sil.