@react-hive/honey-layout 10.6.0 → 10.8.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.
@@ -1,36 +1,37 @@
1
- interface HoneyRafCallbackContext {
1
+ interface HoneyRafOnFrameContext {
2
2
  /**
3
- * Stops the currently running RAF loop.
3
+ * Immediately terminates the active `requestAnimationFrame` loop.
4
4
  *
5
- * Calling this function immediately:
6
- * - Cancels the next animation frame
7
- * - Resets internal timing state
8
- * - Sets `isRafLoopRunning` to `false`
5
+ * This method is **safe to call synchronously from within the frame handler**
6
+ * and is the **recommended mechanism** for ending frame-driven processes based on runtime conditions.
9
7
  *
10
- * This function is safe to call from within the RAF callback
11
- * and is the preferred way to terminate the loop based on
12
- * frame-driven conditions (e.g. inertia decay, animation completion).
8
+ * Typical use cases:
9
+ * - Inertia or momentum decay reaching a threshold
10
+ * - Animation or transition completion
11
+ * - Time- or state-based termination conditions
13
12
  */
14
13
  stop: () => void;
15
14
  }
16
15
  /**
17
- * RAF callback invoked on every animation frame.
16
+ * Function invoked on every animation frame while the RAF loop is active.
18
17
  *
19
- * The callback is invoked with the elapsed time since the previous
20
- * frame and a control context for managing the RAF loop lifecycle.
18
+ * The handler is expected to be **side-effect driven** and may:
19
+ * - Mutate refs
20
+ * - Update React state
21
+ * - Stop the RAF loop via `context.stop()`
21
22
  *
22
- * ⚠️ Callback stability
23
- * The callback should be wrapped in `useCallback` to ensure
24
- * referential stability and to make dependencies explicit.
23
+ * ⚠️ Referential stability
24
+ * This handler **must be wrapped in `useCallback`** to avoid unnecessary
25
+ * re-bindings and to ensure predictable behavior across renders.
25
26
  *
26
- * @param dtMs - Delta time in milliseconds since the previous frame.
27
- * The value is clamped to `maxDeltaMs` to avoid large jumps
28
- * when the tab is inactive, backgrounded, or the browser
29
- * throttles animation frames.
27
+ * @param deltaTimeMs - Time delta in milliseconds since the previous frame.
28
+ * This value is clamped to `maxDeltaMs` to prevent large
29
+ * time steps caused by tab backgrounding, visibility changes,
30
+ * or browser throttling.
30
31
  *
31
- * @param context - RAF loop control helpers.
32
+ * @param context - RAF lifecycle control context. See {@link HoneyRafOnFrameContext}.
32
33
  */
33
- export type HoneyRafCallback = (dtMs: number, context: HoneyRafCallbackContext) => void;
34
+ export type HoneyRafOnFrameHandler = (deltaTimeMs: number, context: HoneyRafOnFrameContext) => void;
34
35
  /**
35
36
  * Configuration options for {@link useHoneyRafLoop}.
36
37
  */
@@ -84,12 +85,35 @@ interface UseHoneyRafLoopOptions {
84
85
  */
85
86
  onError?: (error: unknown) => void;
86
87
  }
88
+ /**
89
+ * Public control API returned by {@link useHoneyRafLoop}.
90
+ */
91
+ export interface HoneyRafLoopApi {
92
+ /**
93
+ * Indicates whether the RAF loop is currently running.
94
+ */
95
+ isRunning: boolean;
96
+ /**
97
+ * Starts the RAF loop.
98
+ *
99
+ * If the loop is already running, this call is ignored.
100
+ */
101
+ start: () => void;
102
+ /**
103
+ * Stops the RAF loop immediately.
104
+ *
105
+ * This method is safe to call:
106
+ * - From user code
107
+ * - From within the RAF frame handler
108
+ */
109
+ stop: () => void;
110
+ }
87
111
  /**
88
112
  * A hook for running a controlled `requestAnimationFrame` loop.
89
113
  *
90
114
  * Features:
91
115
  * - Explicit RAF lifecycle control (`start` / `stop`)
92
- * - Delta time (`dt`) calculation with frame clamping
116
+ * - Delta time calculation with frame clamping
93
117
  * - Automatic cleanup on unmounting
94
118
  * - Conservative handling of tab visibility changes (mobile-safe)
95
119
  * - Safe error handling (stops loop on exception)
@@ -101,7 +125,7 @@ interface UseHoneyRafLoopOptions {
101
125
  * This hook is designed for gesture handling, inertia, physics simulations,
102
126
  * and animation loops that must not trigger React re-renders on every frame.
103
127
  *
104
- * @param callback - Function invoked on each animation frame.
128
+ * @param onFrame - Function invoked on each animation frame.
105
129
  * @param options - Optional configuration for the RAF loop.
106
130
  *
107
131
  * @returns Control helpers and RAF loop state.
@@ -113,7 +137,7 @@ interface UseHoneyRafLoopOptions {
113
137
  *
114
138
  * const velocityRef = useRef({ x: 12, y: 4 });
115
139
  *
116
- * const onFrame = useCallback<HoneyRafCallback>(
140
+ * const onFrame = useCallback<HoneyRafOnFrameHandler>(
117
141
  * (dtMs, { stop }) => {
118
142
  * velocityRef.current.x *= 0.94;
119
143
  * velocityRef.current.y *= 0.94;
@@ -136,9 +160,5 @@ interface UseHoneyRafLoopOptions {
136
160
  * useHoneyRafLoop(onFrame);
137
161
  * ```
138
162
  */
139
- export declare const useHoneyRafLoop: (callback: HoneyRafCallback, { autoStart, resumeOnVisibility, maxDeltaMs, onError, }?: UseHoneyRafLoopOptions) => {
140
- startRafLoop: () => void;
141
- stopRafLoop: () => void;
142
- isRafLoopRunning: boolean;
143
- };
163
+ export declare const useHoneyRafLoop: (onFrame: HoneyRafOnFrameHandler, { autoStart, resumeOnVisibility, maxDeltaMs, onError, }?: UseHoneyRafLoopOptions) => HoneyRafLoopApi;
144
164
  export {};
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Timer operating mode.
3
+ *
4
+ * - `countdown` — decreases time until it reaches `targetTimeMs`
5
+ * - `countup` — increases time until it reaches `targetTimeMs` (if provided)
6
+ */
7
+ type UseHoneyTimerMode = 'countdown' | 'countup';
8
+ type UseHoneyTimerOnEndHandler = () => void;
9
+ export interface UseHoneyTimerOptions {
10
+ /**
11
+ * Initial timer value in milliseconds.
12
+ *
13
+ * - Countdown: starting time
14
+ * - Count-up: initial offset
15
+ */
16
+ initialTimeMs: number;
17
+ /**
18
+ * Target time in milliseconds.
19
+ *
20
+ * - Countdown: usually `0`
21
+ * - Count-up: optional upper limit
22
+ *
23
+ * When reached, the timer stops and `onEnd` is invoked.
24
+ *
25
+ * @default 0
26
+ */
27
+ targetTimeMs?: number;
28
+ /**
29
+ * Direction in which the timer progresses.
30
+ *
31
+ * @default 'countdown'
32
+ */
33
+ mode?: UseHoneyTimerMode;
34
+ /**
35
+ * Whether the timer should automatically start on mount.
36
+ *
37
+ * @default false
38
+ */
39
+ autoStart?: boolean;
40
+ /**
41
+ * Optional callback invoked exactly once when the timer reaches the target time.
42
+ */
43
+ onEnd?: UseHoneyTimerOnEndHandler;
44
+ }
45
+ /**
46
+ * Public control API returned by {@link useHoneyTimer}.
47
+ */
48
+ export interface UseHoneyTimerApi {
49
+ /**
50
+ * Current timer value in milliseconds.
51
+ *
52
+ * This value updates over time while the timer is running.
53
+ */
54
+ timeMs: number;
55
+ /**
56
+ * Indicates whether the timer is currently progressing.
57
+ */
58
+ isRunning: boolean;
59
+ /**
60
+ * Starts the timer from `initialTimeMs`, resetting any previous state.
61
+ */
62
+ start: () => void;
63
+ /**
64
+ * Pauses the timer while preserving the current time value.
65
+ */
66
+ pause: () => void;
67
+ /**
68
+ * Resumes the timer from its current time value.
69
+ *
70
+ * Has no effect if the timer is already running.
71
+ */
72
+ resume: () => void;
73
+ /**
74
+ * Resets the timer to a specific time value.
75
+ *
76
+ * @param timeMs - Optional new timer value. Defaults to `initialTimeMs`.
77
+ */
78
+ reset: (timeMs?: number) => void;
79
+ }
80
+ /**
81
+ * A high-precision timer hook built on top of {@link useHoneyRafLoop}.
82
+ *
83
+ * Features:
84
+ * - Frame-accurate time progression using `requestAnimationFrame`
85
+ * - Supports countdown and count-up modes
86
+ * - Explicit lifecycle control (start / pause / resume / reset)
87
+ * - Drift-free timing using delta-based updates
88
+ * - Safe completion handling via `onEnd`
89
+ *
90
+ * Architectural notes:
91
+ * - Time progression is handled imperatively via refs to avoid stale closures and unnecessary re-renders.
92
+ * - React state is updated only with the derived timer value.
93
+ * - RAF lifecycle is fully delegated to `useHoneyRafLoop`.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const timer = useHoneyTimer({
98
+ * initialTimeMs: 10_000,
99
+ * targetTimeMs: 0,
100
+ * onEnd: () => console.log('Done'),
101
+ * });
102
+ *
103
+ * timer.startTimer();
104
+ * ```
105
+ */
106
+ export declare const useHoneyTimer: ({ initialTimeMs, targetTimeMs, mode, autoStart, onEnd, }: UseHoneyTimerOptions) => UseHoneyTimerApi;
107
+ export {};