@rific/timer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jay Deaton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @rific/timer
2
+
3
+ Animated SVG progress ring timer for React Native.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @rific/timer react-native-svg
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```tsx
14
+ import { Timer } from '@rific/timer'
15
+ import { Text } from 'react-native'
16
+
17
+ const [started, setStarted] = useState<string | null>(null)
18
+
19
+ <Timer
20
+ color='#6200ee'
21
+ duration={30}
22
+ radius={28}
23
+ started={started}
24
+ onStart={() => console.log('started')}
25
+ onStop={() => setStarted(null)}
26
+ >
27
+ <Text>30s</Text>
28
+ </Timer>
29
+
30
+ <Button title='Start' onPress={() => setStarted(new Date().toISOString())} />
31
+ ```
32
+
33
+ ## Props
34
+
35
+ | Prop | Type | Required | Default | Description |
36
+ |---|---|---|---|---|
37
+ | `color` | `string` | ✓ | — | Stroke color of the progress ring |
38
+ | `duration` | `number` | ✓ | — | Timer duration in seconds |
39
+ | `radius` | `number` | ✓ | — | Radius of the SVG ring in pixels |
40
+ | `started` | `string \| null` | ✓ | — | ISO timestamp when the timer started; `null` to stop |
41
+ | `children` | `ReactNode` | | `undefined` | Rendered in the center of the ring |
42
+ | `onStart` | `() => void` | | `undefined` | Called when the timer starts |
43
+ | `onStop` | `() => void` | | `undefined` | Called when the timer reaches 100% |
44
+ | `startProgress` | `number` | | `0` | Starting progress (0–1) |
45
+ | `style` | `ViewStyle` | | `undefined` | Style applied to the container |
46
+ | `width` | `number` | | `6` | Stroke width of the ring |
47
+
48
+ ## How it works
49
+
50
+ Pass an ISO timestamp string to `started` to begin the countdown. Set `started` to `null` to reset. The ring animates from empty to full over `duration` seconds and calls `onStop` on completion.
51
+
52
+ ## Peer dependencies
53
+
54
+ - `react >= 17.0.0`
55
+ - `react-native >= 0.70.0`
56
+ - `react-native-svg >= 13.0.0`
@@ -0,0 +1,18 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { ViewStyle } from 'react-native';
3
+
4
+ type TimerProps = {
5
+ children?: ReactNode;
6
+ color: string;
7
+ duration: number;
8
+ onStart?: () => void;
9
+ onStop?: () => void;
10
+ radius: number;
11
+ startProgress?: number;
12
+ started: string | null;
13
+ style?: ViewStyle;
14
+ width?: number;
15
+ };
16
+ declare const Timer: ({ children, color, duration, onStart, onStop, radius, startProgress, started, style, width }: TimerProps) => React.JSX.Element;
17
+
18
+ export { Timer, type TimerProps };
@@ -0,0 +1,18 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { ViewStyle } from 'react-native';
3
+
4
+ type TimerProps = {
5
+ children?: ReactNode;
6
+ color: string;
7
+ duration: number;
8
+ onStart?: () => void;
9
+ onStop?: () => void;
10
+ radius: number;
11
+ startProgress?: number;
12
+ started: string | null;
13
+ style?: ViewStyle;
14
+ width?: number;
15
+ };
16
+ declare const Timer: ({ children, color, duration, onStart, onStop, radius, startProgress, started, style, width }: TimerProps) => React.JSX.Element;
17
+
18
+ export { Timer, type TimerProps };
package/dist/index.js ADDED
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Timer: () => Timer
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/Timer.tsx
38
+ var import_react = __toESM(require("react"));
39
+ var import_react_native = require("react-native");
40
+ var import_react_native_svg = __toESM(require("react-native-svg"));
41
+ var AnimatedCircle = import_react_native.Animated.createAnimatedComponent(import_react_native_svg.Circle);
42
+ var useNativeDriver = true;
43
+ var Timer = ({ children, color, duration, onStart, onStop, radius, startProgress = 0, started, style, width = 6 }) => {
44
+ const durationRef = (0, import_react.useRef)(duration);
45
+ const progressRef = (0, import_react.useRef)(startProgress);
46
+ const startedRef = (0, import_react.useRef)(null);
47
+ const circumference = 2 * Math.PI * radius;
48
+ const animation = (0, import_react.useRef)(new import_react_native.Animated.Value(0)).current;
49
+ const animatedProps = {
50
+ strokeDashoffset: animation.interpolate({
51
+ inputRange: [0, 1],
52
+ outputRange: [circumference, 0]
53
+ })
54
+ };
55
+ const startTimer = (0, import_react.useCallback)(
56
+ (callback) => {
57
+ const clampedProgress = Math.max(0, Math.min(1, startProgress));
58
+ const remainingDurationMs = Math.max(0, duration * 1e3 * (1 - clampedProgress));
59
+ animation.setValue(clampedProgress);
60
+ if (onStart) onStart();
61
+ import_react_native.Animated.timing(animation, {
62
+ toValue: 1,
63
+ duration: remainingDurationMs,
64
+ easing: import_react_native.Easing.linear,
65
+ useNativeDriver
66
+ }).start(({ finished }) => {
67
+ if (finished && onStop) onStop();
68
+ if (callback) callback();
69
+ });
70
+ },
71
+ [animation, duration, onStart, onStop, startProgress]
72
+ );
73
+ const stopTimer = (0, import_react.useCallback)(
74
+ (callback) => {
75
+ animation.stopAnimation();
76
+ import_react_native.Animated.spring(animation, {
77
+ toValue: 0,
78
+ useNativeDriver
79
+ }).start(callback);
80
+ },
81
+ [animation]
82
+ );
83
+ (0, import_react.useEffect)(() => {
84
+ const startedChanged = started !== startedRef.current;
85
+ const durationChanged = duration !== durationRef.current;
86
+ const progressChanged = startProgress !== progressRef.current;
87
+ if (duration === 0) {
88
+ stopTimer();
89
+ } else if (started) {
90
+ if (startedChanged || durationChanged || progressChanged) {
91
+ animation.stopAnimation(() => startTimer());
92
+ }
93
+ } else {
94
+ stopTimer();
95
+ }
96
+ durationRef.current = duration;
97
+ progressRef.current = startProgress;
98
+ startedRef.current = started;
99
+ }, [animation, duration, startProgress, started, startTimer, stopTimer]);
100
+ return /* @__PURE__ */ import_react.default.createElement(import_react_native.View, { style: [style, styles.root] }, /* @__PURE__ */ import_react.default.createElement(import_react_native_svg.default, { width: radius * 2 + width, height: radius * 2 + width, viewBox: `0 0 ${radius * 2 + width} ${radius * 2 + width}`, style: styles.svg }, /* @__PURE__ */ import_react.default.createElement(import_react_native_svg.G, { rotation: "-90", origin: `${radius + width / 2}, ${radius + width / 2}` }, /* @__PURE__ */ import_react.default.createElement(AnimatedCircle, { cx: radius + width / 2, cy: radius + width / 2, r: radius, stroke: color, strokeWidth: width, fill: "none", strokeDasharray: circumference, ...animatedProps }))), /* @__PURE__ */ import_react.default.createElement(import_react_native.View, { style: styles.children }, children));
101
+ };
102
+ var styles = import_react_native.StyleSheet.create({
103
+ children: {
104
+ position: "absolute"
105
+ },
106
+ root: {
107
+ alignItems: "center",
108
+ justifyContent: "center"
109
+ },
110
+ svg: {}
111
+ });
112
+ // Annotate the CommonJS export names for ESM import in node:
113
+ 0 && (module.exports = {
114
+ Timer
115
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,78 @@
1
+ // src/Timer.tsx
2
+ import React, { useCallback, useEffect, useRef } from "react";
3
+ import { Animated, Easing, StyleSheet, View } from "react-native";
4
+ import Svg, { Circle, G } from "react-native-svg";
5
+ var AnimatedCircle = Animated.createAnimatedComponent(Circle);
6
+ var useNativeDriver = true;
7
+ var Timer = ({ children, color, duration, onStart, onStop, radius, startProgress = 0, started, style, width = 6 }) => {
8
+ const durationRef = useRef(duration);
9
+ const progressRef = useRef(startProgress);
10
+ const startedRef = useRef(null);
11
+ const circumference = 2 * Math.PI * radius;
12
+ const animation = useRef(new Animated.Value(0)).current;
13
+ const animatedProps = {
14
+ strokeDashoffset: animation.interpolate({
15
+ inputRange: [0, 1],
16
+ outputRange: [circumference, 0]
17
+ })
18
+ };
19
+ const startTimer = useCallback(
20
+ (callback) => {
21
+ const clampedProgress = Math.max(0, Math.min(1, startProgress));
22
+ const remainingDurationMs = Math.max(0, duration * 1e3 * (1 - clampedProgress));
23
+ animation.setValue(clampedProgress);
24
+ if (onStart) onStart();
25
+ Animated.timing(animation, {
26
+ toValue: 1,
27
+ duration: remainingDurationMs,
28
+ easing: Easing.linear,
29
+ useNativeDriver
30
+ }).start(({ finished }) => {
31
+ if (finished && onStop) onStop();
32
+ if (callback) callback();
33
+ });
34
+ },
35
+ [animation, duration, onStart, onStop, startProgress]
36
+ );
37
+ const stopTimer = useCallback(
38
+ (callback) => {
39
+ animation.stopAnimation();
40
+ Animated.spring(animation, {
41
+ toValue: 0,
42
+ useNativeDriver
43
+ }).start(callback);
44
+ },
45
+ [animation]
46
+ );
47
+ useEffect(() => {
48
+ const startedChanged = started !== startedRef.current;
49
+ const durationChanged = duration !== durationRef.current;
50
+ const progressChanged = startProgress !== progressRef.current;
51
+ if (duration === 0) {
52
+ stopTimer();
53
+ } else if (started) {
54
+ if (startedChanged || durationChanged || progressChanged) {
55
+ animation.stopAnimation(() => startTimer());
56
+ }
57
+ } else {
58
+ stopTimer();
59
+ }
60
+ durationRef.current = duration;
61
+ progressRef.current = startProgress;
62
+ startedRef.current = started;
63
+ }, [animation, duration, startProgress, started, startTimer, stopTimer]);
64
+ return /* @__PURE__ */ React.createElement(View, { style: [style, styles.root] }, /* @__PURE__ */ React.createElement(Svg, { width: radius * 2 + width, height: radius * 2 + width, viewBox: `0 0 ${radius * 2 + width} ${radius * 2 + width}`, style: styles.svg }, /* @__PURE__ */ React.createElement(G, { rotation: "-90", origin: `${radius + width / 2}, ${radius + width / 2}` }, /* @__PURE__ */ React.createElement(AnimatedCircle, { cx: radius + width / 2, cy: radius + width / 2, r: radius, stroke: color, strokeWidth: width, fill: "none", strokeDasharray: circumference, ...animatedProps }))), /* @__PURE__ */ React.createElement(View, { style: styles.children }, children));
65
+ };
66
+ var styles = StyleSheet.create({
67
+ children: {
68
+ position: "absolute"
69
+ },
70
+ root: {
71
+ alignItems: "center",
72
+ justifyContent: "center"
73
+ },
74
+ svg: {}
75
+ });
76
+ export {
77
+ Timer
78
+ };
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@rific/timer",
3
+ "version": "0.1.0",
4
+ "description": "Animated SVG progress ring timer for React Native",
5
+ "keywords": [
6
+ "react-native",
7
+ "timer",
8
+ "countdown",
9
+ "progress-ring",
10
+ "circular-progress",
11
+ "react-native-svg",
12
+ "svg",
13
+ "countdown-timer"
14
+ ],
15
+ "homepage": "https://github.com/jayrdeaton/react-native-timer#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/jayrdeaton/react-native-timer/issues"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/jayrdeaton/react-native-timer"
22
+ },
23
+ "license": "MIT",
24
+ "author": "Jay Deaton",
25
+ "sideEffects": false,
26
+ "type": "commonjs",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.mjs",
31
+ "require": "./dist/index.js"
32
+ }
33
+ },
34
+ "main": "dist/index.js",
35
+ "module": "dist/index.mjs",
36
+ "types": "dist/index.d.ts",
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "scripts": {
41
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
42
+ "fix": "eslint --fix",
43
+ "lint": "eslint",
44
+ "prepublishOnly": "npm run build",
45
+ "release": "git push --follow-tags",
46
+ "release:major": "npm version major && git push --follow-tags",
47
+ "release:minor": "npm version minor && git push --follow-tags",
48
+ "release:patch": "npm version patch && git push --follow-tags",
49
+ "test": "jest",
50
+ "test:watch": "jest --watchAll",
51
+ "typecheck": "tsc --noEmit",
52
+ "preversion": "npm run lint && npm test"
53
+ },
54
+ "devDependencies": {
55
+ "@testing-library/dom": "^10.4.1",
56
+ "@testing-library/react": "^16.3.2",
57
+ "@types/jest": "^30.0.0",
58
+ "@types/react": "^19.0.0",
59
+ "@typescript-eslint/parser": "^8.59.3",
60
+ "eslint": "^9.39.4",
61
+ "eslint-config-prettier": "^10.1.8",
62
+ "eslint-plugin-package-json": "^1.0.0",
63
+ "eslint-plugin-prettier": "^5.5.5",
64
+ "eslint-plugin-react-native": "^5.0.0",
65
+ "eslint-plugin-simple-import-sort": "^13.0.0",
66
+ "jest": "^30.4.2",
67
+ "jest-environment-jsdom": "^30.4.1",
68
+ "prettier": "^3.8.3",
69
+ "react": "^19.2.6",
70
+ "react-dom": "^19.0.0",
71
+ "react-native": "^0.85.3",
72
+ "react-native-svg": "^15.15.5",
73
+ "ts-jest": "^29.4.9",
74
+ "ts-node": "^10.9.2",
75
+ "tsup": "^8.0.0",
76
+ "typescript": "^5.9.3",
77
+ "typescript-eslint": "^8.59.3"
78
+ },
79
+ "peerDependencies": {
80
+ "react": ">=17.0.0",
81
+ "react-native": ">=0.70.0",
82
+ "react-native-svg": ">=13.0.0"
83
+ },
84
+ "publishConfig": {
85
+ "access": "public"
86
+ }
87
+ }