0.3.1 • Published 2 years ago

simple-circle-timer v0.3.1

Weekly downloads
35
License
GPL-3.0
Repository
github
Last release
2 years ago

Simple React Circle Timer

Lightweight, easily implemented countdown timer with circular progress bar. Optional Play, Pause, and Reset buttons.

demo

Accurate timekeeping:

Instead of relying on the component's state to keep track of time, which is very prone to bugs and innaccuracies, this timer uses interval checks on the current Date object, ensuring accurate results down to the millisecond.

Play/Pause/Reset:

Most importantly, this timer differs from others in the amount of control it gives the user. See below for how to implement your own buttons or trigger the Play, Pause, Reset functions.

Scalable display:

The progress bar animation is handled by pure CSS (including the play/pause display). The entire component itself is scalable to any size (using the 'size' and 'fontSize' props) and color (using the 'fillColor' and 'bgColor' props). The font family and other styling will inherit from your parent component.

Installation

yarn add simple-circle-timer

or

npm install simple-circle-timer

Basic Usage

import {Timer} from 'simple-circle-timer'

const YourComponent = () => (
  <Timer/>
)

Props

size : (int) length and width of component in pixels
fontSize : (int) size of text inside the circle
minutes : (float) number of minutes you want the timer to run for (whole numbers or decimals)
fillColor : (str) color of progress bar & countdown text (hex code or color)
bgColor : (str) color inside the circle (hex code or color)
showMs : (bool) show milliseconds in the displayed time
onComplete : (function) runs when the timer reaches 00:00
completeMsg : (str) text displayed when the time runs out
running : (bool) whether or not the timer is counting down
setRunning : (function) needs to be passed to Timer for play, pause, reset (see below)
reset : (bool) set to true momentarily to trigger reset (see below)
setReset : (function) needs to be passed to Timer for reset (see below)

Play, Pause, Reset

This component is designed to give the user control over the timer. Below is an example of how to set up buttons using React hooks to play, pause, reset, and mount the timer with different settings.

import React, { useState } from 'react';
import {Timer} from 'simple-circle-timer'

const YourComponent = () => {
  const [ timerExists, setTimerExists ] = useState( false )
  const [ running, setRunning ] = useState( true )
  const [ reset, setReset ] = useState( false )

  //new timer is loaded in a paused state, awaiting 'play' command
  const mountPaused = () => {
    setTimerExists( true )
    setRunning( false )
  }

  //new timer is loaded already in a running state
  const mountRunning = () => {
    setTimerExists( true )
    setRunning( true )
  }

  //replace any/all of these buttons with your own components, or trigger the same state changes with functions from elsewhere in your app. 
  return (
    <>
      <div style={{ display: 'grid' }}>
        <button onClick={() => mountRunning()}>Load New Timer (running)</button>
        <button onClick={() => mountPaused()}>Load New Timer (paused)</button>
        <button onClick={() => setTimerExists( false )}>Remove Timer</button>
        <button onClick={() => setRunning( true )}>Play</button>
        <button onClick={() => setRunning( false )}>Pause</button>
        <button onClick={() => setReset( true )}>Reset</button>
      </div>
      {timerExists ? <Timer running={running} setRunning={setRunning} reset={reset} setReset={setReset} /> : null}
    </>
  )
}
0.3.1

2 years ago

0.3.0

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.20

3 years ago

0.1.19

3 years ago

0.1.18

3 years ago

0.1.17

3 years ago

0.1.16

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.10

3 years ago

0.1.11

3 years ago

0.1.12

3 years ago

0.1.13

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago