Coder Social home page Coder Social logo

rollex's Introduction

Rollex

Build Status

Build Status

Versatile counters in React

Installation

Install with yarn:

yarn add rollex
# or npm:
npm i -S rollex

Usage

  1. Import Counter component:
import { Counter } from 'rollex'
  1. Use it:
<Counter seconds={98} />

API

Setting time intervals

There are multiple ways of setting the time interval to count down (or up) to:

You can tell Rollex how many seconds to count:

<Counter seconds={60} />

Or, you can specify timestamps to count to (and from):

// from now to a given timestamp
<Counter to={1506951918155} />
// from `from` to `to`
<Counter from={1506951900123} to={1506951918155} />

Update interval

By default, Rollex counters update every second. You can, however, set your own update period via interval prop:

// update every 2 seconds
<Counter seconds={60} interval={2000} />

Countdown direction

By default, Rollex counters count "down" (from, say, 60 down to 0). You can make a counter go up:

// from 0 to 60
<Counter seconds={60} direction="up" />

Periods and segments

Rollex counters are split into periods (days, hours, minutes, seconds). Each period has its own segment, which contains the digits which correspond to a given period.

By default, Rollex counters use four periods: "days", "hours", "minutes", "seconds" and each segment is exactly 2 digits long.

You can control this behaviour:

// show only hours and minutes
<Counter seconds={3600} minPeriod="minutes" maxPeriod="hours" />
// show 3 digits per segment
<Counter seconds={72 * 3600} digits={3} />

Freezing the countdown

You can temporarily (or not) freeze the counter by passing it a frozen prop:

<Counter seconds={60} frozen />

When frozen is truthy, the counter will not update at all.

Synchronizing time with client

By default, Rollex counters do not try to synchronize time. That means that if a counter is frozen at 59 seconds, and then unfrozen after an hour, it will continue counting down from 59 seconds. If time sync is enabled, the counter will synchronize time and instantly go down to 0.

<Counter seconds={59} syncTime />

Animating the counter

By default, Rollex counters are not animated (they look like digital clocks). If you want to enable animations (make the counters look analog), you should pass the easingFunction prop (any valid CSS easing function):

<Counter seconds={59} easingFunction='ease-in-out' />

You can also control animation duration (300 ms by default):

<Counter seconds={59} easingFunction='ease-in-out' easingDuration={500} />

Radix

You can even control the radix that Rollex counters use! By default it's 10, but if you want to go dozenal, you can:

<Counter seconds={3600} radix={12} />

Transform and wrap digits

If you want to use different symbols for your digits, you can (let's say, you want a dozenal counter with 'X' representing a 10 and 'E' representing an 11):

<Counter seconds={3600} radix={12} digitMap={{
  'A': 'X',
  'B': 'E',
}} />

You can get even more control over your digits with a digit map:

// use images instead of text for digits
<Counter
  seconds={3600}
  digitWrapper={digit => <img src={`/digits/${digit}.jpg`} />}
/>

Labels and separators

All counter segments can be labelled.

// map from periods to strings
<Counter seconds={141234} labels={{
  days: 'days',
  hours: 'hours'
}} />
// use a function (use case: pluralisation)
<Counter seconds={123123} labels={(period, number) => {
  return number % 10 === 1 ? period.slice(0, -1) : period
}}>

Segments can be separated by a separator symbol:

// dd:hh:mm:ss
<Counter seconds={2312413} separator=':' />

License

Released under MIT License.

Authors

Created by Alexander Komarov.

Supported by Umbrellio

rollex's People

Contributors

akxcv avatar jelf avatar guljeny avatar

Stargazers

 avatar Rustam Ibragimov avatar Yuri Smirnov avatar Eric Araújo avatar Vlad Trukhin avatar Optimus Advocate avatar  avatar Stas avatar  avatar

Watchers

James Cloos avatar  avatar Nik Marchenko avatar Stas avatar  avatar  avatar

Forkers

7dr1v3 guljeny

rollex's Issues

Use singleton class instead of global objects

Currently we use window.rollexIntervals object for making multiple counters run on a single interval. It would be better to rewrite this with a singleton class to avoid window pollution.

Remove stage-0 code

Right now the project uses babel-preset-stage-0 for object spread operation and for static propTypes. It's less convenient for users, though, because they'll have to use babel-preset-stage-0 to compile their project with our library.

Travis runs tests twice

Because of the new prepublish script, Travis is now running all tests twice. This is because NPM < 5.0 is executing prepublish upon npm install. In NPM 5.0, prepublish is only executed before npm publish. We don't want to remove current pre-publish functionality, but running tests twice on Travis is still bad.

Discussion: Default segment size limits

Segments have default sizes. Rollex counters should be designed to resemble physical rolling counters as closely as possible. This means that the number of digits in each segment is a constant.

To illustrate the point:
Let's say the counter starts at "10:10:10:10". What should the counter display after 1 second passes? Clearly, "10:10:10:09". The zero is there because in the real, physical rolling counter digits cannot ever appear or disappear. What should the counter display after 1 day passes, then? A physical counter would display "09:10:10:10".

But what happens when the counter has to count down from, let's say, 200 days? By default, the counter should limit itself to 99 displayed days: "99:00:00:00". After 10 days, it will still show "99:00:00:00". However, if the user specifically desires not to limit the size of the segment, the counter should show "200:00:00:00". After 102 days, it should show "098:00:00:00". When there's only 1 day left, it should display "001:00:00:00".

And, if the counting direction is "up", the counter should show "000:00:00:00" initially, because it knows it'll have to get to 200 days before the counting stops.

What about non-decimal numerical bases? What should "days" be limited to by default? Let's say we're dealing with hexadecimals. Should the limit be "ff" ((16 ^ 2) - 1) or something else?

By the way, this will apply not only to "days", but to any segment that represents maxPeriod. For example, if maxPeriod == "hours", and we have 78 hours to count, the counter will display "78:00:00", without limiting "hours" to 23.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.