fix: update docs for RN gradient closes #194
fix: export useCountdown types fixes #193 This is fix for the issue where types for the hook are referenced by the shared package, which is not exported. The solve the problem now we copy the types from the shared package and add them to each package when bundling the code. This is not the best solution but it is the simplest. Once we have a good way to bundle types from monorepo this can be changed.
docs: update CHANGELOG for 3.0.9
@@ -3,6 +3,12 @@
|
|
3 |
All notable changes to this project will be documented in this file.
|
4 |
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
## [3.0.8](2022-01-29)
|
7 |
|
8 |
### Fix
|
3 |
All notable changes to this project will be documented in this file.
|
4 |
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5 |
|
6 |
+
## [3.0.9](2022-03-04)
|
7 |
+
|
8 |
+
### Fix
|
9 |
+
|
10 |
+
- Fix for #193. This is fix for the issue where types for the hook are referenced by the shared package, which is not exported. To solve the problem now we copy the types from the shared package and add them to each package when bundling the code. This is not the best solution but it is the simplest. Once we have a good way to bundle types from monorepo this can be changed.
|
11 |
+
|
12 |
## [3.0.8](2022-01-29)
|
13 |
|
14 |
### Fix
|
@@ -177,18 +177,34 @@ const children = ({ remainingTime }) => (
|
|
177 |
|
178 |
### Add gradient
|
179 |
|
180 |
-
|
181 |
|
182 |
```jsx
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
<
|
192 |
-
|
193 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
```
|
177 |
|
178 |
### Add gradient
|
179 |
|
180 |
+
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](https://snack.expo.dev/@dimitrov/react-native-countdown-circle-timer-gradient?platform=ios) to see how it works.
|
181 |
|
182 |
```jsx
|
183 |
+
const {
|
184 |
+
...
|
185 |
+
} = useCountdown({ isPlaying: true, duration, colors: 'url(#your-unique-id)' })
|
186 |
+
|
187 |
+
<Svg width={size} height={size}>
|
188 |
+
<Defs>
|
189 |
+
<LinearGradient id="your-unique-id" x1="1" y1="0" x2="0" y2="0">
|
190 |
+
<Stop offset="5%" stopColor="gold"/>
|
191 |
+
<Stop offset="95%" stopColor="red"/>
|
192 |
+
</LinearGradient>
|
193 |
+
</Defs>
|
194 |
+
<Path
|
195 |
+
d={path}
|
196 |
+
fill="none"
|
197 |
+
stroke="#d9d9d9"
|
198 |
+
strokeWidth={strokeWidth}
|
199 |
+
/>
|
200 |
+
<Path
|
201 |
+
d={path}
|
202 |
+
fill="none"
|
203 |
+
stroke={stroke}
|
204 |
+
strokeLinecap="butt"
|
205 |
+
strokeWidth={strokeWidth}
|
206 |
+
strokeDasharray={pathLength}
|
207 |
+
strokeDashoffset={strokeDashoffset}
|
208 |
+
/>
|
209 |
+
</Svg>
|
210 |
```
|
@@ -1,15 +1,14 @@
|
|
1 |
{
|
2 |
"name": "react-native-countdown-circle-timer",
|
3 |
-
"version": "3.0.
|
4 |
"description": "Lightweight React Native countdown timer component with color and progress animation based on SVG",
|
5 |
"main": "./lib/index.js",
|
6 |
"source": "./src/index.ts",
|
7 |
"files": [
|
8 |
"lib"
|
9 |
],
|
10 |
"scripts": {
|
11 |
-
"
|
12 |
-
"build": "pnpm ts-declaration && node scripts/build.js",
|
13 |
"dev:push": "pnpm run build && yalc push",
|
14 |
"test": "jest --collectCoverage",
|
15 |
"test:watch": "jest --watch",
|
1 |
{
|
2 |
"name": "react-native-countdown-circle-timer",
|
3 |
+
"version": "3.0.9",
|
4 |
"description": "Lightweight React Native countdown timer component with color and progress animation based on SVG",
|
5 |
"main": "./lib/index.js",
|
6 |
"source": "./src/index.ts",
|
7 |
"files": [
|
8 |
"lib"
|
9 |
],
|
10 |
"scripts": {
|
11 |
+
"build": "node scripts/build.js",
|
|
|
12 |
"dev:push": "pnpm run build && yalc push",
|
13 |
"test": "jest --collectCoverage",
|
14 |
"test:watch": "jest --watch",
|
@@ -1,3 +1,4 @@
|
|
|
|
1 |
const esbuild = require('esbuild')
|
2 |
const pkg = require('../package.json')
|
3 |
|
@@ -8,8 +9,16 @@ const commonProps = {
|
|
8 |
external: ['react', 'react-native', 'react-native-svg'],
|
9 |
}
|
10 |
|
11 |
-
esbuild
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const fs = require('fs')
|
2 |
const esbuild = require('esbuild')
|
3 |
const pkg = require('../package.json')
|
4 |
|
9 |
external: ['react', 'react-native', 'react-native-svg'],
|
10 |
}
|
11 |
|
12 |
+
esbuild
|
13 |
+
.build({
|
14 |
+
...commonProps,
|
15 |
+
outfile: pkg.main,
|
16 |
+
format: 'cjs',
|
17 |
+
})
|
18 |
+
.then(() => {
|
19 |
+
fs.copyFile('../shared/src/types.ts', './lib/index.d.ts', (err) => {
|
20 |
+
if (err) {
|
21 |
+
throw err
|
22 |
+
}
|
23 |
+
})
|
24 |
+
})
|
@@ -3,7 +3,7 @@ import { View } from 'react-native'
|
|
3 |
import type { StyleProp, ViewStyle } from 'react-native'
|
4 |
import Svg, { Path } from 'react-native-svg'
|
5 |
import { useCountdown, getWrapperStyle, timeStyle } from '@countdown/shared'
|
6 |
-
import type { Props } from '
|
7 |
|
8 |
const CountdownCircleTimer = (props: Props) => {
|
9 |
const { children, duration, strokeLinecap, trailColor, trailStrokeWidth } =
|
3 |
import type { StyleProp, ViewStyle } from 'react-native'
|
4 |
import Svg, { Path } from 'react-native-svg'
|
5 |
import { useCountdown, getWrapperStyle, timeStyle } from '@countdown/shared'
|
6 |
+
import type { Props } from '@countdown/shared'
|
7 |
|
8 |
const CountdownCircleTimer = (props: Props) => {
|
9 |
const { children, duration, strokeLinecap, trailColor, trailStrokeWidth } =
|
@@ -1,2 +1,2 @@
|
|
1 |
export { CountdownCircleTimer, useCountdown } from './CountdownCircleTimer'
|
2 |
-
export type { Props } from '
|
1 |
export { CountdownCircleTimer, useCountdown } from './CountdownCircleTimer'
|
2 |
+
export type { Props } from '@countdown/shared'
|
@@ -1,64 +0,0 @@
|
|
1 |
-
type ColorHex = `#${string}`
|
2 |
-
type ColorRGBA = `rgba(${string})`
|
3 |
-
type ColorURL = `url(#${string})`
|
4 |
-
export type ColorRGB = `rgb(${string})`
|
5 |
-
export type ColorFormat = ColorHex | ColorRGB | ColorRGBA | ColorURL
|
6 |
-
type TimeProps = {
|
7 |
-
remainingTime: number
|
8 |
-
elapsedTime: number
|
9 |
-
color: ColorFormat
|
10 |
-
}
|
11 |
-
|
12 |
-
type SingleColor = {
|
13 |
-
/** Single valid color or url to a gradient */
|
14 |
-
colors: ColorFormat
|
15 |
-
/** Colors time works only when the colors prop is an array of colors*/
|
16 |
-
colorsTime?: never
|
17 |
-
}
|
18 |
-
|
19 |
-
type MultipleColors = {
|
20 |
-
/** Array of colors in HEX format. At least 2 colors should be provided */
|
21 |
-
colors: { 0: ColorHex } & { 1: ColorHex } & ColorHex[]
|
22 |
-
/** Indicates the time when a color should switch to the next color. The first item should be the duration and the last one should be 0/goal. Example with duration of 10 seconds: [10, 6, 3, 0] */
|
23 |
-
colorsTime: { 0: number } & { 1: number } & number[]
|
24 |
-
}
|
25 |
-
|
26 |
-
type OnComplete = {
|
27 |
-
/** Indicates if the loop should start over. Default: false */
|
28 |
-
shouldRepeat?: boolean
|
29 |
-
/** Delay in seconds before looping again. Default: 0 */
|
30 |
-
delay?: number
|
31 |
-
/** Set new initial remaining when starting over the animation */
|
32 |
-
newInitialRemainingTime?: number
|
33 |
-
}
|
34 |
-
|
35 |
-
export type Props = {
|
36 |
-
/** Countdown duration in seconds */
|
37 |
-
duration: number
|
38 |
-
/** Set the initial remaining time if it is different than the duration */
|
39 |
-
initialRemainingTime?: number
|
40 |
-
/** Update interval in seconds. Determines how often the timer updates. When set to 0 the value will update on each key frame. Default: 0 */
|
41 |
-
updateInterval?: number
|
42 |
-
/** Width and height of the SVG element. Default: 180 */
|
43 |
-
size?: number
|
44 |
-
/** Path stroke width. Default: 12 */
|
45 |
-
strokeWidth?: number
|
46 |
-
/** Trail stroke width */
|
47 |
-
trailStrokeWidth?: number
|
48 |
-
/** Path stroke line cap. Default: "round" */
|
49 |
-
strokeLinecap?: 'round' | 'square' | 'butt'
|
50 |
-
/** Progress path rotation direction. Default: "clockwise" */
|
51 |
-
rotation?: 'clockwise' | 'counterclockwise'
|
52 |
-
/** Circle trail color - takes any valid color format (HEX, rgb, rgba, etc.). Default: #d9d9d9 */
|
53 |
-
trailColor?: ColorFormat
|
54 |
-
/** Play or pause animation. Default: false */
|
55 |
-
isPlaying?: boolean
|
56 |
-
/** Indicates if the colors should smoothly transition to the next color. Default: true */
|
57 |
-
isSmoothColorTransition?: boolean
|
58 |
-
/** Render function to customize the time/content in the center of the circle */
|
59 |
-
children?: (props: TimeProps) => React.ReactNode
|
60 |
-
/** On animation complete event handler */
|
61 |
-
onComplete?: (totalElapsedTime: number) => OnComplete | void
|
62 |
-
/** On remaining time update event handler */
|
63 |
-
onUpdate?: (remainingTime: number) => void
|
64 |
-
} & (SingleColor | MultipleColors)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1,8 +1,11 @@
|
|
|
|
|
|
1 |
type ColorHex = `#${string}`
|
2 |
type ColorRGBA = `rgba(${string})`
|
3 |
type ColorURL = `url(#${string})`
|
4 |
-
export type ColorRGB = `rgb(${string})`
|
5 |
-
export type ColorFormat = ColorHex | ColorRGB | ColorRGBA | ColorURL
|
|
|
6 |
type TimeProps = {
|
7 |
remainingTime: number
|
8 |
elapsedTime: number
|
@@ -32,7 +35,7 @@ type OnComplete = {
|
|
32 |
newInitialRemainingTime?: number
|
33 |
}
|
34 |
|
35 |
-
export type Props = {
|
36 |
/** Countdown duration in seconds */
|
37 |
duration: number
|
38 |
/** Set the initial remaining time if it is different than the duration */
|
@@ -62,3 +65,20 @@ export type Props = {
|
|
62 |
/** On remaining time update event handler */
|
63 |
onUpdate?: (remainingTime: number) => void
|
64 |
} & (SingleColor | MultipleColors)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/// <reference types="react" />
|
2 |
+
|
3 |
type ColorHex = `#${string}`
|
4 |
type ColorRGBA = `rgba(${string})`
|
5 |
type ColorURL = `url(#${string})`
|
6 |
+
export declare type ColorRGB = `rgb(${string})`
|
7 |
+
export declare type ColorFormat = ColorHex | ColorRGB | ColorRGBA | ColorURL
|
8 |
+
|
9 |
type TimeProps = {
|
10 |
remainingTime: number
|
11 |
elapsedTime: number
|
35 |
newInitialRemainingTime?: number
|
36 |
}
|
37 |
|
38 |
+
export declare type Props = {
|
39 |
/** Countdown duration in seconds */
|
40 |
duration: number
|
41 |
/** Set the initial remaining time if it is different than the duration */
|
65 |
/** On remaining time update event handler */
|
66 |
onUpdate?: (remainingTime: number) => void
|
67 |
} & (SingleColor | MultipleColors)
|
68 |
+
|
69 |
+
export declare const useCountdown: (props: Props) => {
|
70 |
+
elapsedTime: number
|
71 |
+
path: string
|
72 |
+
pathLength: number
|
73 |
+
remainingTime: number
|
74 |
+
rotation: 'clockwise' | 'counterclockwise'
|
75 |
+
size: number
|
76 |
+
stroke: ColorFormat
|
77 |
+
strokeDashoffset: number
|
78 |
+
strokeWidth: number
|
79 |
+
}
|
80 |
+
|
81 |
+
export declare const CountdownCircleTimer: {
|
82 |
+
(props: Props): JSX.Element
|
83 |
+
displayName: 'CountdownCircleTimer'
|
84 |
+
}
|
@@ -1,6 +1,6 @@
|
|
1 |
{
|
2 |
"name": "react-countdown-circle-timer",
|
3 |
-
"version": "3.0.
|
4 |
"description": "Lightweight React countdown timer component with color and progress animation based on SVG",
|
5 |
"main": "./lib/index.js",
|
6 |
"module": "./lib/index.module.js",
|
@@ -10,8 +10,7 @@
|
|
10 |
],
|
11 |
"scripts": {
|
12 |
"start": "node scripts/serve.js",
|
13 |
-
"
|
14 |
-
"build": "pnpm ts-declaration && node scripts/build.js",
|
15 |
"dev:push": "pnpm run build && yalc push",
|
16 |
"test": "jest --collectCoverage",
|
17 |
"test:watch": "jest --watch",
|
1 |
{
|
2 |
"name": "react-countdown-circle-timer",
|
3 |
+
"version": "3.0.9",
|
4 |
"description": "Lightweight React countdown timer component with color and progress animation based on SVG",
|
5 |
"main": "./lib/index.js",
|
6 |
"module": "./lib/index.module.js",
|
10 |
],
|
11 |
"scripts": {
|
12 |
"start": "node scripts/serve.js",
|
13 |
+
"build": "node scripts/build.js",
|
|
|
14 |
"dev:push": "pnpm run build && yalc push",
|
15 |
"test": "jest --collectCoverage",
|
16 |
"test:watch": "jest --watch",
|
@@ -1,3 +1,4 @@
|
|
|
|
1 |
const esbuild = require('esbuild')
|
2 |
const pkg = require('../package.json')
|
3 |
|
@@ -14,8 +15,16 @@ esbuild.build({
|
|
14 |
format: 'cjs',
|
15 |
})
|
16 |
|
17 |
-
esbuild
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const fs = require('fs')
|
2 |
const esbuild = require('esbuild')
|
3 |
const pkg = require('../package.json')
|
4 |
|
15 |
format: 'cjs',
|
16 |
})
|
17 |
|
18 |
+
esbuild
|
19 |
+
.build({
|
20 |
+
...commonProps,
|
21 |
+
outfile: pkg.module,
|
22 |
+
format: 'esm',
|
23 |
+
})
|
24 |
+
.then(() => {
|
25 |
+
fs.copyFile('../shared/src/types.ts', './lib/index.d.ts', (err) => {
|
26 |
+
if (err) {
|
27 |
+
throw err
|
28 |
+
}
|
29 |
+
})
|
30 |
+
})
|
@@ -1,6 +1,6 @@
|
|
1 |
import React from 'react'
|
2 |
import { useCountdown, getWrapperStyle, timeStyle } from '@countdown/shared'
|
3 |
-
import type { Props } from '
|
4 |
|
5 |
const CountdownCircleTimer = (props: Props) => {
|
6 |
const { children, strokeLinecap, trailColor, trailStrokeWidth } = props
|
1 |
import React from 'react'
|
2 |
import { useCountdown, getWrapperStyle, timeStyle } from '@countdown/shared'
|
3 |
+
import type { Props } from '@countdown/shared'
|
4 |
|
5 |
const CountdownCircleTimer = (props: Props) => {
|
6 |
const { children, strokeLinecap, trailColor, trailStrokeWidth } = props
|
@@ -1,2 +1,2 @@
|
|
1 |
export { CountdownCircleTimer, useCountdown } from './CountdownCircleTimer'
|
2 |
-
export type { Props } from '
|
1 |
export { CountdownCircleTimer, useCountdown } from './CountdownCircleTimer'
|
2 |
+
export type { Props } from '@countdown/shared'
|
@@ -1,64 +0,0 @@
|
|
1 |
-
type ColorHex = `#${string}`
|
2 |
-
type ColorRGBA = `rgba(${string})`
|
3 |
-
type ColorURL = `url(#${string})`
|
4 |
-
export type ColorRGB = `rgb(${string})`
|
5 |
-
export type ColorFormat = ColorHex | ColorRGB | ColorRGBA | ColorURL
|
6 |
-
type TimeProps = {
|
7 |
-
remainingTime: number
|
8 |
-
elapsedTime: number
|
9 |
-
color: ColorFormat
|
10 |
-
}
|
11 |
-
|
12 |
-
type SingleColor = {
|
13 |
-
/** Single valid color or url to a gradient */
|
14 |
-
colors: ColorFormat
|
15 |
-
/** Colors time works only when the colors prop is an array of colors*/
|
16 |
-
colorsTime?: never
|
17 |
-
}
|
18 |
-
|
19 |
-
type MultipleColors = {
|
20 |
-
/** Array of colors in HEX format. At least 2 colors should be provided */
|
21 |
-
colors: { 0: ColorHex } & { 1: ColorHex } & ColorHex[]
|
22 |
-
/** Indicates the time when a color should switch to the next color. The first item should be the duration and the last one should be 0/goal. Example with duration of 10 seconds: [10, 6, 3, 0] */
|
23 |
-
colorsTime: { 0: number } & { 1: number } & number[]
|
24 |
-
}
|
25 |
-
|
26 |
-
type OnComplete = {
|
27 |
-
/** Indicates if the loop should start over. Default: false */
|
28 |
-
shouldRepeat?: boolean
|
29 |
-
/** Delay in seconds before looping again. Default: 0 */
|
30 |
-
delay?: number
|
31 |
-
/** Set new initial remaining when starting over the animation */
|
32 |
-
newInitialRemainingTime?: number
|
33 |
-
}
|
34 |
-
|
35 |
-
export type Props = {
|
36 |
-
/** Countdown duration in seconds */
|
37 |
-
duration: number
|
38 |
-
/** Set the initial remaining time if it is different than the duration */
|
39 |
-
initialRemainingTime?: number
|
40 |
-
/** Update interval in seconds. Determines how often the timer updates. When set to 0 the value will update on each key frame. Default: 0 */
|
41 |
-
updateInterval?: number
|
42 |
-
/** Width and height of the SVG element. Default: 180 */
|
43 |
-
size?: number
|
44 |
-
/** Path stroke width. Default: 12 */
|
45 |
-
strokeWidth?: number
|
46 |
-
/** Trail stroke width */
|
47 |
-
trailStrokeWidth?: number
|
48 |
-
/** Path stroke line cap. Default: "round" */
|
49 |
-
strokeLinecap?: 'round' | 'square' | 'butt'
|
50 |
-
/** Progress path rotation direction. Default: "clockwise" */
|
51 |
-
rotation?: 'clockwise' | 'counterclockwise'
|
52 |
-
/** Circle trail color - takes any valid color format (HEX, rgb, rgba, etc.). Default: #d9d9d9 */
|
53 |
-
trailColor?: ColorFormat
|
54 |
-
/** Play or pause animation. Default: false */
|
55 |
-
isPlaying?: boolean
|
56 |
-
/** Indicates if the colors should smoothly transition to the next color. Default: true */
|
57 |
-
isSmoothColorTransition?: boolean
|
58 |
-
/** Render function to customize the time/content in the center of the circle */
|
59 |
-
children?: (props: TimeProps) => React.ReactNode
|
60 |
-
/** On animation complete event handler */
|
61 |
-
onComplete?: (totalElapsedTime: number) => OnComplete | void
|
62 |
-
/** On remaining time update event handler */
|
63 |
-
onUpdate?: (remainingTime: number) => void
|
64 |
-
} & (SingleColor | MultipleColors)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
React Native countdown timer component in a circle shape with color and progress animation.
<img src="https://user-images.githubusercontent.com/10707142/66097204-ca68c200-e59d-11e9-9b70-688409755aaa.gif" width="200"> <img src="https://user-images.githubusercontent.com/10707142/65935516-a0869280-e419-11e9-9bb0-40c4d1ef2bbe.gif" width="200"> <img src="https://user-images.githubusercontent.com/10707142/65963815-cfbdf380-e45b-11e9-809d-970174e88914.gif" width="200">
: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
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.
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>
)
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' })
| Prop Name | Type | Default | Description |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| duration | number | required | Countdown duration in seconds |
| colors | string | string[] | required | colors
prop is either:<br> - Single valid color in any format or URL to a gradient<br> - Array of colors in HEX format. At least 2 colors should be provided |
| colorsTime | number[] | - | 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 |
| isPlaying | boolean | false | Play or pause animation |
| initialRemainingTime | number | - | Set the initial remaining time if it is different than the duration |
| updateInterval | number | 0 | Update interval in seconds. Determines how often the timer updates. When set to 0 the value will update on each key frame |
| size | number | 180 | Width and height of the SVG element |
| strokeWidth | number | 12 | Path stroke width |
| trailStrokeWidth | number | strokeWidth | Trail stroke width |
| strokeLinecap | round | square | butt | round | Path stroke line cap |
| rotation | clockwise | counterclockwise | clockwise | Progress path rotation direction |
| trailColor | string | #d9d9d9 | Circle trail color - takes any valid color format |
| isSmoothColorTransition | boolean | true | Indicates 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 |
duration
propOnce 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.
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.
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
}}
/>
)
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.
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:
mm:ss
(minutes and seconds)const children = ({ remainingTime }) => {
const minutes = Math.floor(remainingTime / 60)
const seconds = remainingTime % 60
return `${minutes}:${seconds}`
}
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}`
}
a11y
supportView
element and add the following attributes accessible={true} accessibilityLabel={your-aria-abel}
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>
)
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>