@dcl/hooks 1.0.0 → 1.0.1-20251223125619.commit-1446f8b
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.
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
type UseCounterTimerOptions = {
|
|
2
|
+
/** Interval in milliseconds between each tick. Default: 1000 */
|
|
3
|
+
interval?: number;
|
|
4
|
+
/** Target value to count towards. Default: 0 */
|
|
5
|
+
target?: number;
|
|
6
|
+
/** Callback when counter reaches target. Default: undefined */
|
|
7
|
+
onComplete?: () => void;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Counter hook that counts up or down towards a target value.
|
|
11
|
+
* Direction is automatically determined by comparing initial value to target.
|
|
12
|
+
*
|
|
13
|
+
* @param initialValue - Starting value for the counter
|
|
14
|
+
* @param options - Configuration options for the counter
|
|
15
|
+
* @param options.interval - Interval in milliseconds between each tick. Default: 1000
|
|
16
|
+
* @param options.target - Target value to count towards. Default: 0
|
|
17
|
+
* @param options.onComplete - Callback when counter reaches target
|
|
18
|
+
* @returns Object containing count (current value), start/stop/reset functions, and isActive state
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // Count down from 10 to 0
|
|
22
|
+
* const { count, start } = useCounterTimer(10);
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Count up from 50 to 100
|
|
26
|
+
* const { count, start } = useCounterTimer(50, { target: 100 });
|
|
27
|
+
*/
|
|
28
|
+
declare const useCounterTimer: (initialValue?: number, options?: UseCounterTimerOptions) => {
|
|
29
|
+
count: number;
|
|
30
|
+
start: (value?: number) => void;
|
|
31
|
+
stop: () => void;
|
|
32
|
+
reset: (value?: number) => void;
|
|
33
|
+
isActive: boolean;
|
|
34
|
+
};
|
|
35
|
+
export { useCounterTimer };
|
|
36
|
+
export type { UseCounterTimerOptions };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Counter hook that counts up or down towards a target value.
|
|
4
|
+
* Direction is automatically determined by comparing initial value to target.
|
|
5
|
+
*
|
|
6
|
+
* @param initialValue - Starting value for the counter
|
|
7
|
+
* @param options - Configuration options for the counter
|
|
8
|
+
* @param options.interval - Interval in milliseconds between each tick. Default: 1000
|
|
9
|
+
* @param options.target - Target value to count towards. Default: 0
|
|
10
|
+
* @param options.onComplete - Callback when counter reaches target
|
|
11
|
+
* @returns Object containing count (current value), start/stop/reset functions, and isActive state
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Count down from 10 to 0
|
|
15
|
+
* const { count, start } = useCounterTimer(10);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Count up from 50 to 100
|
|
19
|
+
* const { count, start } = useCounterTimer(50, { target: 100 });
|
|
20
|
+
*/
|
|
21
|
+
const useCounterTimer = (initialValue = 0, options = {}) => {
|
|
22
|
+
const { interval = 1000, onComplete, target = 0 } = options;
|
|
23
|
+
const [count, setCount] = useState(initialValue);
|
|
24
|
+
const [isActive, setIsActive] = useState(false);
|
|
25
|
+
const onCompleteRef = useRef(onComplete);
|
|
26
|
+
const isCountingUp = initialValue < target;
|
|
27
|
+
const step = isCountingUp ? 1 : -1;
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
onCompleteRef.current = onComplete;
|
|
30
|
+
}, [onComplete]);
|
|
31
|
+
const start = useCallback((value) => {
|
|
32
|
+
if (value !== undefined) {
|
|
33
|
+
setCount(value);
|
|
34
|
+
}
|
|
35
|
+
setIsActive(true);
|
|
36
|
+
}, []);
|
|
37
|
+
const stop = useCallback(() => {
|
|
38
|
+
setIsActive(false);
|
|
39
|
+
}, []);
|
|
40
|
+
const reset = useCallback((value = initialValue) => {
|
|
41
|
+
setCount(value);
|
|
42
|
+
setIsActive(false);
|
|
43
|
+
}, [initialValue]);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (!isActive)
|
|
46
|
+
return;
|
|
47
|
+
const intervalId = setInterval(() => {
|
|
48
|
+
setCount((value) => {
|
|
49
|
+
const newValue = value + step;
|
|
50
|
+
const hasReachedTarget = isCountingUp
|
|
51
|
+
? newValue >= target
|
|
52
|
+
: newValue <= target;
|
|
53
|
+
if (hasReachedTarget) {
|
|
54
|
+
setIsActive(false);
|
|
55
|
+
onCompleteRef.current?.();
|
|
56
|
+
return target;
|
|
57
|
+
}
|
|
58
|
+
return newValue;
|
|
59
|
+
});
|
|
60
|
+
}, interval);
|
|
61
|
+
return () => {
|
|
62
|
+
clearInterval(intervalId);
|
|
63
|
+
};
|
|
64
|
+
}, [isActive, interval, step, target, isCountingUp]);
|
|
65
|
+
return {
|
|
66
|
+
count,
|
|
67
|
+
start,
|
|
68
|
+
stop,
|
|
69
|
+
reset,
|
|
70
|
+
isActive,
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
export { useCounterTimer };
|
|
74
|
+
//# sourceMappingURL=useCounterTimer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCounterTimer.js","sourceRoot":"","sources":["../../src/hooks/useCounterTimer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAWhE;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,eAAe,GAAG,CACtB,eAAuB,CAAC,EACxB,UAAkC,EAAE,EACpC,EAAE;IACF,MAAM,EAAE,QAAQ,GAAG,IAAI,EAAE,UAAU,EAAE,MAAM,GAAG,CAAC,EAAE,GAAG,OAAO,CAAA;IAC3D,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAA;IAChD,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC/C,MAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,CAA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A;oBACzB,OAAO,MAAM,CAAA;gBACf,CAAC;gBAED,OAAO,QAAQ,CAAA;YACjB,CAAC,CAAC,CAAA;QACJ,CAAC,EAAE,QAAQ,CAAC,CAAA;QAEZ,OAAO,GAAG,EAAE;YACV,aAAa,CAAC,UAAU,CAAC,CAAA;QAC3B,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAA;IAEpD,OAAO;QACL,KAAK;QACL,KAAK;QACL,IAAI;QACJ,KAAK;QACL,QAAQ;KACT,CAAA;AACH,CAAC,CAAA;AAED,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcl/hooks",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1-20251223125619.commit-1446f8b",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "esm/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"<rootDir>/test/setup.ts"
|
|
78
78
|
]
|
|
79
79
|
},
|
|
80
|
-
"commit": "
|
|
80
|
+
"commit": "1446f8b56b46bc5df904fb4755a45341bab801ff"
|
|
81
81
|
}
|