npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-relative-timer

v1.0.0

Published

React timer component

Downloads

3

Readme

react-relative-timer

A Counter module for React applications. This can be used a step up or step down counter. This is inspired by the Github's relative-time and tries to allow maximum flexibility to the user.

Basic usage

Say you have a web application that shows user posted stories (like twitter) and want to show when a user posted a story; Ex: posted a few seconds ago, posted 2 minutes ago, posted hours ago, posted days ago, ...

Install the dependency from npm

npm i react-relative-timer
import ReactRelativeTimer from 'react-relative-timer';

...
  render() { 
      <ReactRelativeTimer
          className="Timer"
          referenceTime={postedTimeInMills}
          formatMessage={({ seconds, minutes, hours, days }) => {
            if (minutes === 0 && hours === 0 && days === 0) {
              return 'posted a few seconds ago';
            }
            if (hours === 0 && days === 0) {
              return `posted ${minutes} minutes ago `;
            }
            if (days === 0) {
              return 'posted hours ago';
            }
            return 'posted days ago';
          }}
          updateInterval={30000}
     />
   }

String returned by the formatMessage will be the output of this component. The time difference between the referenceTime and current time is the parameter (Time Object) passed to the formatMessage function.

API Specification

|Prop|Usage| Type |Default Value|Example| |---|---|---|---|---| |referenceTime| Timer uses this time as the base to calculate the time difference| Number (in milliseconds)| Date.now() | 1602781020000| |updateInterval| Interval the output should be updated| Number (in milliseconds) | 10000 | 600000| |formatMessage| Format the output of the component. Should return a string with the expected display text| Function (param: Time Object) | ({ totalSeconds, referenceTime }) => '${totalSeconds} passed from ${new Date(referenceTime)}'| ({ minutes }) => '${minutes}min ago |now| Difference between now and referenceTime is used in the counter. Override this if you want current time to be something other than current epoch time | Function | () => Date.now() | () => Date.now() + c| |clearTimer|If you want to stop the output being updated, override this function| Function (param: Time Object) | () => false | ({ hours}) => hours < 5| |isStepUp| Decide on the step up or step down counter functionality| Boolean | true | false| | className | Use this to style output div by passing a CSS class name | String | '' | timer| |dataId| Access the output JS element selectors | 'String' | react-relative-timer | my-timer |

Timer Object

For formatMessage and clearTimer functions a Timer Object is passed with following keys

{
    totalMills,   // Diff between now() - referenceTime if step up or referenceTime - now() if step down
    totalSeconds, // Diff in Seconds
    mills,        // How many milliseconds in the time diff
    seconds,      // How many seconds in the time diff
    minutes,      // How many minutes in the time diff
    hours,        // How many hours in the time diff
    days,         // How many days in the time diff
    referenceTime // referenceTime
 }