Socket
Socket
Sign inDemoInstall

react-native-countdown-circle-timer

Package Overview
Dependencies
526
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-countdown-circle-timer

Lightweight React Native countdown timer component with color and progress animation based on SVG


Version published
Weekly downloads
12K
increased by12.88%
Maintainers
1
Install size
22.0 kB
Created
Weekly downloads
 

Readme

Source

React Native Countdown Circle Timer

npm npm Codecov npm bundle size

React Native countdown timer component in a circle shape with color and progress animation.

Features

:zap: Performance optimized with single requestAnimationFrame loop to animate color and progress
:rainbow: Transition between colors during the countdown
:european_castle: Fully customizable content in the center of the circle
:rocket: Support iOS and Android

Install

yarn add react-native-countdown-circle-timer

This component has a peer dependency on react-native-svg to draw the countdown circle. react-native-svg has to be installed and linked into your project.

Usage

Component

Check the Expo Snack demo to get started.

import { Text } from 'react-native'
import { CountdownCircleTimer } from 'react-native-countdown-circle-timer'

const UrgeWithPleasureComponent = () => (
  <CountdownCircleTimer
    isPlaying
    duration={7}
    colors={['#004777', '#F7B801', '#A30000', '#A30000']}
    colorsTime={[7, 5, 2, 0]}
  >
    {({ remainingTime }) => <Text>{remainingTime}</Text>}
  </CountdownCircleTimer>
)

Hook

The package exports a hook useCountdown, which accepts the same props as the component and returns all props needed to render your own circle.

import { useCountdown } from 'react-native-countdown-circle-timer'

const {
  path,
  pathLength,
  stroke,
  strokeDashoffset,
  remainingTime,
  elapsedTime,
  size,
  strokeWidth,
} = useCountdown({ isPlaying: true, duration: 7, colors: '#abc' })

Props

Prop NameTypeDefaultDescription
durationnumberrequiredCountdown duration in seconds
colorsstring | string[]requiredcolors prop is either:
- Single valid color in any format or URL to a gradient
- Array of colors in HEX format. At least 2 colors should be provided
colorsTimenumber[]-Indicates the time when a color should switch to the next color. The first number is the countdown duration and the last one is 0 or goal. Works only when colors is an array of HEX colors
isPlayingbooleanfalsePlay or pause animation
initialRemainingTimenumber-Set the initial remaining time if it is different than the duration
updateIntervalnumber0Update interval in seconds. Determines how often the timer updates. When set to 0 the value will update on each key frame
sizenumber180Width and height of the SVG element
strokeWidthnumber12Path stroke width
trailStrokeWidthnumberstrokeWidthTrail stroke width
strokeLinecapround | square | buttroundPath stroke line cap
rotationclockwise | counterclockwiseclockwiseProgress path rotation direction
isGrowingbooleanfalseIndicated if the progress path should be growing instead of shrinking
trailColorstring#d9d9d9Circle trail color - takes any valid color format
isSmoothColorTransitionbooleantrueIndicates if the colors should smoothly transition to the next color
children(props: { remainingTime: number, elapsedTime: number, color: string }) => ReactNode-Render function to customize the time/content in the center of the circle
onComplete(totalElapsedTime: number) => void | { shouldRepeat: boolean, delay?: number, newInitialRemainingTime?: number }-On animation complete event handler
onUpdate(remainingTime: number) => void-On remaining time update event handler

Recipes

Changing duration prop

Once the component is mounted the duration prop can be changed the the timer will respect the new duration. In case the new duration is bigger than the previous one then the timer will continue to the new duration. In case the new duration is smaller then the previous one then the timer will ne over. If you want to restart the timer when the duration changes then pass a new key prop to CountdownCircleTimer component and the timer will start over with the new values provided.

Restart timer at any given time

Pass a key prop to CountdownCircleTimer and change the key when the timer should be restarted. Check this demo to find out one possible implementation.

Repeat timer when countdown is completed

Return an object from onComplete handler, which indicates if the animation should be repeated. Example:

const UrgeWithPleasureComponent = () => (
  <CountdownCircleTimer
    isPlaying
    duration={10}
    colors="#A30000"
    onComplete={() => {
      // do your stuff here
      return { shouldRepeat: true, delay: 1.5 } // repeat animation in 1.5 seconds
    }}
  />
)

Set the initial remaining time different then the duration provided

Pass the remaining time to initialRemainingTime prop. Example:

const UrgeWithPleasureComponent = () => (
  <CountdownCircleTimer
    isPlaying
    duration={60}
    initialRemainingTime={15}
    colors="#A30000"
  />
)

In the example above, the countdown will start at 15 seconds (one quarter before it's done) and it will animate for 15 seconds until reaches 0.

Time formatting functions

children prop of CountdownCircleTimer component will receive as a prop remainingTime in seconds. Below are a few functions that can be used to get different time formatting:

  • Format mm:ss (minutes and seconds)
const children = ({ remainingTime }) => {
  const minutes = Math.floor(remainingTime / 60)
  const seconds = remainingTime % 60

  return `${minutes}:${seconds}`
}
  • Format hh:mm:ss (hours, minutes and seconds)
const children = ({ remainingTime }) => {
  const hours = Math.floor(remainingTime / 3600)
  const minutes = Math.floor((remainingTime % 3600) / 60)
  const seconds = remainingTime % 60

  return `${hours}:${minutes}:${seconds}`
}

Add a11y support

  • Wrapper the timer in an View element and add the following attributes accessible={true} accessibilityLabel={your-aria-abel}
  • Add the following Text element to your children function that will make the screen reader read the remaining time while the timer is running.
const children = ({ remainingTime }) => (
  <Text
    accessibilityRole="timer"
    accessibilityLiveRegion="assertive"
    importantForAccessibility="yes"
  >
    {remainingTime} seconds
  </Text>
)

Add gradient

Since the gradient definition and the usage of it has to be placed under the same SVG element we should use the useCountdown hook and build the countdown component ourself. Check this demo to see how it works.

const {
    ...
  } = useCountdown({ isPlaying: true, duration, colors: 'url(#your-unique-id)' })

  <Svg width={size} height={size}>
    <Defs>
      <LinearGradient id="your-unique-id" x1="1" y1="0" x2="0" y2="0">
        <Stop offset="5%" stopColor="gold"/>
        <Stop offset="95%" stopColor="red"/>
      </LinearGradient>
    </Defs>
    <Path
      d={path}
      fill="none"
      stroke="#d9d9d9"
      strokeWidth={strokeWidth}
    />
    <Path
      d={path}
      fill="none"
      stroke={stroke}
      strokeLinecap="butt"
      strokeWidth={strokeWidth}
      strokeDasharray={pathLength}
      strokeDashoffset={strokeDashoffset}
    />
  </Svg>

Keywords

FAQs

Last updated on 15 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc