@react-hive/honey-layout 11.1.0 → 11.2.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.
@@ -12,3 +12,4 @@ export * from './use-honey-synthetic-scroll-y';
12
12
  export * from './use-honey-raf-loop';
13
13
  export * from './use-honey-timer';
14
14
  export * from './use-honey-decay';
15
+ export * from './use-honey-latest';
@@ -1,7 +1,8 @@
1
+ import type { InertiaOptions } from '@react-hive/honey-utils';
1
2
  /**
2
3
  * Configuration options for {@link useHoneyDecay}.
3
4
  */
4
- export interface UseHoneyDecayOptions {
5
+ export interface UseHoneyDecayOptions extends Pick<InertiaOptions, 'friction' | 'minVelocityPxMs' | 'emaAlpha'> {
5
6
  /**
6
7
  * Initial numeric value from which inertial motion starts.
7
8
  *
@@ -22,24 +23,16 @@ export interface UseHoneyDecayOptions {
22
23
  */
23
24
  max: number;
24
25
  /**
25
- * Exponential friction coefficient applied per millisecond.
26
+ * Optional callback invoked exactly once when inertial motion terminates.
26
27
  *
27
- * Controls how quickly velocity decays over time.
28
+ * Triggered when inertia ends due to:
29
+ * - velocity decaying below `minVelocityPxMs`
30
+ * - movement being blocked by bounds
31
+ * - an explicit call to `stop()`
28
32
  *
29
- * Smaller values produce longer, floatier inertia;
30
- * larger values result in a quicker stop.
31
- *
32
- * @default 0.002
33
- */
34
- friction?: number;
35
- /**
36
- * Minimum absolute velocity below which inertia is considered complete.
37
- *
38
- * This prevents unnecessary micro-updates and jitter near rest.
39
- *
40
- * @default 0.01
33
+ * Not invoked if inertia was never started.
41
34
  */
42
- minVelocityPxMs?: number;
35
+ onStop?: () => void;
43
36
  }
44
37
  /**
45
38
  * Public control API returned by {@link useHoneyDecay}.
@@ -61,9 +54,30 @@ export interface UseHoneyDecayApi {
61
54
  /**
62
55
  * Updates the hard bounds used by the decay simulation.
63
56
  *
64
- * The current value is clamped implicitly on the next frame if it lies outside the new bounds.
57
+ * This method is safe to call **at any time**, including while inertia is actively running.
58
+ *
59
+ * ### Behavior
60
+ * - If the current value lies **within** the new bounds:
61
+ * - bounds are updated
62
+ * - inertia (if running) continues uninterrupted
63
+ *
64
+ * - If the current value lies **outside** the new bounds:
65
+ * - the value is immediately clamped to the nearest boundary
66
+ * - internal velocity is reset to `0`
67
+ * - any active inertia is **terminated immediately**
68
+ * - `onStop` is invoked exactly once (if inertia was active)
69
+ *
70
+ * This deterministic behavior mirrors native scroll engines and ensures:
71
+ * - no overshoot
72
+ * - no extra inertia frames
73
+ * - consistent `onStop` semantics
65
74
  *
66
- * This is intended for dynamic layouts where overflow can change (e.g. resize, content updates).
75
+ * ### Intended usage
76
+ * - Responding to layout or content changes
77
+ * - Handling resize / orientation changes
78
+ * - Updating overscroll or overflow limits dynamically
79
+ *
80
+ * ⚠️ This method should **not** be called from inside the RAF frame handler.
67
81
  *
68
82
  * @param min - New lower bound (inclusive)
69
83
  * @param max - New upper bound (inclusive)
@@ -81,18 +95,30 @@ export interface UseHoneyDecayApi {
81
95
  */
82
96
  start: (velocityPxMs: number) => void;
83
97
  /**
84
- * Immediately stops inertial motion.
98
+ * Immediately sets the value and starts inertial motion from that value in a single atomic operation.
99
+ *
100
+ * This is the preferred way to hand off from a gesture (e.g. drag end) to inertia, as it:
101
+ * - avoids transient intermediate states
102
+ * - guarantees correct value/velocity ordering
103
+ * - ensures `onStop` semantics remain consistent
104
+ *
105
+ * @param value - Starting value for the inertia simulation
106
+ * @param velocityPxMs - Initial velocity in pixels per millisecond (`px/ms`)
85
107
  */
86
- stop: () => void;
108
+ startFrom: (value: number, velocityPxMs: number) => void;
87
109
  /**
88
- * Immediately sets the value and cancels any active inertia.
110
+ * Immediately stops inertial motion.
89
111
  *
90
- * @param value - The value to apply immediately.
112
+ * If inertia is currently active, this will:
113
+ * - cancel the RAF loop
114
+ * - reset internal velocity
115
+ * - invoke `onStop` exactly once
91
116
  */
92
- snapTo: (value: number) => void;
117
+ stop: () => void;
93
118
  }
94
119
  /**
95
- * A bounded, velocity-based inertia (decay) hook built on top of {@link useHoneyRafLoop} and {@link applyInertiaStep}.
120
+ * A bounded, velocity-based inertia (decay) hook built on top
121
+ * of {@link useHoneyRafLoop} and {@link applyInertiaStep}.
96
122
  *
97
123
  * This hook models **momentum-driven motion** where:
98
124
  * - Motion starts with an initial velocity
@@ -150,4 +176,4 @@ export interface UseHoneyDecayApi {
150
176
  * );
151
177
  * ```
152
178
  */
153
- export declare const useHoneyDecay: ({ initialValue, min, max, friction, minVelocityPxMs, }: UseHoneyDecayOptions) => UseHoneyDecayApi;
179
+ export declare const useHoneyDecay: ({ initialValue, min, max, friction, minVelocityPxMs, emaAlpha, onStop, }: UseHoneyDecayOptions) => UseHoneyDecayApi;
@@ -0,0 +1,18 @@
1
+ import type { RefObject } from 'react';
2
+ type UseHoneyLatest = {
3
+ <T>(value: T): RefObject<T>;
4
+ <T>(value: T | undefined): RefObject<T | undefined>;
5
+ };
6
+ /**
7
+ * Stores the latest value in a stable ref.
8
+ *
9
+ * Guarantees that:
10
+ * - `ref.current` always points to the latest value
11
+ * - the ref object identity never changes
12
+ *
13
+ * Overload behavior:
14
+ * - If a non-optional value is provided, `.current` is non-optional
15
+ * - If an optional value is provided, `.current` is optional
16
+ */
17
+ export declare const useHoneyLatest: UseHoneyLatest;
18
+ export {};