React Chrono

Modern Timeline Component for React

README

  


Build Status DeepScan grade Codacy Badge react-chrono
Snyk Vulnerabilities for GitHub Repo Depfu
npm bundle size
<!-- Try it on CodeSandbox!
Edit react-chrono -->

Features


- 🚥 Render timelines in three different modes (Horizontal, Vertical, Vertical-Alternating).
- 📺  Auto play the timeline with the slideshow mode.
- 🖼️  Display Images & Videos in the timeline with ease.
- 🔧  Render custom content easily.
- ⚡  Data driven API.
- 🎨  Customize colors with ease.
- 🎭  Use custom icons in the timeline.
- 💪  Built with Typescript.
- 🎨  Styled with emotion.

Table of Contents


  - Mode
  - 📺Media
  - Outline
  - 🎨Theme

⚡ Installation


  1. ``` js
  2. // install with yarn
  3. yarn add react-chrono

  4. // or with npm
  5. npm install react-chrono
  6. ```

Getting Started


Please make sure you wrap the component in a container that has a width and height.

When no mode is specified, the component defaults to HORIZONTAL mode. Please check props for all the available options.

  1. ``` js
  2.   import React from "react"
  3.   import { Chrono } from "react-chrono";

  4.   const Home = () => {
  5.     const items = [{
  6.       title: "May 1940",
  7.       cardTitle: "Dunkirk",
  8.       url: "http://www.history.com",
  9.       cardSubtitle:"Men of the British Expeditionary Force (BEF) wade out to..",
  10.       cardDetailedText: "Men of the British Expeditionary Force (BEF) wade out to..",
  11.       media: {
  12.         type: "IMAGE",
  13.         source: {
  14.           url: "http://someurl/image.jpg"
  15.         }
  16.       }
  17.     }, ...];

  18.     return (
  19.       <div style={{ width: "500px", height: "400px" }}>
  20.         <Chrono items={items} />
  21.       </div>
  22.     )
  23.   }
  24. ```

app-home

🚥Vertical Mode


To render the timeline vertically use the VERTICAL mode

  1. ``` js
  2. <div style={{ width: '500px', height: '950px' }}>
  3.   <Chrono items={items} mode="VERTICAL" />
  4. </div>
  5. ```

vertical-basic

🚥Vertical Alternating


In VERTICAL_ALTERNATING mode the timeline is rendered vertically with cards alternating between left and right side.

  1. ``` js
  2. <div style={{ width: '500px', height: '950px' }}>
  3.   <Chrono items={items} mode="VERTICAL_ALTERNATING" />
  4. </div>
  5. ```

app-tree

📺Slideshow


Play the timeline automatically with the slideShow mode. This prop enables the play button on the ui controls.

  1. ``` js
  2. <div style={{ width: '500px', height: '950px' }}>
  3.   <Chrono items={items} slideShow mode="VERTICAL_ALTERNATING" />
  4. </div>
  5. ```



Props


namedescriptiondefault
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
activeItemIndexSelects0
allowDynamicUpdateAllowsfalse
borderLessCardsRemovesfalse
buttonTextsCustomize|
cardHeightSets200
cardLessDisablesfalse
cardPositionHorizontalPositions|
cardWidthSets|
disableAutoScrollOnClickDisablesfalse
disableClickOnCircleDisablesfalse
disableNavOnKeyDisablesfalse
enableOutlineEnablesfalse
flipLayoutFlipsfalse
focusActiveItemOnLoadSettingfalse
fontSizesProperty|
hideControlsHidesfalse
itemWidthWidth300
itemsCollection[]
lineWidthProp3px
modeSets`VERTICAL_ALTERNATING`
onItemSelectedCallback|
onScrollEndUse|
scrollableMakestrue
showAllCardsHorizontalInfalse
slideItemDurationDuration5000
slideShowEnablesfalse
themeProp|
timelineCircleDimensionDimensionsfalse
useReadMoreEnablestrue
mediaHeightSets200
classNamesProp|

Mode


react-chrono supports three modes HORIZONTAL, VERTICAL and VERTICAL_ALTERNATING. No additional setting is required.

  1. ``` js
  2. <Chrono items={items} mode="HORIZONTAL" />
  3. ```

  1. ``` js
  2. <Chrono items={items} mode="VERTICAL" />
  3. ```

  1. ``` js
  2. <Chrono items={items} mode="VERTICAL_ALTERNATING" />
  3. ```

Timeline item Model


namedescriptiontype
------------------------------------------------------------------------------
titletitleString
cardTitletitleString
cardSubtitletextString
cardDetailedTextdetailedString
mediamediaObject
urlurlString

  1. ``` js
  2. {
  3.   title: "May 1940",
  4.   cardTitle: "Dunkirk",
  5.   cardSubtitle:
  6.     "Men of the British Expeditionary Force (BEF) wade out to a destroyer during the evacuation from Dunkirk.",
  7.   cardDetailedText: ["paragraph1", "paragraph2"],
  8. }
  9. ```

if you have a large text to display(via cardDetailedText) and want to split the text into paragraphs, you can pass an array of strings.

each array entry will be created as a paragraph inside the timeline card.

⌨Keyboard Navigation


The timeline can be navigated via keyboard.

- For `HORIZONTAL` mode use your LEFT RIGHT arrow keys for navigation.- For `VERTICAL` or `VERTICAL_ALTERNATING` mode, the timeline can be navigated via the UP DOWN arrow keys.- To easily jump to the first item or the last item in the timeline, use HOME or END keys.

To disable keyboard navigation set disableNavOnKey to true.

  1. ``` js
  2. <Chrono items={items} disableNavOnKey />
  3. ```

Scrollable


With the scrollable prop, you can enable scrolling on both VERTICAL and VERTICAL_ALTERNATING modes.

  1. ``` js
  2. <Chrono items={items} scrollable />
  3. ```

The scrollbar is not shown by default. To enable the scrollbar, pass an object with prop scrollbar to scrollable prop.

  1. ``` js
  2. <Chrono items={items} scrollable={{ scrollbar: true }} />
  3. ```

📺Media


Both images and videos can be embedded in the timeline.


Just add the media attribute to the Timeline Item model and the component will take care of the rest.

  To embed a image

  1. ``` js
  2. {
  3.   title: "May 1940",
  4.   cardTitle: "Dunkirk",
  5.   media: {
  6.     name: "dunkirk beach",
  7.     source: {
  8.       url: "http://someurl/image.jpg"
  9.     },
  10.     type: "IMAGE"
  11.   }
  12. }
  13. ```

  To embed a video

Videos start playing automatically when active and will be automatically paused when not active.


Like images, videos are also automatically hidden when not in the visible viewport of the container.

  1. ``` js
  2. {
  3.   title: "7 December 1941",
  4.   cardTitle: "Pearl Harbor",
  5.   media: {
  6.     source: {
  7.       url: "/pearl-harbor.mp4",
  8.       type: "mp4"
  9.     },
  10.     type: "VIDEO",
  11.     name: "Pearl Harbor"
  12.   }
  13. }
  14. ```

To embed YouTube videos, use the right embed url.

  1. ``` js
  2. {
  3.   title: "7 December 1941",
  4.   cardTitle: "Pearl Harbor",
  5.   media: {
  6.     source: {
  7.       url: "https://www.youtube.com/embed/f6cz9gtMTeI",
  8.       type: "mp4"
  9.     },
  10.     type: "VIDEO",
  11.     name: "Pearl Harbor"
  12.   }
  13. }
  14. ```

media

🛠Rendering custom content


The component also supports embedding custom content in the Timeline cards.

To insert custom content, just pass the blocked elements between the Chrono tags.

For e.g the below snippet will create 2 timeline items. Each div element is automatically converted into a timeline item and inserted into the timeline card.
The items collection is completely optional and custom rendering is supported on all 3 modes.

  1. ``` js
  2. <Chrono mode="VERTICAL">
  3.   <div>
  4.     <p>Lorem Ipsum. Lorem Ipsum. Lorem Ipsum</p>
  5.   </div>
  6.   <div>
  7.     <img src="<url to  a nice image" />
  8.   </div>
  9. </Chrono>
  10. ```

The items collection will also work nicely with any custom content that is passed.


The following snippet sets the the title and cardTitle for the custom contents.

  1. ``` js
  2. const items = [
  3.   { title: 'Timeline title 1', cardTitle: 'Card Title 1' },
  4.   { title: 'Timeline title 2', cardTitle: 'Card Title 2' },
  5. ];

  6. <Chrono mode="VERTICAL" items={items}>
  7.   <div>
  8.     <p>Lorem Ipsum. Lorem Ipsum. Lorem Ipsum</p>
  9.   </div>
  10.   <div>
  11.     <img src="<url to  a nice image" />
  12.   </div>
  13. </Chrono>;
  14. ```

🎭Custom icons for the Timeline


To use custom icons in the timeline, pass in the collection of images between the chrono tags wrapped in a container.

The icons are sequentially set (i.e) the first image you pass will be used as the icon for the first timeline item and so on.

Please make sure to pass in the image collection inside a container with a special className chrono-icons. This convention is mandatory as the component uses this class name to pick the images.

  1. ``` js
  2. <Chrono items={items} mode="VERTICAL_ALTERNATING">
  3.   <div className="chrono-icons">
  4.     <img src="image1.svg" alt="image1" />
  5.     <img src="image2.svg" alt="image2" />
  6.   </div>
  7. </Chrono>
  8. ```

custom icons also works if you are rendering custom content inside the cards.


  1. ``` js
  2. <Chrono mode="VERTICAL" items={items}>
  3.   <div>
  4.     <p>Lorem Ipsum. Lorem Ipsum. Lorem Ipsum</p>
  5.   </div>
  6.   <div>
  7.     <img src="<url to  a nice image" />
  8.   </div>
  9.   <div className="chrono-icons">
  10.     <img src="image1.svg" alt="image1" />
  11.     <img src="image2.svg" alt="image2" />
  12.   </div>
  13. </Chrono>
  14. ```

Slideshow mode


Slideshow can be enabled by setting the slideShow prop to true. You can also set an optional slideItemDuration that sets the time delay between cards.

setting this prop enables the play button in the timeline control panel.

  1. ``` js
  2. <Chrono items={items} slideShow slideItemDuration={4500} />
  3. ```

Outline


With enableOutline prop you can enable outline on the timelines and quickly jump to a specific timeline item.
The outlines are only supported on VERTICAL and VERTICAL_ALTERNATING modes.

  1. ``` js
  2. <Chrono items={items} enableOutline />
  3. ```

media

Item Width


The itemWidth prop can be used to set the width of each individual timeline sections. This setting is applicable only for the HORIZONTAL mode.

🎨Theme


Customize colors with the theme prop.

  1. ``` js
  2. <Chrono
  3.   items={items}
  4.   theme={{
  5.     primary: 'red',
  6.     secondary: 'blue',
  7.     cardBgColor: 'yellow',
  8.     cardForeColor: 'violet',
  9.     titleColor: 'black',
  10.     titleColorActive: 'red',
  11.   }}
  12. />
  13. ```

Customize Font sizes


Use the fontSizes prop to customize the font sizes of the timeline card.

  1. ``` js
  2. <Chrono
  3.   items={data}
  4.   mode="HORIZONTAL"
  5.   fontSizes={{
  6.     cardSubtitle: '0.85rem',
  7.     cardText: '0.8rem',
  8.     cardTitle: '1rem',
  9.     title: '1rem',
  10.   }}
  11. ></Chrono>
  12. ```

Customize alt text for buttons


With the buttonTexts prop, you can change the button's alt text.

  1. ``` js
  2. <Chrono
  3.   items={data}
  4.   mode="HORIZONTAL"
  5.   buttonTexts={{
  6.     first: 'Jump to First',
  7.     last: 'Jump to Last',
  8.     next: 'Next',
  9.     previous: 'Previous',
  10.   }}
  11. ></Chrono>
  12. ```

Defaults

| Property   | Value            |
| :--------- | :--------------- |
| first    | 'Go to First'    |
| last     | 'Go to Last'     |
| next     | 'Next'           |
| play     | 'Play Slideshow' |
| previous | 'Previous'       |

📦CodeSandbox Examples



📚Storybook


Deep dive into wide variety of examples hosted as a Storybook.

-

🔨Build Setup


  1. ``` sh
  2. # install dependencies
  3. pnpm install

  4. # start dev
  5. pnpm dev

  6. # run css linting
  7. pnpm lint:css

  8. # eslint
  9. pnpm eslint

  10. # prettier
  11. pnpm lint

  12. # package lib
  13. pnpm rollup
  14. ```

🧪Tests


  1. ``` js
  2.   # run unit tests
  3.   pnpm test

  4.   # run cypress tests
  5.   pnpm cypress:test
  6. ```

🤝Contributing


2. Create your feature branch (git checkout -b new-feature)
3. Commit your changes (git commit -am 'Add feature')
4. Push to the branch (git push origin new-feature)
5. Create a new Pull Request

🧱Built with


- Styled with emotion.

Meta


Huge thanks to BrowserStack for the Open Source License!

Distributed under the MIT license. See LICENSE for more information.

Prabhu Murthy – @prabhumurthy2 – prabhu.m.murthy@gmail.com

Buy Me A Coffee



[react]: https://reactjs.org
[typescript]: https://typescriptlang.org
[emotion]: https://emotion.sh/

Contributors ✨


Thanks goes to these wonderful people (emoji key):





Alois

📖

Koji

📖

Alexandre Girard

💻

Ryuya

📖

Taqi Abbas

💻

megasoft78

💻

Eric(书生)

💻






This project follows the all-contributors specification. Contributions of any kind welcome!