@usefy/use-timer 0.0.1

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,275 @@
1
+ /**
2
+ * Supported time format types for display
3
+ */
4
+ type TimeFormat = "HH:MM:SS" | "HH:MM:SS.SSS" | "MM:SS" | "mm:ss.SSS" | "SS";
5
+ /**
6
+ * Custom time formatter function type
7
+ */
8
+ type TimeFormatter = (timeMs: number) => string;
9
+ /**
10
+ * Timer status states
11
+ */
12
+ type TimerStatus = "idle" | "running" | "paused" | "finished";
13
+ /**
14
+ * Options for useTimer hook
15
+ */
16
+ interface UseTimerOptions {
17
+ /**
18
+ * Update interval in milliseconds
19
+ * @default 100
20
+ */
21
+ interval?: number;
22
+ /**
23
+ * Time format preset or custom formatter function
24
+ * @default "MM:SS"
25
+ */
26
+ format?: TimeFormat | TimeFormatter;
27
+ /**
28
+ * Whether to start the timer automatically on mount
29
+ * @default false
30
+ */
31
+ autoStart?: boolean;
32
+ /**
33
+ * Whether to loop the timer when it completes
34
+ * @default false
35
+ */
36
+ loop?: boolean;
37
+ /**
38
+ * Whether to use requestAnimationFrame for smoother animations
39
+ * When true, ignores interval option and syncs with display refresh rate
40
+ * @default false
41
+ */
42
+ useRAF?: boolean;
43
+ /**
44
+ * Called when the timer completes (time reaches 0)
45
+ */
46
+ onComplete?: () => void;
47
+ /**
48
+ * Called on each tick with the remaining time in milliseconds
49
+ */
50
+ onTick?: (remainingMs: number) => void;
51
+ /**
52
+ * Called when the timer starts
53
+ */
54
+ onStart?: () => void;
55
+ /**
56
+ * Called when the timer pauses
57
+ */
58
+ onPause?: () => void;
59
+ /**
60
+ * Called when the timer resets
61
+ */
62
+ onReset?: () => void;
63
+ /**
64
+ * Called when the timer stops
65
+ */
66
+ onStop?: () => void;
67
+ }
68
+ /**
69
+ * Return type for useTimer hook
70
+ */
71
+ interface UseTimerReturn {
72
+ /**
73
+ * Remaining time in milliseconds
74
+ */
75
+ time: number;
76
+ /**
77
+ * Initial time in milliseconds
78
+ */
79
+ initialTime: number;
80
+ /**
81
+ * Formatted time string based on format option
82
+ */
83
+ formattedTime: string;
84
+ /**
85
+ * Progress percentage (0-100)
86
+ * 0 at start, 100 when complete
87
+ */
88
+ progress: number;
89
+ /**
90
+ * Current timer status
91
+ */
92
+ status: TimerStatus;
93
+ /**
94
+ * Whether the timer is currently running
95
+ */
96
+ isRunning: boolean;
97
+ /**
98
+ * Whether the timer is paused
99
+ */
100
+ isPaused: boolean;
101
+ /**
102
+ * Whether the timer has finished (time = 0)
103
+ */
104
+ isFinished: boolean;
105
+ /**
106
+ * Whether the timer is idle (never started or after reset)
107
+ */
108
+ isIdle: boolean;
109
+ /**
110
+ * Hours component (0+)
111
+ */
112
+ hours: number;
113
+ /**
114
+ * Minutes component (0-59)
115
+ */
116
+ minutes: number;
117
+ /**
118
+ * Seconds component (0-59)
119
+ */
120
+ seconds: number;
121
+ /**
122
+ * Milliseconds component (0-999)
123
+ */
124
+ milliseconds: number;
125
+ /**
126
+ * Start or resume the timer
127
+ */
128
+ start: () => void;
129
+ /**
130
+ * Pause the timer (preserves remaining time)
131
+ */
132
+ pause: () => void;
133
+ /**
134
+ * Stop the timer (preserves remaining time, resets status to idle)
135
+ */
136
+ stop: () => void;
137
+ /**
138
+ * Reset the timer to initial time
139
+ */
140
+ reset: () => void;
141
+ /**
142
+ * Reset and start immediately
143
+ */
144
+ restart: () => void;
145
+ /**
146
+ * Toggle between running and paused states
147
+ */
148
+ toggle: () => void;
149
+ /**
150
+ * Add time in milliseconds
151
+ */
152
+ addTime: (ms: number) => void;
153
+ /**
154
+ * Subtract time in milliseconds (minimum 0)
155
+ */
156
+ subtractTime: (ms: number) => void;
157
+ /**
158
+ * Set time to a specific value in milliseconds
159
+ */
160
+ setTime: (ms: number) => void;
161
+ }
162
+
163
+ /**
164
+ * Helper object to create milliseconds from various time units
165
+ *
166
+ * @example
167
+ * ```tsx
168
+ * import { useTimer, ms } from "@usefy/use-timer";
169
+ *
170
+ * // 5 minute timer
171
+ * const timer = useTimer(ms.minutes(5));
172
+ *
173
+ * // 1 hour 30 minutes timer
174
+ * const timer = useTimer(ms.hours(1) + ms.minutes(30));
175
+ * ```
176
+ */
177
+ declare const ms: {
178
+ /** Convert seconds to milliseconds */
179
+ readonly seconds: (n: number) => number;
180
+ /** Convert minutes to milliseconds */
181
+ readonly minutes: (n: number) => number;
182
+ /** Convert hours to milliseconds */
183
+ readonly hours: (n: number) => number;
184
+ };
185
+ /**
186
+ * A powerful countdown timer hook with comprehensive controls and features.
187
+ *
188
+ * @param initialTimeMs - Initial time in milliseconds
189
+ * @param options - Timer configuration options
190
+ * @returns Timer state and control functions
191
+ *
192
+ * @example
193
+ * ```tsx
194
+ * // Basic usage
195
+ * function Timer() {
196
+ * const timer = useTimer(60000); // 60 seconds
197
+ *
198
+ * return (
199
+ * <div>
200
+ * <p>{timer.formattedTime}</p>
201
+ * <button onClick={timer.toggle}>
202
+ * {timer.isRunning ? "Pause" : "Start"}
203
+ * </button>
204
+ * <button onClick={timer.reset}>Reset</button>
205
+ * </div>
206
+ * );
207
+ * }
208
+ * ```
209
+ *
210
+ * @example
211
+ * ```tsx
212
+ * // With callbacks and auto-start
213
+ * const timer = useTimer(30000, {
214
+ * autoStart: true,
215
+ * onComplete: () => alert("Time's up!"),
216
+ * onTick: (remaining) => console.log(`${remaining}ms left`),
217
+ * });
218
+ * ```
219
+ *
220
+ * @example
221
+ * ```tsx
222
+ * // With time helpers
223
+ * import { useTimer, ms } from "@usefy/use-timer";
224
+ *
225
+ * const timer = useTimer(ms.minutes(5), {
226
+ * format: "MM:SS",
227
+ * loop: true,
228
+ * });
229
+ * ```
230
+ */
231
+ declare function useTimer(initialTimeMs: number, options?: UseTimerOptions): UseTimerReturn;
232
+
233
+ /**
234
+ * Supported time units for utility functions
235
+ */
236
+ type TimeUnit = "ms" | "seconds" | "minutes" | "hours";
237
+ /**
238
+ * Convert a time value to milliseconds
239
+ * @param time - The time value to convert
240
+ * @param unit - The unit of the time value
241
+ * @returns Time in milliseconds (minimum 0)
242
+ */
243
+ declare function toMs(time: number, unit: TimeUnit): number;
244
+ /**
245
+ * Convert milliseconds to a specific time unit
246
+ * @param ms - Time in milliseconds
247
+ * @param unit - Target time unit
248
+ * @returns Time in the specified unit
249
+ */
250
+ declare function fromMs(ms: number, unit: TimeUnit): number;
251
+ /**
252
+ * Decomposed time object
253
+ */
254
+ interface DecomposedTime {
255
+ hours: number;
256
+ minutes: number;
257
+ seconds: number;
258
+ milliseconds: number;
259
+ }
260
+ /**
261
+ * Decompose milliseconds into hours, minutes, seconds, and milliseconds
262
+ * @param ms - Time in milliseconds
263
+ * @returns Decomposed time object
264
+ */
265
+ declare function decompose(ms: number): DecomposedTime;
266
+
267
+ /**
268
+ * Format milliseconds into a time string
269
+ * @param ms - Time in milliseconds
270
+ * @param format - Desired format
271
+ * @returns Formatted time string
272
+ */
273
+ declare function formatTime(ms: number, format: TimeFormat): string;
274
+
275
+ export { type DecomposedTime, type TimeFormat, type TimeFormatter, type TimeUnit, type TimerStatus, type UseTimerOptions, type UseTimerReturn, decompose, formatTime, fromMs, ms, toMs, useTimer };
@@ -0,0 +1,275 @@
1
+ /**
2
+ * Supported time format types for display
3
+ */
4
+ type TimeFormat = "HH:MM:SS" | "HH:MM:SS.SSS" | "MM:SS" | "mm:ss.SSS" | "SS";
5
+ /**
6
+ * Custom time formatter function type
7
+ */
8
+ type TimeFormatter = (timeMs: number) => string;
9
+ /**
10
+ * Timer status states
11
+ */
12
+ type TimerStatus = "idle" | "running" | "paused" | "finished";
13
+ /**
14
+ * Options for useTimer hook
15
+ */
16
+ interface UseTimerOptions {
17
+ /**
18
+ * Update interval in milliseconds
19
+ * @default 100
20
+ */
21
+ interval?: number;
22
+ /**
23
+ * Time format preset or custom formatter function
24
+ * @default "MM:SS"
25
+ */
26
+ format?: TimeFormat | TimeFormatter;
27
+ /**
28
+ * Whether to start the timer automatically on mount
29
+ * @default false
30
+ */
31
+ autoStart?: boolean;
32
+ /**
33
+ * Whether to loop the timer when it completes
34
+ * @default false
35
+ */
36
+ loop?: boolean;
37
+ /**
38
+ * Whether to use requestAnimationFrame for smoother animations
39
+ * When true, ignores interval option and syncs with display refresh rate
40
+ * @default false
41
+ */
42
+ useRAF?: boolean;
43
+ /**
44
+ * Called when the timer completes (time reaches 0)
45
+ */
46
+ onComplete?: () => void;
47
+ /**
48
+ * Called on each tick with the remaining time in milliseconds
49
+ */
50
+ onTick?: (remainingMs: number) => void;
51
+ /**
52
+ * Called when the timer starts
53
+ */
54
+ onStart?: () => void;
55
+ /**
56
+ * Called when the timer pauses
57
+ */
58
+ onPause?: () => void;
59
+ /**
60
+ * Called when the timer resets
61
+ */
62
+ onReset?: () => void;
63
+ /**
64
+ * Called when the timer stops
65
+ */
66
+ onStop?: () => void;
67
+ }
68
+ /**
69
+ * Return type for useTimer hook
70
+ */
71
+ interface UseTimerReturn {
72
+ /**
73
+ * Remaining time in milliseconds
74
+ */
75
+ time: number;
76
+ /**
77
+ * Initial time in milliseconds
78
+ */
79
+ initialTime: number;
80
+ /**
81
+ * Formatted time string based on format option
82
+ */
83
+ formattedTime: string;
84
+ /**
85
+ * Progress percentage (0-100)
86
+ * 0 at start, 100 when complete
87
+ */
88
+ progress: number;
89
+ /**
90
+ * Current timer status
91
+ */
92
+ status: TimerStatus;
93
+ /**
94
+ * Whether the timer is currently running
95
+ */
96
+ isRunning: boolean;
97
+ /**
98
+ * Whether the timer is paused
99
+ */
100
+ isPaused: boolean;
101
+ /**
102
+ * Whether the timer has finished (time = 0)
103
+ */
104
+ isFinished: boolean;
105
+ /**
106
+ * Whether the timer is idle (never started or after reset)
107
+ */
108
+ isIdle: boolean;
109
+ /**
110
+ * Hours component (0+)
111
+ */
112
+ hours: number;
113
+ /**
114
+ * Minutes component (0-59)
115
+ */
116
+ minutes: number;
117
+ /**
118
+ * Seconds component (0-59)
119
+ */
120
+ seconds: number;
121
+ /**
122
+ * Milliseconds component (0-999)
123
+ */
124
+ milliseconds: number;
125
+ /**
126
+ * Start or resume the timer
127
+ */
128
+ start: () => void;
129
+ /**
130
+ * Pause the timer (preserves remaining time)
131
+ */
132
+ pause: () => void;
133
+ /**
134
+ * Stop the timer (preserves remaining time, resets status to idle)
135
+ */
136
+ stop: () => void;
137
+ /**
138
+ * Reset the timer to initial time
139
+ */
140
+ reset: () => void;
141
+ /**
142
+ * Reset and start immediately
143
+ */
144
+ restart: () => void;
145
+ /**
146
+ * Toggle between running and paused states
147
+ */
148
+ toggle: () => void;
149
+ /**
150
+ * Add time in milliseconds
151
+ */
152
+ addTime: (ms: number) => void;
153
+ /**
154
+ * Subtract time in milliseconds (minimum 0)
155
+ */
156
+ subtractTime: (ms: number) => void;
157
+ /**
158
+ * Set time to a specific value in milliseconds
159
+ */
160
+ setTime: (ms: number) => void;
161
+ }
162
+
163
+ /**
164
+ * Helper object to create milliseconds from various time units
165
+ *
166
+ * @example
167
+ * ```tsx
168
+ * import { useTimer, ms } from "@usefy/use-timer";
169
+ *
170
+ * // 5 minute timer
171
+ * const timer = useTimer(ms.minutes(5));
172
+ *
173
+ * // 1 hour 30 minutes timer
174
+ * const timer = useTimer(ms.hours(1) + ms.minutes(30));
175
+ * ```
176
+ */
177
+ declare const ms: {
178
+ /** Convert seconds to milliseconds */
179
+ readonly seconds: (n: number) => number;
180
+ /** Convert minutes to milliseconds */
181
+ readonly minutes: (n: number) => number;
182
+ /** Convert hours to milliseconds */
183
+ readonly hours: (n: number) => number;
184
+ };
185
+ /**
186
+ * A powerful countdown timer hook with comprehensive controls and features.
187
+ *
188
+ * @param initialTimeMs - Initial time in milliseconds
189
+ * @param options - Timer configuration options
190
+ * @returns Timer state and control functions
191
+ *
192
+ * @example
193
+ * ```tsx
194
+ * // Basic usage
195
+ * function Timer() {
196
+ * const timer = useTimer(60000); // 60 seconds
197
+ *
198
+ * return (
199
+ * <div>
200
+ * <p>{timer.formattedTime}</p>
201
+ * <button onClick={timer.toggle}>
202
+ * {timer.isRunning ? "Pause" : "Start"}
203
+ * </button>
204
+ * <button onClick={timer.reset}>Reset</button>
205
+ * </div>
206
+ * );
207
+ * }
208
+ * ```
209
+ *
210
+ * @example
211
+ * ```tsx
212
+ * // With callbacks and auto-start
213
+ * const timer = useTimer(30000, {
214
+ * autoStart: true,
215
+ * onComplete: () => alert("Time's up!"),
216
+ * onTick: (remaining) => console.log(`${remaining}ms left`),
217
+ * });
218
+ * ```
219
+ *
220
+ * @example
221
+ * ```tsx
222
+ * // With time helpers
223
+ * import { useTimer, ms } from "@usefy/use-timer";
224
+ *
225
+ * const timer = useTimer(ms.minutes(5), {
226
+ * format: "MM:SS",
227
+ * loop: true,
228
+ * });
229
+ * ```
230
+ */
231
+ declare function useTimer(initialTimeMs: number, options?: UseTimerOptions): UseTimerReturn;
232
+
233
+ /**
234
+ * Supported time units for utility functions
235
+ */
236
+ type TimeUnit = "ms" | "seconds" | "minutes" | "hours";
237
+ /**
238
+ * Convert a time value to milliseconds
239
+ * @param time - The time value to convert
240
+ * @param unit - The unit of the time value
241
+ * @returns Time in milliseconds (minimum 0)
242
+ */
243
+ declare function toMs(time: number, unit: TimeUnit): number;
244
+ /**
245
+ * Convert milliseconds to a specific time unit
246
+ * @param ms - Time in milliseconds
247
+ * @param unit - Target time unit
248
+ * @returns Time in the specified unit
249
+ */
250
+ declare function fromMs(ms: number, unit: TimeUnit): number;
251
+ /**
252
+ * Decomposed time object
253
+ */
254
+ interface DecomposedTime {
255
+ hours: number;
256
+ minutes: number;
257
+ seconds: number;
258
+ milliseconds: number;
259
+ }
260
+ /**
261
+ * Decompose milliseconds into hours, minutes, seconds, and milliseconds
262
+ * @param ms - Time in milliseconds
263
+ * @returns Decomposed time object
264
+ */
265
+ declare function decompose(ms: number): DecomposedTime;
266
+
267
+ /**
268
+ * Format milliseconds into a time string
269
+ * @param ms - Time in milliseconds
270
+ * @param format - Desired format
271
+ * @returns Formatted time string
272
+ */
273
+ declare function formatTime(ms: number, format: TimeFormat): string;
274
+
275
+ export { type DecomposedTime, type TimeFormat, type TimeFormatter, type TimeUnit, type TimerStatus, type UseTimerOptions, type UseTimerReturn, decompose, formatTime, fromMs, ms, toMs, useTimer };