0.3.4 • Published 2 years ago

react-headless-timeline v0.3.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

⚡️ React Headless Timeline

Headless components for building custom timelines for React. Headless means it doesn't provide any UI for the timeline but only handles the logic. Supports both horizontal and vertical timelines.

🚀 Test, build and publish

Features


  • ⚡️ Highly performant and efficient
  • 🔒 Type safe - written in TypeScript
  • 🫧 Doesn't render any UI - it's totally up to you how you're going to style your timeline
  • 🏃‍ Small bundle size

Overview


See what library does and what doesn't for you to decide whether it's a good fit for you or not.

👍 What it does for you:

  • Handles the timeline logic
  • Helps you render timeline components in correct position and size

👎 What it doesn't for you:

  • It doesn't provide any pre-made UI components for you (no need to overwrite any styles to make timeline looks like in designer's dream)

Still not sure? Check some examples ⬇️

📖 Content


Install


npm install react-headless-timeline

Quick start


import Timeline from 'react-headless-timeline';

function App() {
  const timelineConfig = {
    startDate: new Date(1651201447127), // 29th Apr 2022 05:04:07 CET
    endDate: new Date(1651221447127), // 29th Apr 2022 10:37:27 CET
  }

  const events = [
    { 
      startDate: new Date(1651204649127), // 29th Apr 2022 05:57:29 CET
      endDate: new Date(1651214649127), // 29th Apr 2022 08:44:09 CET
    },
  ];
  
  return (
    <Timeline.Provider {...timelineConfig}>
      <>
        <Timeline.Headers
          render={({ headers, getHeaderStyles }) => (
            <div style={{ position: 'relative', width: '100%', height: 30 }}>
              {headers.map((header) => (
                <span key={header.getTime()} style={getHeaderStyles(header)}>
                  {header.getTime()}
                </span>
              ))}
            </div>
          )}
        />

        <Timeline.Events
          render={({ getEventStyles }) => (
            <div style={{ position: 'relative', width: '100%', height: 30 }}>
              {events.map((evt, i) => (
                <span key={i} style={getEventStyles(evt)}>
                  Event
                </span>
              ))}
            </div>
          )}
        />
      </>
    </Timeline.Provider>
  );
}

API


  • Timeline.Provider

    Core timeline component. You have to wrap all of your timeline components inside provider.

    Props: (* - these are required)

    NameTypeDefaultDescription
    startDate *Date
    endDate *Date
    direction"horizontal" / "vertical""horizontal"Determines timeline direction

    Example:

    import Timeline from 'react-headless-timeline';
    
    // ...
    
    <Timeline.Provider startDate={startDate} endDate={endDate} direction="horizontal">
      {children}
    </Timeline.Provider>

    Other examples: See all ⬇️


  • Timeline.Headers

    Provides headers and helper function to generate headers CSS styles (size and position).

    Props: (* - these are required)

    NameTypeDefaultDescription
    cellsnumber2Determines how many headers/cells you want to render. For example if you have a timeline with startDate set to today 10AM and endDate set to today 4PM, by default headers will be an array with startDate and endDate. But let's say you want to display headers every 1 hour, you should then set cells to 7 - 10AM, 11AM, 12AM, 1PM, 2PM, 3PM, 4PM. The minimum value is 2
    render *functionRender your UI inside this function. See example below...

    Example:

    import Timeline from 'react-headless-timeline';
    
    // ...
    
    <Timeline.Headers
      render={({ headers, getHeaderStyles }) => (
        <div>
          {headers.map((header) => (
            <span key={header.getTime()} style={getHeaderStyles(header)}>
              {header.getTime()}
            </span>
          ))}
        </div>
      )}
    />

    Other examples: See all ⬇️


  • Timeline.Events

    Provides helper function to generate event CSS styles (size and position).

    Props: (* - these are required)

    NameTypeDefaultDescription
    render *functionRender your UI inside this function. See example below...

    Example:

    import Timeline from 'react-headless-timeline';
    
    // ...
    
    <Timeline.Events
      render={({ getEventStyles }) => (
        <div>
          {events.map((evt, i) => (
            <span key={i} style={getEventStyles(evt)}>
              Event
            </span>
          ))}
        </div>
      )}
    />

    Other examples: See all ⬇️


  • Timeline.Indicators.CurrentTime

    Provides current time indicator with updates (re-renders) every second or full minute. This component will re-render itself.

    Update every full minute means update at 10:45:00, 10:46:00 and so on...

    Props: (* - these are required)

    NameTypeDefaultDescription
    updateInterval"second" / "minute""minute"Determines update interval. In most cases you'll want to use "minute" option to limit re-renders
    render *functionRender your UI inside this function. See example below...

    Example:

    import Timeline from 'react-headless-timeline';
    
    // ...
    
    <Timeline.Indicators.CurrentTime
      render={({ currentTime, styles }) => (
        <div style={{ height: '100%', ...styles }}>
          <StyledHeader>{currentTime.getTime()}</StyledHeader>
        </div>
      )}
    />

    Other examples:

  • Timeline.Indicators.Time

    This indicator exposes function to position your component properly. It's useful for events without end date.

    Props: (* - these are required)

    NameTypeDefaultDescription
    render *functionRender your UI inside this function. See example below...

    Example:

    import Timeline from 'react-headless-timeline';
    
    // ...
    
    <Timeline.Indicators.Time
      render={({ getIndicatorStyles }) => (
        <div style={{ height: '100%', ...getIndicatorStyles(date) }}>
          <StyledHeader>
            {label}
          </StyledHeader>
        </div>
      )}
    />

    Other examples:


General rules


  • You should use CSS box-sizing: border-box on your timeline components to prevent sizing bugs.

Examples


License


MIT

0.3.2

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago