react-native-livechart 4.26.0 → 4.28.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.
Files changed (142) hide show
  1. package/dist/components/CustomReferenceLineOverlay.d.ts +8 -1
  2. package/dist/components/CustomReferenceLineOverlay.d.ts.map +1 -1
  3. package/dist/components/CustomTooltipOverlay.d.ts +3 -2
  4. package/dist/components/CustomTooltipOverlay.d.ts.map +1 -1
  5. package/dist/components/LiveChart.d.ts +1 -1
  6. package/dist/components/LiveChart.d.ts.map +1 -1
  7. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  8. package/dist/components/MultiSeriesDots.d.ts +1 -1
  9. package/dist/components/MultiSeriesDots.d.ts.map +1 -1
  10. package/dist/components/MultiSeriesValueLabels.d.ts +1 -1
  11. package/dist/components/MultiSeriesValueLabels.d.ts.map +1 -1
  12. package/dist/components/MultiSeriesValueLines.d.ts +1 -1
  13. package/dist/components/MultiSeriesValueLines.d.ts.map +1 -1
  14. package/dist/components/ReferenceLineOverlay.d.ts +4 -0
  15. package/dist/components/ReferenceLineOverlay.d.ts.map +1 -1
  16. package/dist/components/ReferenceValueBadge.d.ts +15 -0
  17. package/dist/components/ReferenceValueBadge.d.ts.map +1 -0
  18. package/dist/components/XAxisOverlay.d.ts +17 -0
  19. package/dist/components/XAxisOverlay.d.ts.map +1 -1
  20. package/dist/core/liveChartEngineTick.d.ts +4 -0
  21. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  22. package/dist/core/liveChartSeriesEngineTick.d.ts +15 -2
  23. package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
  24. package/dist/core/resolveConfig.d.ts +28 -1
  25. package/dist/core/resolveConfig.d.ts.map +1 -1
  26. package/dist/core/useLiveChartEngine.d.ts +9 -0
  27. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  28. package/dist/core/useLiveChartSeriesEngine.d.ts +18 -4
  29. package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
  30. package/dist/draw/grid.d.ts +6 -4
  31. package/dist/draw/grid.d.ts.map +1 -1
  32. package/dist/draw/line.d.ts +21 -3
  33. package/dist/draw/line.d.ts.map +1 -1
  34. package/dist/hooks/crosshairSeries.d.ts +2 -2
  35. package/dist/hooks/crosshairSeries.d.ts.map +1 -1
  36. package/dist/hooks/crosshairShared.d.ts +16 -1
  37. package/dist/hooks/crosshairShared.d.ts.map +1 -1
  38. package/dist/hooks/referenceBadgeFont.d.ts +5 -0
  39. package/dist/hooks/referenceBadgeFont.d.ts.map +1 -0
  40. package/dist/hooks/resolveChartLayout.d.ts +18 -2
  41. package/dist/hooks/resolveChartLayout.d.ts.map +1 -1
  42. package/dist/hooks/useBadge.d.ts +16 -1
  43. package/dist/hooks/useBadge.d.ts.map +1 -1
  44. package/dist/hooks/useChartSkiaFont.d.ts +1 -0
  45. package/dist/hooks/useChartSkiaFont.d.ts.map +1 -1
  46. package/dist/hooks/useCrosshair.d.ts +7 -2
  47. package/dist/hooks/useCrosshair.d.ts.map +1 -1
  48. package/dist/hooks/useCrosshairSeries.d.ts +6 -1
  49. package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
  50. package/dist/hooks/useLatestCallback.d.ts +6 -0
  51. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  52. package/dist/hooks/useMarkers.d.ts.map +1 -1
  53. package/dist/hooks/useMultiSeriesDegen.d.ts +1 -1
  54. package/dist/hooks/useMultiSeriesDegen.d.ts.map +1 -1
  55. package/dist/hooks/useMultiSeriesLinePaths.d.ts.map +1 -1
  56. package/dist/hooks/usePanScroll.d.ts +3 -1
  57. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  58. package/dist/hooks/usePinchZoom.d.ts +7 -4
  59. package/dist/hooks/usePinchZoom.d.ts.map +1 -1
  60. package/dist/hooks/useReferenceDrag.d.ts +20 -5
  61. package/dist/hooks/useReferenceDrag.d.ts.map +1 -1
  62. package/dist/hooks/useReferenceLine.d.ts +24 -12
  63. package/dist/hooks/useReferenceLine.d.ts.map +1 -1
  64. package/dist/hooks/useReferenceLinePress.d.ts +4 -2
  65. package/dist/hooks/useReferenceLinePress.d.ts.map +1 -1
  66. package/dist/hooks/useReferenceTagStack.d.ts +31 -0
  67. package/dist/hooks/useReferenceTagStack.d.ts.map +1 -0
  68. package/dist/hooks/useScrubMarkers.d.ts +12 -0
  69. package/dist/hooks/useScrubMarkers.d.ts.map +1 -0
  70. package/dist/hooks/useXAxis.d.ts +1 -1
  71. package/dist/hooks/useXAxis.d.ts.map +1 -1
  72. package/dist/hooks/useYAxis.d.ts +1 -1
  73. package/dist/hooks/useYAxis.d.ts.map +1 -1
  74. package/dist/hooks/useYAxisLabelWidth.d.ts +6 -0
  75. package/dist/hooks/useYAxisLabelWidth.d.ts.map +1 -0
  76. package/dist/hooks/useYAxisScaleGesture.d.ts +34 -0
  77. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -0
  78. package/dist/index.d.ts +1 -1
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/math/axisBadgeLayout.d.ts +16 -0
  81. package/dist/math/axisBadgeLayout.d.ts.map +1 -0
  82. package/dist/math/markers.d.ts +3 -0
  83. package/dist/math/markers.d.ts.map +1 -1
  84. package/dist/math/range.d.ts +1 -1
  85. package/dist/math/range.d.ts.map +1 -1
  86. package/dist/math/referenceLines.d.ts.map +1 -1
  87. package/dist/math/referenceTagStack.d.ts +36 -0
  88. package/dist/math/referenceTagStack.d.ts.map +1 -0
  89. package/dist/math/scrubMarkers.d.ts +12 -0
  90. package/dist/math/scrubMarkers.d.ts.map +1 -0
  91. package/dist/types.d.ts +262 -37
  92. package/dist/types.d.ts.map +1 -1
  93. package/package.json +1 -1
  94. package/src/components/CustomReferenceLineOverlay.tsx +50 -8
  95. package/src/components/CustomTooltipOverlay.tsx +5 -1
  96. package/src/components/LiveChart.tsx +580 -138
  97. package/src/components/LiveChartSeries.tsx +139 -19
  98. package/src/components/MultiSeriesDots.tsx +4 -2
  99. package/src/components/MultiSeriesValueLabels.tsx +4 -2
  100. package/src/components/MultiSeriesValueLines.tsx +4 -2
  101. package/src/components/ReferenceLineOverlay.tsx +62 -26
  102. package/src/components/ReferenceValueBadge.tsx +102 -0
  103. package/src/components/XAxisOverlay.tsx +104 -1
  104. package/src/core/liveChartEngineTick.ts +49 -4
  105. package/src/core/liveChartSeriesEngineTick.ts +107 -14
  106. package/src/core/resolveConfig.ts +103 -7
  107. package/src/core/useLiveChartEngine.ts +45 -9
  108. package/src/core/useLiveChartSeriesEngine.ts +47 -6
  109. package/src/draw/grid.ts +17 -13
  110. package/src/draw/line.ts +46 -5
  111. package/src/hooks/crosshairSeries.ts +37 -5
  112. package/src/hooks/crosshairShared.ts +56 -1
  113. package/src/hooks/referenceBadgeFont.ts +35 -0
  114. package/src/hooks/resolveChartLayout.ts +51 -3
  115. package/src/hooks/useBadge.ts +44 -5
  116. package/src/hooks/useChartSkiaFont.ts +1 -1
  117. package/src/hooks/useCrosshair.ts +444 -310
  118. package/src/hooks/useCrosshairSeries.ts +57 -20
  119. package/src/hooks/useLatestCallback.ts +15 -0
  120. package/src/hooks/useMarkers.ts +51 -39
  121. package/src/hooks/useMultiSeriesDegen.ts +10 -5
  122. package/src/hooks/useMultiSeriesLinePaths.ts +2 -0
  123. package/src/hooks/usePanScroll.ts +155 -127
  124. package/src/hooks/usePinchZoom.ts +94 -66
  125. package/src/hooks/useReferenceDrag.ts +409 -156
  126. package/src/hooks/useReferenceLine.ts +453 -277
  127. package/src/hooks/useReferenceLineGrouping.ts +1 -1
  128. package/src/hooks/useReferenceLinePress.ts +121 -53
  129. package/src/hooks/useReferenceTagStack.ts +81 -0
  130. package/src/hooks/useScrubMarkers.ts +79 -0
  131. package/src/hooks/useXAxis.ts +11 -2
  132. package/src/hooks/useYAxis.ts +1 -1
  133. package/src/hooks/useYAxisLabelWidth.ts +26 -0
  134. package/src/hooks/useYAxisScaleGesture.ts +263 -0
  135. package/src/index.ts +11 -0
  136. package/src/math/axisBadgeLayout.ts +37 -0
  137. package/src/math/markers.ts +28 -3
  138. package/src/math/range.ts +35 -0
  139. package/src/math/referenceLines.ts +5 -0
  140. package/src/math/referenceTagStack.ts +112 -0
  141. package/src/math/scrubMarkers.ts +55 -0
  142. package/src/types.ts +263 -37
@@ -1,5 +1,8 @@
1
+ import { stackedTagAt, type ReferenceTagStack } from "../math/referenceTagStack";
2
+ import { useCallback, useMemo } from "react";
1
3
  import { Gesture } from "react-native-gesture-handler";
2
4
  import {
5
+ isSharedValue,
3
6
  useAnimatedReaction,
4
7
  useDerivedValue,
5
8
  useSharedValue,
@@ -18,7 +21,14 @@ import {
18
21
  } from "../math/referenceDrag";
19
22
  import { referenceLineForm } from "../math/referenceLines";
20
23
  import type { ReferenceLine } from "../types";
21
- import { computeValueAtY, pinnedPlotY, snapPrice } from "./crosshairShared";
24
+ import {
25
+ computeValueAtY,
26
+ pinnedPlotY,
27
+ snapPriceOutward,
28
+ snapPriceWithin,
29
+ } from "./crosshairShared";
30
+
31
+ import { useLatestCallback } from "./useLatestCallback";
22
32
 
23
33
  /** Vertical reach (px) around a line within which a touch grabs it. */
24
34
  const GRAB_SLOP = 14;
@@ -29,13 +39,54 @@ const DRAG_ACTIVATE_PX = 4;
29
39
  * (and the onDragIn/Out reaction never fires) when the feature is unused. */
30
40
  const EMPTY: never[] = [];
31
41
 
42
+ /**
43
+ * The value a drag at canvas `y` gives `line` against the current range: the
44
+ * price at that Y, snapped to `line.snap` and clamped to `bounds` (the line's
45
+ * `bounds`, read from its SharedValue when it has one). Null
46
+ * before the canvas is laid out. `atFinger`: the value the finger sets (what the
47
+ * range fits and `onChange` reports) — the nearest increment, rounded outward
48
+ * once the finger is past the plot's edge so even a coarse `snap` can widen the
49
+ * range. Otherwise the value the line is drawn and committed at — rounded inward
50
+ * at the edges, so the line stays on the plot.
51
+ */
52
+ function dragValueAtY(
53
+ line: ReferenceLine,
54
+ bounds: [number, number] | undefined,
55
+ y: number,
56
+ displayMin: number,
57
+ displayMax: number,
58
+ canvasHeight: number,
59
+ padTop: number,
60
+ padBottom: number,
61
+ atFinger: boolean,
62
+ ): number | null {
63
+ "worklet";
64
+ const raw = computeValueAtY(
65
+ y,
66
+ displayMin,
67
+ displayMax,
68
+ canvasHeight,
69
+ padTop,
70
+ padBottom,
71
+ );
72
+ if (raw === null) return null;
73
+ const snapped = atFinger
74
+ ? snapPriceOutward(
75
+ raw,
76
+ line.snap,
77
+ y < padTop ? 1 : y > canvasHeight - padBottom ? -1 : 0,
78
+ )
79
+ : snapPriceWithin(raw, line.snap, displayMin, displayMax);
80
+ return clampToBounds(snapped, bounds);
81
+ }
82
+
32
83
  /**
33
84
  * Builds the per-line **drag** gesture for draggable Form-A reference lines: grab a
34
85
  * line near its value-Y and drag vertically to set a new value, with optional
35
86
  * `snap` + `bounds` clamp. Mirrors the order-ticket reticle in {@link useCrosshair}
36
87
  * (value↔Y via `computeValueAtY` / `pinnedPlotY`, frozen value re-projected
37
- * each frame) but per line, writing into the shared `dragValues` array the layout
38
- * and overlays read.
88
+ * each frame) but per line, writing the value the finger sets into the shared
89
+ * `dragValues` array (what the range fit reads). The overlays read `drawnValues`.
39
90
  *
40
91
  * The pan uses `manualActivation`: it grabs only when a touch starts within
41
92
  * {@link GRAB_SLOP} of a draggable line (and inside its `grabRange`, when it has
@@ -46,8 +97,18 @@ const EMPTY: never[] = [];
46
97
  * falls through to scrub on a horizontal start, so a drag begun on a line always
47
98
  * wins the race rather than dropping a scrub crosshair (#163).
48
99
  *
49
- * Also fires the per-line drag callbacks: `onChange` (de-duped during drag),
50
- * `onCommit` (on release), and `onDragIn` / `onDragOut` (value crossing the visible
100
+ * The dragged line stays under the finger: `drawnValues` re-maps it from the
101
+ * finger's last Y whenever the range changes, or the plot is resized, while the
102
+ * finger rests. `dragValues` keeps the value the finger last set. If another line
103
+ * takes the dragged line's index mid-drag (lines added or removed before it, told
104
+ * apart by `id`), the drag lets go: it moves and commits nothing, and
105
+ * gives that line back its own value immediately. This touch stays cancelled if
106
+ * the original line later returns to the grabbed index.
107
+ *
108
+ * Also fires the per-line drag callbacks: `onChange` (as the finger moves the
109
+ * line, de-duped to value changes, and once more on release if the line was
110
+ * drawn elsewhere), `onCommit` (on release, with the value under the finger),
111
+ * and `onDragIn` / `onDragOut` (drawn value crossing the visible
51
112
  * range or a `bounds`, from a drag or the axis rescaling — edge-detected each frame).
52
113
  */
53
114
  export function useReferenceDrag(
@@ -57,11 +118,16 @@ export function useReferenceDrag(
57
118
  dragValues: SharedValue<number[]>,
58
119
  dragActive: SharedValue<boolean[]>,
59
120
  enabled: boolean,
121
+ tagStack?: SharedValue<ReferenceTagStack>,
60
122
  ): {
61
123
  gesture: ReturnType<typeof Gesture.Pan>;
62
124
  /** True when a touch at (x,y) would grab a draggable line — lets the scrub
63
125
  * gesture decline that press so it never drops a crosshair on a line (#163). */
64
126
  hitTest: (x: number, y: number) => boolean;
127
+ /** `dragValues` with the line being dragged kept under the finger, re-mapped
128
+ * against the current range. What the lines' overlays, grouping and press
129
+ * hit-test read; the range fit reads `dragValues`. */
130
+ drawnValues: SharedValue<number[]>;
65
131
  } {
66
132
  const anyDraggable =
67
133
  enabled &&
@@ -72,6 +138,10 @@ export function useReferenceDrag(
72
138
  (l.onDragIn != null || l.onDragOut != null),
73
139
  );
74
140
 
141
+ // The gesture retains this container while committed line props change.
142
+ // Reanimated updates it on the UI thread without rebuilding the recognizer.
143
+ const linesSV = useDerivedValue(() => lines);
144
+
75
145
  // Per-line handle Y (canvas px), index-aligned with `lines`; -1 when the line
76
146
  // isn't draggable or the canvas isn't laid out. Off-screen lines pin to the
77
147
  // nearest plot edge so they stay grabbable. Recomputed each frame (UI thread).
@@ -85,8 +155,8 @@ export function useReferenceDrag(
85
155
  const bottom = ch - padding.bottom;
86
156
  if (bottom <= top) return EMPTY;
87
157
  const out: number[] = [];
88
- for (let i = 0; i < lines.length; i++) {
89
- const l = lines[i];
158
+ for (let i = 0; i < linesSV.get().length; i++) {
159
+ const l = linesSV.get()[i];
90
160
  if (
91
161
  !l.draggable ||
92
162
  referenceLineForm(l) !== "line" ||
@@ -103,32 +173,136 @@ export function useReferenceDrag(
103
173
 
104
174
  // Per-line grab x-ranges (`ReferenceLine.grabRange`), index-aligned with
105
175
  // `lines`; null = anywhere along the line. Read by the gesture worklets below,
106
- // which close over this render's `lines` as the callbacks do.
107
- const grabRanges = lines.map((l) => l.grabRange ?? null);
176
+ // through shared values so line updates do not rebuild the recognizer.
177
+ const grabRanges = useDerivedValue(() => {
178
+ const ranges: (ReferenceLine["grabRange"] | null)[] = [];
179
+ for (const line of linesSV.get()) ranges.push(line.grabRange ?? null);
180
+ return ranges;
181
+ });
182
+
183
+ // Per-line drag bounds (`ReferenceLine.bounds`), index-aligned with `lines`.
184
+ // The worklet captures the bounds themselves rather than `linesSV`, so a
185
+ // SharedValue bound is one of its inputs: moving it re-clamps the drag and
186
+ // re-checks onDragIn / onDragOut on the UI thread, without a render.
187
+ const boundsInputs = useMemo(() => lines.map((l) => l.bounds), [lines]);
188
+ const bounds = useDerivedValue(() => {
189
+ const out: ([number, number] | undefined)[] = [];
190
+ for (const b of boundsInputs) out.push(isSharedValue<[number, number]>(b) ? b.get() : b);
191
+ return out;
192
+ });
108
193
 
109
194
  const dragIndex = useSharedValue(-1);
110
195
  const startX = useSharedValue(0);
111
196
  const startY = useSharedValue(0);
197
+ const pointerOffsetY = useSharedValue(0);
112
198
  const activated = useSharedValue(false);
113
199
  const lastChange = useSharedValue(0);
200
+ // The finger's latest Y during a drag, and the grabbed line's `id`: lines are
201
+ // index-aligned, so a line added or removed before it mid-drag hands its index
202
+ // to another line, and the drag must not move that one.
203
+ const lastY = useSharedValue(0);
204
+ const grabbedId = useSharedValue<string | undefined>(undefined);
205
+ const { displayMin, displayMax, canvasHeight } = engine;
206
+
207
+ // The dragged line under the finger. The range (live data, a range animation)
208
+ // can move, or the plot be resized, while the finger rests, and the gesture maps
209
+ // the finger only when it moves, so re-map from its last Y here. Being a
210
+ // derived value, everything that reads it is ordered after it and draws this
211
+ // frame's value. `dragValues` keeps the value the finger last set: the range
212
+ // fits that one (fitting this one would let a line held near the plot edge push
213
+ // the range out every frame) and `onChange` reports it.
214
+ const drawnValues = useDerivedValue<number[]>(() => {
215
+ const values = dragValues.get();
216
+ const i = dragIndex.get();
217
+ if (i < 0 || !activated.get()) return values;
218
+ const l = linesSV.get()[i];
219
+ if (l === undefined || l.id !== grabbedId.get()) return values;
220
+ const v = dragValueAtY(
221
+ l,
222
+ bounds.get()[i],
223
+ lastY.get(),
224
+ displayMin.get(),
225
+ displayMax.get(),
226
+ canvasHeight.get(),
227
+ padding.top,
228
+ padding.bottom,
229
+ false,
230
+ );
231
+ if (v === null || v === values[i]) return values;
232
+ const out = values.slice();
233
+ out[i] = v;
234
+ return out;
235
+ });
236
+
237
+ // Once the grabbed identity is lost, this touch cannot resume a drag if that
238
+ // line later returns. Restore a replacement's own value and clear ownership.
239
+ const clearDrag = useCallback(() => {
240
+ "worklet";
241
+ const i = dragIndex.get();
242
+ const l = linesSV.get()[i];
243
+ if (i >= 0 && activated.get()) {
244
+ if (l !== undefined && l.id !== grabbedId.get()) {
245
+ const own = l.value ?? 0;
246
+ const values = dragValues.get();
247
+ if (i < values.length && values[i] !== own) {
248
+ const restored = values.slice();
249
+ restored[i] = own;
250
+ dragValues.set(restored);
251
+ }
252
+ }
253
+ const active = dragActive.get();
254
+ if (i < active.length && active[i]) {
255
+ const next = active.slice();
256
+ next[i] = false;
257
+ dragActive.set(next);
258
+ }
259
+ }
260
+ dragIndex.set(-1);
261
+ activated.set(false);
262
+ }, [
263
+ activated,
264
+ dragActive,
265
+ dragIndex,
266
+ dragValues,
267
+ grabbedId,
268
+ linesSV,
269
+ ]);
270
+
271
+ // Reordering/removal must also cancel while the finger is resting. Derived
272
+ // drawing alone cannot reset the gesture state or the range-fit override.
273
+ useAnimatedReaction(
274
+ () => {
275
+ const i = dragIndex.get();
276
+ const l = linesSV.get()[i];
277
+ return i >= 0 && (l === undefined || l.id !== grabbedId.get());
278
+ },
279
+ (invalid) => {
280
+ if (invalid) clearDrag();
281
+ },
282
+ );
114
283
 
115
- // JS-thread callback dispatch — closes over the latest `lines` each render.
284
+ // JS dispatchers always use the latest committed callback, and reject an
285
+ // event queued for a line that has since been replaced.
116
286
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread gesture */
117
- function emitChange(i: number, v: number) {
118
- lines[i]?.onChange?.(v);
119
- }
287
+ const emitChange = useLatestCallback((i: number, id: string | undefined, v: number) => {
288
+ const line = lines[i];
289
+ if (line && line.id === id) line.onChange?.(v);
290
+ });
120
291
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread gesture */
121
- function emitCommit(i: number, v: number) {
122
- lines[i]?.onCommit?.(v);
123
- }
292
+ const emitCommit = useLatestCallback((i: number, id: string | undefined, v: number) => {
293
+ const line = lines[i];
294
+ if (line && line.id === id) line.onCommit?.(v);
295
+ });
124
296
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread reaction */
125
- function emitDragOut(i: number, v: number) {
126
- lines[i]?.onDragOut?.(v);
127
- }
297
+ const emitDragOut = useLatestCallback((i: number, id: string | undefined, v: number) => {
298
+ const line = lines[i];
299
+ if (line && line.id === id) line.onDragOut?.(v);
300
+ });
128
301
  /* istanbul ignore next -- runs via scheduleOnRN from the UI-thread reaction */
129
- function emitDragIn(i: number, v: number) {
130
- lines[i]?.onDragIn?.(v);
131
- }
302
+ const emitDragIn = useLatestCallback((i: number, id: string | undefined, v: number) => {
303
+ const line = lines[i];
304
+ if (line && line.id === id) line.onDragIn?.(v);
305
+ });
132
306
 
133
307
  // onDragIn / onDragOut — edge-detect each line's "out of the watched interval"
134
308
  // state (from a drag or the axis rescaling under a fixed value).
@@ -139,8 +313,8 @@ export function useReferenceDrag(
139
313
  const dMin = engine.displayMin.get();
140
314
  const dMax = engine.displayMax.get();
141
315
  const out: boolean[] = [];
142
- for (let i = 0; i < lines.length; i++) {
143
- const l = lines[i];
316
+ for (let i = 0; i < linesSV.get().length; i++) {
317
+ const l = linesSV.get()[i];
144
318
  if (
145
319
  referenceLineForm(l) !== "line" ||
146
320
  l.value === undefined ||
@@ -149,8 +323,8 @@ export function useReferenceDrag(
149
323
  out.push(false);
150
324
  continue;
151
325
  }
152
- const v = dragValues.get()[i] ?? l.value;
153
- out.push(referenceValueOut(v, dMin, dMax, l.bounds));
326
+ const v = drawnValues.get()[i] ?? l.value;
327
+ out.push(referenceValueOut(v, dMin, dMax, bounds.get()[i]));
154
328
  }
155
329
  return out;
156
330
  },
@@ -159,145 +333,224 @@ export function useReferenceDrag(
159
333
  if (!prev || curr === prev) return;
160
334
  for (let i = 0; i < curr.length; i++) {
161
335
  if (prev[i] === undefined || curr[i] === prev[i]) continue;
162
- const l = lines[i];
163
- const v = dragValues.get()[i] ?? l.value ?? 0;
164
- if (curr[i]) scheduleOnRN(emitDragOut, i, v);
165
- else scheduleOnRN(emitDragIn, i, v);
336
+ const l = linesSV.get()[i];
337
+ const v = drawnValues.get()[i] ?? l.value ?? 0;
338
+ if (curr[i]) scheduleOnRN(emitDragOut, i, l.id, v);
339
+ else scheduleOnRN(emitDragIn, i, l.id, v);
166
340
  }
167
341
  },
168
- [lines, anyDragInOut],
169
342
  );
170
343
 
171
- // ── Gesture callbacks (UI-thread worklets — excluded from Jest coverage) ──────
172
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
173
- const onTouchesDown = (
174
- e: { changedTouches: { x: number; y: number }[] },
175
- manager: { fail: () => void },
176
- ) => {
177
- "worklet";
178
- const t = e.changedTouches[0];
179
- if (!t) return;
180
- const i = nearestDraggableIndex(handleYs.get(), t.y, GRAB_SLOP, {
181
- x: t.x,
182
- ranges: grabRanges,
183
- });
184
- if (i < 0) {
185
- manager.fail();
186
- return;
187
- }
188
- dragIndex.set(i);
189
- startX.set(t.x);
190
- startY.set(t.y);
191
- };
192
-
193
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
194
- const onTouchesMove = (
195
- e: { allTouches: { x: number; y: number }[] },
196
- manager: { activate: () => void },
197
- ) => {
198
- "worklet";
199
- if (dragIndex.get() < 0) return;
200
- const t = e.allTouches[0];
201
- if (!t) return;
202
- // A line is grabbed → it owns the touch: any drag past the threshold drags it.
203
- // We don't fail on horizontal intent (which used to hand the touch to scrub) —
204
- // that let the scrub crosshair win the race even on a vertical drag started on
205
- // the line (#163). Scrub / scroll still run off a line's grab band.
206
- const intent = resolveDragIntent(
207
- t.x - startX.get(),
208
- t.y - startY.get(),
209
- DRAG_ACTIVATE_PX,
210
- );
211
- if (intent === "activate") manager.activate();
212
- };
344
+ const { gesture, hitTest } = useMemo(() => {
345
+ const canDragTag = (index: number, x: number) => {
346
+ "worklet";
347
+ if (!linesSV.get()[index]?.draggable) return false;
348
+ const range = grabRanges.get()[index];
349
+ return !range || (x >= range[0] && x <= range[1]);
350
+ };
351
+ // ── Gesture callbacks (UI-thread worklets — excluded from Jest coverage) ──────
352
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
353
+ const onTouchesDown = (
354
+ e: { changedTouches: { x: number; y: number }[] },
355
+ manager: { fail: () => void },
356
+ ) => {
357
+ "worklet";
358
+ const t = e.changedTouches[0];
359
+ if (!t) return;
360
+ const tag = stackedTagAt(tagStack?.get().tags ?? [], t.x, t.y);
361
+ const i = tag ? (linesSV.get()[tag.index]?.id === tag.lineId && canDragTag(tag.index, t.x) ? tag.index : -1) : nearestDraggableIndex(handleYs.get(), t.y, GRAB_SLOP, {
362
+ x: t.x,
363
+ ranges: grabRanges.get(),
364
+ });
365
+ const l = linesSV.get()[i];
366
+ if (l === undefined) {
367
+ manager.fail();
368
+ return;
369
+ }
370
+ pointerOffsetY.set(tag ? t.y - tag.lineY : 0);
371
+ dragIndex.set(i);
372
+ grabbedId.set(l.id);
373
+ startX.set(t.x);
374
+ startY.set(t.y);
375
+ };
213
376
 
214
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
215
- const onStart = () => {
216
- "worklet";
217
- const i = dragIndex.get();
218
- if (i < 0) return;
219
- activated.set(true);
220
- const arr = dragActive.get().slice();
221
- arr[i] = true;
222
- dragActive.set(arr);
223
- lastChange.set(dragValues.get()[i] ?? lines[i].value ?? 0);
224
- };
377
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
378
+ const onTouchesMove = (
379
+ e: { allTouches: { x: number; y: number }[] },
380
+ manager: { activate: () => void },
381
+ ) => {
382
+ "worklet";
383
+ if (dragIndex.get() < 0) return;
384
+ const t = e.allTouches[0];
385
+ if (!t) return;
386
+ // A line is grabbed → it owns the touch: any drag past the threshold drags it.
387
+ // We don't fail on horizontal intent (which used to hand the touch to scrub) —
388
+ // that let the scrub crosshair win the race even on a vertical drag started on
389
+ // the line (#163). Scrub / scroll still run off a line's grab band.
390
+ const intent = resolveDragIntent(
391
+ t.x - startX.get(),
392
+ t.y - startY.get(),
393
+ DRAG_ACTIVATE_PX,
394
+ );
395
+ if (intent === "activate") manager.activate();
396
+ };
225
397
 
226
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
227
- const onUpdate = (e: { y: number }) => {
228
- "worklet";
229
- const i = dragIndex.get();
230
- if (i < 0) return;
231
- const l = lines[i];
232
- const raw = computeValueAtY(
233
- e.y,
234
- engine.displayMin.get(),
235
- engine.displayMax.get(),
236
- engine.canvasHeight.get(),
237
- padding.top,
238
- padding.bottom,
239
- );
240
- if (raw === null) return;
241
- const v = clampToBounds(snapPrice(raw, l.snap), l.bounds);
242
- const arr = dragValues.get().slice();
243
- arr[i] = v;
244
- dragValues.set(arr);
245
- if (v !== lastChange.get()) {
246
- lastChange.set(v);
247
- if (l.onChange) scheduleOnRN(emitChange, i, v);
248
- }
249
- };
250
-
251
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
252
- const onFinalize = () => {
253
- "worklet";
254
- const i = dragIndex.get();
255
- if (i >= 0 && activated.get()) {
256
- const v = dragValues.get()[i] ?? lines[i].value ?? 0;
398
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
399
+ const onStart = (e: { y: number }) => {
400
+ "worklet";
401
+ const i = dragIndex.get();
402
+ const l = linesSV.get()[i];
403
+ // The line may be gone, or another have taken its index, since the grab.
404
+ if (l === undefined || l.id !== grabbedId.get()) {
405
+ clearDrag();
406
+ return;
407
+ }
408
+ lastY.set(e.y - pointerOffsetY.get());
409
+ activated.set(true);
257
410
  const arr = dragActive.get().slice();
258
- arr[i] = false;
411
+ arr[i] = true;
259
412
  dragActive.set(arr);
260
- if (lines[i].onCommit) scheduleOnRN(emitCommit, i, v);
261
- }
262
- dragIndex.set(-1);
263
- activated.set(false);
264
- };
413
+ lastChange.set(dragValues.get()[i] ?? l.value ?? 0);
414
+ };
265
415
 
266
- // Hit-test shared with the scrub gesture: does this drag own the touch? The
267
- // scrub's `onStart` consults it to bail, so it never drops a crosshair on a
268
- // line even though it activates independently of this manual-activation pan
269
- // (the `Exclusive` priority alone doesn't hold scrub back — not while this
270
- // gesture is merely pressed, and not while it is dragging either). Once a line
271
- // is grabbed the answer is yes regardless of position: the scrub asks with its
272
- // touch-DOWN point, which a longer drag has carried the line away from, so the
273
- // geometric test alone said "no line here" and a crosshair opened mid-drag.
274
- // Otherwise it is the y-reach around the handles, along the whole line — or,
275
- // for a line with a `grabRange`, only inside it.
276
- /* istanbul ignore next -- worklet, runs on the UI thread */
277
- const hitTest = (x: number, y: number): boolean => {
278
- "worklet";
279
- if (!anyDraggable) return false;
280
- return referenceDragOwnsTouch(
281
- dragIndex.get(),
282
- handleYs.get(),
283
- y,
284
- GRAB_SLOP,
285
- {
286
- x,
287
- ranges: grabRanges,
288
- },
289
- );
290
- };
416
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
417
+ const onUpdate = (e: { y: number }) => {
418
+ "worklet";
419
+ const i = dragIndex.get();
420
+ // Not after an `onStart` that let go (the line gone or replaced by then).
421
+ if (i < 0 || !activated.get()) return;
422
+ lastY.set(e.y - pointerOffsetY.get());
423
+ const l = linesSV.get()[i];
424
+ if (l === undefined || l.id !== grabbedId.get()) {
425
+ clearDrag();
426
+ return;
427
+ }
428
+ const v = dragValueAtY(
429
+ l,
430
+ bounds.get()[i],
431
+ e.y - pointerOffsetY.get(),
432
+ displayMin.get(),
433
+ displayMax.get(),
434
+ canvasHeight.get(),
435
+ padding.top,
436
+ padding.bottom,
437
+ true,
438
+ );
439
+ if (v === null) return;
440
+ const arr = dragValues.get().slice();
441
+ arr[i] = v;
442
+ dragValues.set(arr);
443
+ if (v !== lastChange.get()) {
444
+ lastChange.set(v);
445
+ if (l.onChange) scheduleOnRN(emitChange, i, l.id, v);
446
+ }
447
+ };
448
+
449
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
450
+ const onFinalize = () => {
451
+ "worklet";
452
+ const i = dragIndex.get();
453
+ const l = linesSV.get()[i];
454
+ if (i >= 0 && activated.get() && l !== undefined) {
455
+ if (l.id === grabbedId.get()) {
456
+ // Commit what is drawn: the range may have moved under a still finger
457
+ // since its last move. It stays there once released.
458
+ const v =
459
+ dragValueAtY(
460
+ l,
461
+ bounds.get()[i],
462
+ lastY.get(),
463
+ displayMin.get(),
464
+ displayMax.get(),
465
+ canvasHeight.get(),
466
+ padding.top,
467
+ padding.bottom,
468
+ false,
469
+ ) ??
470
+ dragValues.get()[i] ??
471
+ l.value ??
472
+ 0;
473
+ if (dragValues.get()[i] !== v) {
474
+ const values = dragValues.get().slice();
475
+ values[i] = v;
476
+ dragValues.set(values);
477
+ }
478
+ // An `onChange`-only consumer ends where the line is drawn.
479
+ if (v !== lastChange.get() && l.onChange) {
480
+ scheduleOnRN(emitChange, i, l.id, v);
481
+ }
482
+ if (l.onCommit) scheduleOnRN(emitCommit, i, l.id, v);
483
+ }
484
+ }
485
+ clearDrag();
486
+ };
487
+
488
+ // Hit-test shared with the scrub gesture: does this drag own the touch? The
489
+ // scrub's `onStart` consults it to bail, so it never drops a crosshair on a
490
+ // line even though it activates independently of this manual-activation pan
491
+ // (the `Exclusive` priority alone doesn't hold scrub back — not while this
492
+ // gesture is merely pressed, and not while it is dragging either). Once a line
493
+ // is grabbed the answer is yes regardless of position: the scrub asks with its
494
+ // touch-DOWN point, which a longer drag has carried the line away from, so the
495
+ // geometric test alone said "no line here" and a crosshair opened mid-drag.
496
+ // Otherwise it is the y-reach around the handles, along the whole line — or,
497
+ // for a line with a `grabRange`, only inside it.
498
+ /* istanbul ignore next -- worklet, runs on the UI thread */
499
+ const hitTest = (x: number, y: number): boolean => {
500
+ "worklet";
501
+ if (!anyDraggable) return false;
502
+ if (dragIndex.get() >= 0) return true;
503
+ const tag = stackedTagAt(tagStack?.get().tags ?? [], x, y);
504
+ if (tag) return linesSV.get()[tag.index]?.id === tag.lineId && canDragTag(tag.index, x);
505
+ return referenceDragOwnsTouch(
506
+ dragIndex.get(),
507
+ handleYs.get(),
508
+ y,
509
+ GRAB_SLOP,
510
+ {
511
+ x,
512
+ ranges: grabRanges.get(),
513
+ },
514
+ );
515
+ };
516
+
517
+ const gesture = Gesture.Pan()
518
+ .enabled(anyDraggable)
519
+ .maxPointers(1)
520
+ .manualActivation(true)
521
+ .onTouchesDown(onTouchesDown)
522
+ .onTouchesMove(onTouchesMove)
523
+ .onStart(onStart)
524
+ .onUpdate(onUpdate)
525
+ .onFinalize(onFinalize);
291
526
 
292
- const gesture = Gesture.Pan()
293
- .enabled(anyDraggable)
294
- .maxPointers(1)
295
- .manualActivation(true)
296
- .onTouchesDown(onTouchesDown)
297
- .onTouchesMove(onTouchesMove)
298
- .onStart(onStart)
299
- .onUpdate(onUpdate)
300
- .onFinalize(onFinalize);
527
+ return { gesture, hitTest };
528
+ }, [
529
+ activated,
530
+ anyDraggable,
531
+ bounds,
532
+ canvasHeight,
533
+ clearDrag,
534
+ displayMax,
535
+ displayMin,
536
+ dragActive,
537
+ dragIndex,
538
+ dragValues,
539
+ emitChange,
540
+ emitCommit,
541
+ grabRanges,
542
+ grabbedId,
543
+ handleYs,
544
+ lastChange,
545
+ lastY,
546
+ linesSV,
547
+ padding.bottom,
548
+ padding.top,
549
+ startX,
550
+ startY,
551
+ pointerOffsetY,
552
+ tagStack,
553
+ ]);
301
554
 
302
- return { gesture, hitTest };
555
+ return { gesture, hitTest, drawnValues };
303
556
  }