React Vertical Timeline In SPFX

Introduction

 
Vertical Timeline is a graphical representation of a period of time, on which important events are marked. In this article we will learn how to implement vertical timeline in our SPFX webpart component.
 

Steps

 
Open a command prompt and create a directory for the SPFx solution.
 
md spfx-ReactTimeline
 
Navigate to the above-created directory.
 
cd spfx-ReactTimeline
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter for the default name (spfx-ReactTimeline in this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
 
Permissions to access web APIs
 
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
Selected choice - ReactTimeline
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
 
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
 
npm shrinkwrap
 
In the command prompt, type the below command to open the solution in the code editor of your choice.
  1. npm i react-vertical-timeline-component  
  2. npm i csstype    
  3. npm install @material-ui/icons 
VerticalTimeline Props
 
 animate={ Boolean }  Enable or disable animations on elements (default: true).
 className={ String }  Add extra class name to root div element.

 layout={ String }  Choose if you want '1-column' or '2-columns' (default: '2-columns').

 
VerticalTimelineElement Props
 
 className={ String } Add extra class name to root div element.

 contentArrowStyle={ Object }

 Add extra style to content arrow div element.
 contentStyle={ Object }

 Add extra style to content div element.

 date={ String }

 Date of the element.

 dateClassName={ String }

 Add extra class name to the element’s date.

 icon={ String }

 Icon of the element.

 iconClassName={ String }

 Add extra class name to the element’s icon.

 iconOnClick={ Function }

 onClick handler of the element’s icon.

 onTimelineElementClick={ Function }

 onClick handler of the vertical timeline element’s div.

 iconStyle={ Object }  Style of the element’s icon.

 position={ String }

 Position of the element (left or right).

 style={ Object }

 Add extra style to root div element.

 textClassName={ String }

 Add extra class name to the text container.
 
 in ReactTimeline.tsx,
  1. import * as React from 'react';  
  2. import { IReactTimelineProps } from './IReactTimelineProps';  
  3. import { VerticalTimeline, VerticalTimelineElement }  from 'react-vertical-timeline-component';  
  4. import 'react-vertical-timeline-component/style.min.css';  
  5. import SchoolIcon from '@material-ui/icons/School';  
  6. import WorkIcon from '@material-ui/icons/Work';  
  7. import StarIcon from '@material-ui/icons/Star';  
  8. import LoyaltyIcon from '@material-ui/icons/Loyalty';  
  9.   
  10. import './mystyle.css';  
  11. import * as CSS from 'csstype';   
  12. var divStyle: CSS.Properties<string | number> = {     
  13.   background: 'rgb(227, 227, 227)'      
  14.   };  
  15. export default class ReactTimeline extends React.Component<IReactTimelineProps, {}> {  
  16.   public render(): React.ReactElement<IReactTimelineProps> {  
  17.     return (  
  18.       <div style={divStyle}>  
  19.        <VerticalTimeline>  
  20.   <VerticalTimelineElement  
  21.     className="vertical-timeline-element--work"  
  22.     contentStyle={{ background: 'rgb(33, 150, 243)', color: '#fff' }}  
  23.     contentArrowStyle={{ borderRight: '7px solid  rgb(33, 150, 243)' }}  
  24.     date="2019 - 2020"  
  25.     iconStyle={{ background: 'rgb(33, 150, 243)', color: '#fff' }}  
  26.     icon={<LoyaltyIcon />}  
  27.   >  
  28.     <h3 className="vertical-timeline-element-title">Most Valuable Proffesional</h3>  
  29.     <h4 className="vertical-timeline-element-subtitle">MVP</h4>  
  30.     <p>  
  31.   1St time awarded as a C#Corner MVP  
  32.     </p>  
  33.   </VerticalTimelineElement>  
  34.    
  35.   <VerticalTimelineElement  
  36.     className="vertical-timeline-element--work"  
  37.     date="2019 - Till Date"  
  38.     iconStyle={{ background: 'rgb(33, 150, 243)', color: '#fff' }}  
  39.     icon={<WorkIcon />}  
  40.   >  
  41.     <h3 className="vertical-timeline-element-title">Software Engineer,Smartek21</h3>  
  42.     <h4 className="vertical-timeline-element-subtitle">SharePoint Developer</h4>  
  43.     <p>  
  44.     SharePoint Online ,Onpremise,M365 Development  
  45.     </p>  
  46.   </VerticalTimelineElement>  
  47.   <VerticalTimelineElement  
  48.     className="vertical-timeline-element--work"  
  49.     date="2016 - 2019"  
  50.     iconStyle={{ background: 'rgb(33, 150, 243)', color: '#fff' }}  
  51.     icon={<WorkIcon />}  
  52.   >  
  53.     <h3 className="vertical-timeline-element-title">Junior Software Engineer,Zylog Systems</h3>  
  54.     <h4 className="vertical-timeline-element-subtitle">SharePoint Developer</h4>  
  55.     <p>  
  56.       SharePoint Online ,Onpremise,M365 Development  
  57.     </p>  
  58.   </VerticalTimelineElement>  
  59.   <VerticalTimelineElement  
  60.     className="vertical-timeline-element--education"  
  61.     date="2012 - 2014"  
  62.     iconStyle={{ background: 'rgb(233, 30, 99)', color: '#fff' }}  
  63.     icon={<SchoolIcon />}  
  64.   >  
  65.     <h3 className="vertical-timeline-element-title">Master of Engineering</h3>  
  66.     <h4 className="vertical-timeline-element-subtitle">M.E.</h4>  
  67.     <p>  
  68.       Computer and Communication  
  69.     </p>  
  70.   </VerticalTimelineElement>  
  71.   <VerticalTimelineElement  
  72.     className="vertical-timeline-element--education"  
  73.     date="2009 - 2012"  
  74.     iconStyle={{ background: 'rgb(233, 30, 99)', color: '#fff' }}  
  75.     icon={<SchoolIcon />}  
  76.   >  
  77.     <h3 className="vertical-timeline-element-title">Bachelor of Engineering</h3>  
  78.     <h4 className="vertical-timeline-element-subtitle">B.E.</h4>  
  79.     <p>  
  80.       Computer Science and Engineering  
  81.     </p>  
  82.   </VerticalTimelineElement>  
  83.   <VerticalTimelineElement  
  84.     className="vertical-timeline-element--education"  
  85.     date="2007 - 2009"  
  86.     iconStyle={{ background: 'rgb(233, 30, 99)', color: '#fff' }}  
  87.     icon={<SchoolIcon />}  
  88.   >  
  89.     <h3 className="vertical-timeline-element-title">Diploma in Computer Engineering</h3>  
  90.     <h4 className="vertical-timeline-element-subtitle">Diploma Degree</h4>  
  91.     <p>  
  92.       Computer Technology  
  93.     </p>  
  94.   </VerticalTimelineElement>  
  95.   <VerticalTimelineElement  
  96.     iconStyle={{ background: 'rgb(16, 204, 82)', color: '#fff' }}  
  97.     icon={<StarIcon />}  
  98.   />  
  99. </VerticalTimeline>  
  100.       </div>  
  101.     );  
  102.   }  

 in mystyle.css,
  1. .vertical-timeline::before {  
  2.     content"";  
  3.     positionabsolute;  
  4.     top: 0px;  
  5.     left: 18px;  
  6.     height100%;  
  7.     width4px;  
  8.     background:black!important;  

Expected Output
 
 

Conclusion

 
In this article, we learned how to implement Vertical timeline in our spfx component. I hope this helps someone. Happy coding :)