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-compound-timerv3

v1.0.1

Published

Timer compound react component

Downloads

4

Readme

react-compound-timerv3

About

This is a forked version of react-compound-timer compatible with React 18.


Timer compound component for react and react-native to make building timers less painfull. It incapsulates all timer logic - you should only think about rendering!

See Working Examples

Forward Count

Just render a simple timer and start counting forward from 0. Use compound components to render time units. You can see all avaliable time units in this example.

<Timer>
  <Timer.Days /> days
  <Timer.Hours /> hours
  <Timer.Minutes /> minutes
  <Timer.Seconds /> seconds
  <Timer.Milliseconds /> milliseconds
</Timer>

Backward Count

The same simple timer, but counting backward.

<Timer initialTime={55000} direction="backward">
  {() => (
    <React.Fragment>
      <Timer.Days /> days
      <Timer.Hours /> hours
      <Timer.Minutes /> minutes
      <Timer.Seconds /> seconds
      <Timer.Milliseconds /> milliseconds
    </React.Fragment>
  )}
</Timer>

Controls

Get action functions from props and use them to control your timer.

<Timer initialTime={55000}>
  {({ start, resume, pause, stop, reset, timerState }) => (
    <React.Fragment>
      <div>
        <Timer.Days /> days
        <Timer.Hours /> hours
        <Timer.Minutes /> minutes
        <Timer.Seconds /> seconds
        <Timer.Milliseconds /> milliseconds
      </div>
      <div>{timerState}</div>
      <br />
      <div>
        <button onClick={start}>Start</button>
        <button onClick={pause}>Pause</button>
        <button onClick={resume}>Resume</button>
        <button onClick={stop}>Stop</button>
        <button onClick={reset}>Reset</button>
      </div>
    </React.Fragment>
  )}
</Timer>

No autoplay

You can just render a timer, and then start it only by using action function 'start' from props.

<Timer initialTime={55000} startImmediately={false}>
  {({ start, resume, pause, stop, reset, timerState }) => (
    <React.Fragment>
      <div>
        <Timer.Days /> days
        <Timer.Hours /> hours
        <Timer.Minutes /> minutes
        <Timer.Seconds /> seconds
        <Timer.Milliseconds /> milliseconds
      </div>
      <div>{timerState}</div>
      <br />
      <div>
        <button onClick={start}>Start</button>
        <button onClick={pause}>Pause</button>
        <button onClick={resume}>Resume</button>
        <button onClick={stop}>Stop</button>
        <button onClick={reset}>Reset</button>
      </div>
    </React.Fragment>
  )}
</Timer>

With hooks

Write your own hooks on timer actions.

<Timer
  initialTime={55000}
  startImmediately={false}
  onStart={() => console.log("onStart hook")}
  onResume={() => console.log("onResume hook")}
  onPause={() => console.log("onPause hook")}
  onStop={() => console.log("onStop hook")}
  onReset={() => console.log("onReset hook")}
>
  {({ start, resume, pause, stop, reset, timerState }) => (
    <React.Fragment>
      <div>
        <Timer.Days /> days
        <Timer.Hours /> hours
        <Timer.Minutes /> minutes
        <Timer.Seconds /> seconds
        <Timer.Milliseconds /> milliseconds
      </div>
      <div>{timerState}</div>
      <br />
      <div>
        <button onClick={start}>Start</button>
        <button onClick={pause}>Pause</button>
        <button onClick={resume}>Resume</button>
        <button onClick={stop}>Stop</button>
        <button onClick={reset}>Reset</button>
      </div>
    </React.Fragment>
  )}
</Timer>

Last Unit Property

Control your last unit. For example, 1 minute 30 seconds can be 90 seconds, if you set lastUnit as 'seconds'. It means that minutes, hours and days will not be computed.

<Timer initialTime={60000 * 60 * 48 + 5000} lastUnit="h" direction="backward">
  {() => (
    <React.Fragment>
      <Timer.Days /> days
      <Timer.Hours /> hours
      <Timer.Minutes /> minutes
      <Timer.Seconds /> seconds
      <Timer.Milliseconds /> milliseconds
    </React.Fragment>
  )}
</Timer>

With checkpoints

If you need to call some functions on certain time - provide checkpoints property. It is an array of objects. Each object contains time and callback, that will be fired, when timer intersects checkpoint's time.

<Timer
  initialTime={60000 * 60 * 48 + 5000}
  direction="backward"
  checkpoints={[
    {
      time: 60000 * 60 * 48,
      callback: () => console.log("Checkpoint A"),
    },
    {
      time: 60000 * 60 * 48 - 5000,
      callback: () => console.log("Checkpoint B"),
    },
  ]}
>
  {() => (
    <React.Fragment>
      <Timer.Days /> days
      <Timer.Hours /> hours
      <Timer.Minutes /> minutes
      <Timer.Seconds /> seconds
      <Timer.Milliseconds /> milliseconds
    </React.Fragment>
  )}
</Timer>

React Native

Timer compound component also works for react-native applications. All you have to do is wrap the elements in a tag from react-native.

Countdown example with milliseconds

import { View, Text } from "react-native";
import Timer from "react-compound-timer";

<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
  <Timer
    initialTime={60 * 1000}
    direction="backward"
    timeToUpdate={10}
    checkpoints={[
      {
        time: 0,
        callback: () => alert("countdown finished"),
      },
    ]}
  >
    <Text style={{ fontFamily: "Helvetica Neue" }}>
      <Text style={{ fontSize: 32 }}>
        <Timer.Seconds />
      </Text>
      <Text style={{ fontSize: 12 }}>
        <Timer.Milliseconds />
      </Text>
    </Text>
  </Timer>
</View>;