1.3.0 • Published 4 years ago

custom-countdown v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Build Status Coverage Status

react-countdown360

A simple and customizable React circular countdown that counts down a number of seconds. This is a React implementation of John Schult's original jquery.countdown360

Examples

You can find some examples in the examples directory.

Basic example Stylized example Formatter example

Quickstart

Install with npm:

npm install react-countdown360

Render your countdown:

import Countdown360 from 'react-countdown360'

const App = () => {
  return <Countdown360 seconds={10} />
}

Documentation

Props

NameTypeDefaultDescription
secondsNumber-Number of seconds for the countdown
autoStartBooleantrueStart the countdown immediatly after rendering
backgroundColorString'#fff'Color for the center of the circle
borderFillColorString'#f11'Color for the filled part of the border
borderUnfillColorString'#e6e2e7'Color for the unfilled part of the border
borderWidthNumber20Width in pixels of the border
clockwiseBooleanfalseSelect the direction of rotation you prefer
fontColorString'#111'Font color for the label
fontFamilyString'sans-serif'The font to use for the label
fontSizeNumber45Font size in pixels
fontWeightNumber700Font weight for the label
onCompleteFunctionundefinedA callback called when the countdown is over
smoothBooleanfalseUpdate the border once every second or smoothly
startingAngleNumber0The angle at which the countdown should start
timeFormatterFunctimeFormatterSecondsA function that returns the value to display
unitFormatterFuncunitFormatterSecondsA function that returns the unit to display
widthNumber200Width in pixels of the countdown to render

Time Formatters

You can customize the way the value on the countdown is shown by providing a custom timeFormatter function.

By default, the value shown is the number of seconds remaining rounded to the nearest integer.

A time formatter is a function that takes one single argument, being the number of milliseconds left, and returns the value to show on the countdown.

For instance, the following function is a time formatter that always shows a value with at least two digits:

const timeFormatterTwoDigits = timeLeft => {
  return Math.round(timeLeft / 1000).toString().padStart(2, '0')
}

We already provide a few time formatters that you can import from the package.

NameDescriptionExamplesSuggested unit formatter
timeFormatterSecondsRounded number of seconds (default behaviour)0, 1, 12unitFormatterSeconds
timeFormatterDigitalClockMM:SS00:00, 00:12, 01: 59unitFormatterBlank

Unit Formatters

You can also customize the unit shown on the countdown by providing a custom unitFormatter function.

A unit formatter is a function that takes one single argument, being the value shown on the countdown (as returned by the given time formatter), and returns the unit to display.

For instance, the following function is a unit formatter to show the number of seconds in Spanish:

const unitFormatterSpanishSeconds = value => {
  return value.toString() === '1' ? 'segundo' : 'segundos'
}

Be careful when choosing your unit formatter that it matches the time formatter in use!

We already provide a few unit formatters that you can import from the package.

NameDescriptionExamples
unitFormatterSeconds'second' or 'seconds'second, seconds
unitFormatterBlankAn empty string

Methods

  • start: start or resume the countdown
  • stop: pause the countdown
  • addSeconds (Number): add or remove (if negative) the given number of seconds to remaining number of seconds
  • extendTimer (Number): add or remove (if negative) the given number of seconds to the total number of seconds