@molecule/app-ui-react-native 2.0.1 → 2.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.
Files changed (193) hide show
  1. package/README.md +81 -10
  2. package/dist/components/Alert.d.ts.map +1 -1
  3. package/dist/components/Alert.js +2 -3
  4. package/dist/components/Button.d.ts +3 -1
  5. package/dist/components/Button.d.ts.map +1 -1
  6. package/dist/components/Button.js +35 -3
  7. package/dist/components/Skeleton.d.ts.map +1 -1
  8. package/dist/components/Skeleton.js +13 -12
  9. package/dist/components/Spinner.js +2 -2
  10. package/dist/elements/Anchor.d.ts +15 -0
  11. package/dist/elements/Anchor.d.ts.map +1 -0
  12. package/dist/elements/Anchor.js +57 -0
  13. package/dist/elements/Box.d.ts +19 -0
  14. package/dist/elements/Box.d.ts.map +1 -0
  15. package/dist/elements/Box.js +157 -0
  16. package/dist/elements/CheckField.d.ts +18 -0
  17. package/dist/elements/CheckField.d.ts.map +1 -0
  18. package/dist/elements/CheckField.js +103 -0
  19. package/dist/elements/Disclosure.d.ts +11 -0
  20. package/dist/elements/Disclosure.d.ts.map +1 -0
  21. package/dist/elements/Disclosure.js +67 -0
  22. package/dist/elements/Fieldset.d.ts +16 -0
  23. package/dist/elements/Fieldset.d.ts.map +1 -0
  24. package/dist/elements/Fieldset.js +34 -0
  25. package/dist/elements/FileField.d.ts +12 -0
  26. package/dist/elements/FileField.d.ts.map +1 -0
  27. package/dist/elements/FileField.js +111 -0
  28. package/dist/elements/FormArea.d.ts +18 -0
  29. package/dist/elements/FormArea.d.ts.map +1 -0
  30. package/dist/elements/FormArea.js +42 -0
  31. package/dist/elements/GlyphIcon.d.ts +19 -0
  32. package/dist/elements/GlyphIcon.d.ts.map +1 -0
  33. package/dist/elements/GlyphIcon.js +43 -0
  34. package/dist/elements/Image.d.ts +17 -0
  35. package/dist/elements/Image.d.ts.map +1 -0
  36. package/dist/elements/Image.js +26 -0
  37. package/dist/elements/Link.d.ts +20 -0
  38. package/dist/elements/Link.d.ts.map +1 -0
  39. package/dist/elements/Link.js +53 -0
  40. package/dist/elements/Pressable.d.ts +18 -0
  41. package/dist/elements/Pressable.d.ts.map +1 -0
  42. package/dist/elements/Pressable.js +108 -0
  43. package/dist/elements/PulseDots.d.ts +15 -0
  44. package/dist/elements/PulseDots.d.ts.map +1 -0
  45. package/dist/elements/PulseDots.js +78 -0
  46. package/dist/elements/RangeField.d.ts +11 -0
  47. package/dist/elements/RangeField.d.ts.map +1 -0
  48. package/dist/elements/RangeField.js +266 -0
  49. package/dist/elements/RichText.d.ts +23 -0
  50. package/dist/elements/RichText.d.ts.map +1 -0
  51. package/dist/elements/RichText.js +55 -0
  52. package/dist/elements/Rule.d.ts +11 -0
  53. package/dist/elements/Rule.d.ts.map +1 -0
  54. package/dist/elements/Rule.js +19 -0
  55. package/dist/elements/ScrollArea.d.ts +21 -0
  56. package/dist/elements/ScrollArea.d.ts.map +1 -0
  57. package/dist/elements/ScrollArea.js +101 -0
  58. package/dist/elements/SelectAction.d.ts +20 -0
  59. package/dist/elements/SelectAction.d.ts.map +1 -0
  60. package/dist/elements/SelectAction.js +117 -0
  61. package/dist/elements/SelectField.d.ts +11 -0
  62. package/dist/elements/SelectField.d.ts.map +1 -0
  63. package/dist/elements/SelectField.js +220 -0
  64. package/dist/elements/SelectMenu.d.ts +19 -0
  65. package/dist/elements/SelectMenu.d.ts.map +1 -0
  66. package/dist/elements/SelectMenu.js +122 -0
  67. package/dist/elements/Svg.d.ts +155 -0
  68. package/dist/elements/Svg.d.ts.map +1 -0
  69. package/dist/elements/Svg.js +535 -0
  70. package/dist/elements/TableBox.d.ts +19 -0
  71. package/dist/elements/TableBox.d.ts.map +1 -0
  72. package/dist/elements/TableBox.js +87 -0
  73. package/dist/elements/TableCell.d.ts +16 -0
  74. package/dist/elements/TableCell.d.ts.map +1 -0
  75. package/dist/elements/TableCell.js +69 -0
  76. package/dist/elements/Text.d.ts +17 -0
  77. package/dist/elements/Text.d.ts.map +1 -0
  78. package/dist/elements/Text.js +96 -0
  79. package/dist/elements/TextField.d.ts +25 -0
  80. package/dist/elements/TextField.d.ts.map +1 -0
  81. package/dist/elements/TextField.js +257 -0
  82. package/dist/elements/accessibility.d.ts +36 -0
  83. package/dist/elements/accessibility.d.ts.map +1 -0
  84. package/dist/elements/accessibility.js +244 -0
  85. package/dist/elements/className.d.ts +45 -0
  86. package/dist/elements/className.d.ts.map +1 -0
  87. package/dist/elements/className.js +8 -0
  88. package/dist/elements/decimal.d.ts +5 -0
  89. package/dist/elements/decimal.d.ts.map +1 -0
  90. package/dist/elements/decimal.js +11 -0
  91. package/dist/elements/documentUrl.d.ts +35 -0
  92. package/dist/elements/documentUrl.d.ts.map +1 -0
  93. package/dist/elements/documentUrl.js +89 -0
  94. package/dist/elements/drag-drop.d.ts +11 -0
  95. package/dist/elements/drag-drop.d.ts.map +1 -0
  96. package/dist/elements/drag-drop.js +277 -0
  97. package/dist/elements/drag.d.ts +53 -0
  98. package/dist/elements/drag.d.ts.map +1 -0
  99. package/dist/elements/drag.js +520 -0
  100. package/dist/elements/dragModifiers.d.ts +22 -0
  101. package/dist/elements/dragModifiers.d.ts.map +1 -0
  102. package/dist/elements/dragModifiers.js +62 -0
  103. package/dist/elements/events.d.ts +45 -0
  104. package/dist/elements/events.d.ts.map +1 -0
  105. package/dist/elements/events.js +101 -0
  106. package/dist/elements/fieldBounds.d.ts +5 -0
  107. package/dist/elements/fieldBounds.d.ts.map +1 -0
  108. package/dist/elements/fieldBounds.js +60 -0
  109. package/dist/elements/flow.d.ts +36 -0
  110. package/dist/elements/flow.d.ts.map +1 -0
  111. package/dist/elements/flow.js +47 -0
  112. package/dist/elements/form.d.ts +48 -0
  113. package/dist/elements/form.d.ts.map +1 -0
  114. package/dist/elements/form.js +112 -0
  115. package/dist/elements/grid.d.ts +96 -0
  116. package/dist/elements/grid.d.ts.map +1 -0
  117. package/dist/elements/grid.js +272 -0
  118. package/dist/elements/index.d.ts +36 -0
  119. package/dist/elements/index.d.ts.map +1 -0
  120. package/dist/elements/index.js +35 -0
  121. package/dist/elements/inherited.d.ts +181 -0
  122. package/dist/elements/inherited.d.ts.map +1 -0
  123. package/dist/elements/inherited.js +512 -0
  124. package/dist/elements/labels.d.ts +39 -0
  125. package/dist/elements/labels.d.ts.map +1 -0
  126. package/dist/elements/labels.js +163 -0
  127. package/dist/elements/measure.d.ts +27 -0
  128. package/dist/elements/measure.d.ts.map +1 -0
  129. package/dist/elements/measure.js +24 -0
  130. package/dist/elements/outsidePress.d.ts +31 -0
  131. package/dist/elements/outsidePress.d.ts.map +1 -0
  132. package/dist/elements/outsidePress.js +94 -0
  133. package/dist/elements/paste.d.ts +22 -0
  134. package/dist/elements/paste.d.ts.map +1 -0
  135. package/dist/elements/paste.js +49 -0
  136. package/dist/elements/press.d.ts +41 -0
  137. package/dist/elements/press.d.ts.map +1 -0
  138. package/dist/elements/press.js +131 -0
  139. package/dist/elements/rangeKeyboard.d.ts +41 -0
  140. package/dist/elements/rangeKeyboard.d.ts.map +1 -0
  141. package/dist/elements/rangeKeyboard.js +58 -0
  142. package/dist/elements/rangeValues.d.ts +20 -0
  143. package/dist/elements/rangeValues.d.ts.map +1 -0
  144. package/dist/elements/rangeValues.js +101 -0
  145. package/dist/elements/ref.d.ts +16 -0
  146. package/dist/elements/ref.d.ts.map +1 -0
  147. package/dist/elements/ref.js +22 -0
  148. package/dist/elements/reveal.d.ts +8 -0
  149. package/dist/elements/reveal.d.ts.map +1 -0
  150. package/dist/elements/reveal.js +10 -0
  151. package/dist/elements/scrollReveal.d.ts +19 -0
  152. package/dist/elements/scrollReveal.d.ts.map +1 -0
  153. package/dist/elements/scrollReveal.js +52 -0
  154. package/dist/elements/selectOptions.d.ts +25 -0
  155. package/dist/elements/selectOptions.d.ts.map +1 -0
  156. package/dist/elements/selectOptions.js +60 -0
  157. package/dist/elements/style.d.ts +30 -0
  158. package/dist/elements/style.d.ts.map +1 -0
  159. package/dist/elements/style.js +368 -0
  160. package/dist/elements/svgHover.d.ts +41 -0
  161. package/dist/elements/svgHover.d.ts.map +1 -0
  162. package/dist/elements/svgHover.js +152 -0
  163. package/dist/elements/table.d.ts +52 -0
  164. package/dist/elements/table.d.ts.map +1 -0
  165. package/dist/elements/table.js +104 -0
  166. package/dist/elements/types.d.ts +25 -0
  167. package/dist/elements/types.d.ts.map +1 -0
  168. package/dist/elements/types.js +8 -0
  169. package/dist/elements/viewport.d.ts +53 -0
  170. package/dist/elements/viewport.d.ts.map +1 -0
  171. package/dist/elements/viewport.js +62 -0
  172. package/dist/elements/wheelZoom.d.ts +12 -0
  173. package/dist/elements/wheelZoom.d.ts.map +1 -0
  174. package/dist/elements/wheelZoom.js +8 -0
  175. package/dist/index.d.ts +9 -4
  176. package/dist/index.d.ts.map +1 -1
  177. package/dist/index.js +9 -4
  178. package/dist/platform/focus.d.ts +31 -0
  179. package/dist/platform/focus.d.ts.map +1 -0
  180. package/dist/platform/focus.js +93 -0
  181. package/dist/platform/index.d.ts +31 -0
  182. package/dist/platform/index.d.ts.map +1 -0
  183. package/dist/platform/index.js +179 -0
  184. package/dist/platform/layout.d.ts +13 -0
  185. package/dist/platform/layout.d.ts.map +1 -0
  186. package/dist/platform/layout.js +15 -0
  187. package/dist/platform/position.d.ts +5 -0
  188. package/dist/platform/position.d.ts.map +1 -0
  189. package/dist/platform/position.js +177 -0
  190. package/dist/platform/stack.d.ts +86 -0
  191. package/dist/platform/stack.d.ts.map +1 -0
  192. package/dist/platform/stack.js +318 -0
  193. package/package.json +67 -8
@@ -0,0 +1,520 @@
1
+ import { createContext, useCallback, useLayoutEffect, useRef, } from 'react';
2
+ import { useDragModifiers } from './dragModifiers.js';
3
+ import { mouseEvent } from './events.js';
4
+ const NO_MODIFIERS = {
5
+ altKey: false,
6
+ ctrlKey: false,
7
+ metaKey: false,
8
+ shiftKey: false,
9
+ };
10
+ const TOUCH_SLOP = 8;
11
+ const LONG_PRESS_MS = 500;
12
+ const DOUBLE_TAP_MS = 300;
13
+ const DOUBLE_TAP_RADIUS = 24;
14
+ /** The viewport and pointer stream shared by the shapes in a native SVG canvas. */
15
+ export const SvgDragScope = createContext(null);
16
+ /** Lets a descendant with a secondary tap action negotiate its ancestor's long-press drag. */
17
+ export const LongPressDragScope = createContext(null);
18
+ /**
19
+ * Preserves hover, focus and assistive activation when a View owns the touch responder.
20
+ *
21
+ * @param props - The element's hover callbacks.
22
+ * @param press - The press interaction's focus and assistive actions.
23
+ * @param activate - The primary assistive activation, if present.
24
+ * @param secondary - The localized secondary action, if present.
25
+ * @returns Props for the same View that owns the drag.
26
+ */
27
+ export function dragViewActions(props, press, activate, secondary) {
28
+ const actions = press.accessibilityActions ?? [];
29
+ const hasActivate = actions.some(({ name }) => name === 'activate');
30
+ return {
31
+ focusable: press.focusable,
32
+ onFocus: press.onFocus,
33
+ onBlur: press.onBlur,
34
+ onPointerDown: press.onPointerDown,
35
+ onPointerEnter: props.onMouseEnter
36
+ ? (event) => {
37
+ if (event.nativeEvent.pointerType !== 'touch')
38
+ props.onMouseEnter?.(mouseEvent(event));
39
+ }
40
+ : undefined,
41
+ onPointerLeave: props.onMouseLeave
42
+ ? (event) => {
43
+ if (event.nativeEvent.pointerType !== 'touch')
44
+ props.onMouseLeave?.(mouseEvent(event));
45
+ }
46
+ : undefined,
47
+ onAccessibilityTap: activate,
48
+ accessibilityActions: activate || secondary || actions.length
49
+ ? [
50
+ ...actions,
51
+ ...(activate && !hasActivate ? [{ name: 'activate' }] : []),
52
+ ...(secondary ? [{ name: 'secondaryAction', label: secondary.label }] : []),
53
+ ]
54
+ : undefined,
55
+ onAccessibilityAction(event) {
56
+ const { actionName } = event.nativeEvent;
57
+ if (actionName === 'activate' && activate)
58
+ activate();
59
+ else if (actionName === 'secondaryAction')
60
+ secondary?.run();
61
+ else
62
+ press.onAccessibilityAction?.(event);
63
+ },
64
+ };
65
+ }
66
+ /**
67
+ * Retains a measurable host and forwards the original ref to its caller.
68
+ *
69
+ * @param forward - The caller's host ref.
70
+ * @returns The frame ref and the callback to attach to the host.
71
+ */
72
+ export function useDragFrame(forward) {
73
+ const frame = useRef(null);
74
+ const attach = useCallback((host) => {
75
+ frame.current = host;
76
+ forward?.(host);
77
+ }, [forward]);
78
+ return [frame, attach];
79
+ }
80
+ /** Pairs the touch root with the host bounds in screen coordinates. */
81
+ function measureFrame(host, receive) {
82
+ let root;
83
+ let bounds;
84
+ const finish = () => {
85
+ if (root === undefined || bounds === undefined)
86
+ return;
87
+ receive(root && bounds ? { rootX: root.x, rootY: root.y, bounds } : null);
88
+ };
89
+ // Responder page coordinates and measure's page coordinates both use the touch root.
90
+ // Pair that measurement with the window frame to account for modal and embedded roots.
91
+ host.measure((_x, _y, _width, _height, pageX, pageY) => {
92
+ root = Number.isFinite(pageX) && Number.isFinite(pageY) ? { x: pageX, y: pageY } : null;
93
+ finish();
94
+ });
95
+ host.measureInWindow((x, y, width, height) => {
96
+ bounds = [x, y, width, height].every(Number.isFinite) ? { x, y, width, height } : null;
97
+ finish();
98
+ });
99
+ }
100
+ /** Copies native contact data before React Native can reuse the event. */
101
+ function snapshot(touch) {
102
+ const { identifier, pageX, pageY, locationX, locationY, timestamp } = touch;
103
+ if (!Number.isFinite(pageX) || !Number.isFinite(pageY))
104
+ return undefined;
105
+ return { identifier, pageX, pageY, locationX, locationY, timestamp };
106
+ }
107
+ /** Reads the touches changed in this notification. */
108
+ function changedTouches({ nativeEvent }) {
109
+ return nativeEvent.changedTouches?.length ? nativeEvent.changedTouches : [nativeEvent];
110
+ }
111
+ /** Looks up the active contact in a responder update. */
112
+ function changed(event, id) {
113
+ const touches = changedTouches(event);
114
+ const touch = id === undefined ? touches[0] : touches.find((item) => item.identifier === id);
115
+ return touch && snapshot(touch);
116
+ }
117
+ /** Converts a touch root coordinate to a point in the window. */
118
+ function windowPoint(contact, frame) {
119
+ return {
120
+ x: contact.pageX - frame.rootX + frame.bounds.x,
121
+ y: contact.pageY - frame.rootY + frame.bounds.y,
122
+ };
123
+ }
124
+ /** Describes the contact relative to the host and to the drag origin. */
125
+ function sample(contact, frame, origin, modifiers = NO_MODIFIERS) {
126
+ const position = windowPoint(contact, frame);
127
+ return {
128
+ x: position.x - frame.bounds.x,
129
+ y: position.y - frame.bounds.y,
130
+ dx: position.x - origin.x,
131
+ dy: position.y - origin.y,
132
+ bounds: frame.bounds,
133
+ ...modifiers,
134
+ };
135
+ }
136
+ /**
137
+ * Owns contact, tap and drag through one native gesture responder on a View or SVG shape.
138
+ *
139
+ * @param frame - The receiving View, or the enclosing SVG viewport.
140
+ * @param options - The drag and tap behavior to dispatch.
141
+ * @returns Responder props, or undefined when ordinary pressability should own the host.
142
+ */
143
+ export function useNativeDrag(frame, options) {
144
+ const latest = useRef(options);
145
+ const active = useRef(null);
146
+ const started = useRef(null);
147
+ const cache = useRef(null);
148
+ const pending = useRef([]);
149
+ const lastTap = useRef(null);
150
+ const hasDrag = options.drag !== undefined;
151
+ const localModifiers = useDragModifiers();
152
+ const modifiers = options.modifiers ?? localModifiers;
153
+ const hasZoom = options.zoom !== undefined;
154
+ const enabled = hasDrag || hasZoom || options.secondary !== undefined;
155
+ useLayoutEffect(() => {
156
+ latest.current = options;
157
+ });
158
+ useLayoutEffect(() => () => {
159
+ const current = active.current;
160
+ active.current = null;
161
+ const last = started.current?.last;
162
+ started.current = null;
163
+ cache.current = null;
164
+ lastTap.current = null;
165
+ pending.current = [];
166
+ clearTimeout(current?.timer);
167
+ if (last)
168
+ latest.current.drag?.onCancel?.(last);
169
+ }, [enabled, hasDrag, hasZoom, frame]);
170
+ if (!enabled)
171
+ return undefined;
172
+ /** Delivers responder updates in order even when measurement completes out of order. */
173
+ function enqueue(send, host, waitForPointer) {
174
+ const queue = pending.current;
175
+ let measured;
176
+ let pointerReady = !waitForPointer;
177
+ const delivery = {
178
+ ready: false,
179
+ send() {
180
+ const bounds = measured ?? cache.current;
181
+ if (measured)
182
+ cache.current = measured;
183
+ send(bounds);
184
+ },
185
+ };
186
+ queue.push(delivery);
187
+ const drain = () => {
188
+ if (pending.current !== queue)
189
+ return;
190
+ delivery.ready = measured !== undefined && pointerReady;
191
+ // A fast release can arrive before the first bridge measurement. Deliver in touch order.
192
+ while (queue[0]?.ready && pending.current === queue)
193
+ queue.shift()?.send();
194
+ };
195
+ const ready = (bounds) => {
196
+ measured = bounds;
197
+ drain();
198
+ };
199
+ waitForPointer?.(() => {
200
+ pointerReady = true;
201
+ drain();
202
+ });
203
+ if (host)
204
+ measureFrame(host, ready);
205
+ else
206
+ ready(null);
207
+ }
208
+ /** Dispatches the drag phase using the measured host frame. */
209
+ function deliver(current, contact, phase) {
210
+ let keys = modifiers.read(contact, phase);
211
+ const waitForPointer = !keys && Number.isFinite(contact.timestamp)
212
+ ? (ready) => queueMicrotask(() => {
213
+ keys = modifiers.read(contact, phase);
214
+ ready();
215
+ })
216
+ : undefined;
217
+ enqueue((bounds) => {
218
+ const drag = latest.current.drag;
219
+ if (phase === 'start')
220
+ current.downModifiers = keys;
221
+ if (!bounds || !drag)
222
+ return;
223
+ if (!current.origin) {
224
+ current.origin = windowPoint(current.down, bounds);
225
+ current.last = sample(current.down, bounds, current.origin, current.downModifiers);
226
+ started.current = current;
227
+ drag.onStart(current.last);
228
+ }
229
+ if (phase === 'start' || started.current !== current)
230
+ return;
231
+ const previous = current.last;
232
+ const next = sample(contact, bounds, current.origin, keys);
233
+ current.last = next;
234
+ if (phase === 'end') {
235
+ started.current = null;
236
+ drag.onEnd?.(next);
237
+ }
238
+ else if (previous?.x !== next.x ||
239
+ previous.y !== next.y ||
240
+ previous.dx !== next.dx ||
241
+ previous.dy !== next.dy ||
242
+ previous.bounds.width !== next.bounds.width ||
243
+ previous.bounds.height !== next.bounds.height ||
244
+ previous.altKey !== next.altKey ||
245
+ previous.ctrlKey !== next.ctrlKey ||
246
+ previous.metaKey !== next.metaKey ||
247
+ previous.shiftKey !== next.shiftKey) {
248
+ drag.onMove?.(next);
249
+ }
250
+ }, frame?.current ?? undefined, waitForPointer);
251
+ }
252
+ /** Records that this contact crossed the drag threshold. */
253
+ function acquire(current) {
254
+ current.acquired = true;
255
+ deliver(current, current.down, 'start');
256
+ }
257
+ /** Activates an ordinary press or recognizes a second tap. */
258
+ function tap(contact, event, at) {
259
+ const { secondary, press } = latest.current;
260
+ const previous = lastTap.current;
261
+ if (secondary &&
262
+ previous &&
263
+ at - previous.at <= DOUBLE_TAP_MS &&
264
+ Math.hypot(contact.pageX - previous.contact.pageX, contact.pageY - previous.contact.pageY) <=
265
+ DOUBLE_TAP_RADIUS) {
266
+ lastTap.current = null;
267
+ secondary.run();
268
+ }
269
+ else {
270
+ lastTap.current = secondary ? { contact, at } : null;
271
+ press?.onPress?.(event);
272
+ }
273
+ }
274
+ /** Cancels the active drag after already queued updates are delivered. */
275
+ function cancel(current, takenOver = false) {
276
+ enqueue(() => {
277
+ if (started.current !== current)
278
+ return;
279
+ started.current = null;
280
+ const drag = latest.current.drag;
281
+ if (current.last)
282
+ (drag?.onCancel ?? (takenOver ? drag?.onEnd : undefined))?.(current.last);
283
+ });
284
+ }
285
+ /** Ends contact and dispatches the terminal gesture and press callbacks. */
286
+ function finish(current, contact, event) {
287
+ if (active.current !== current)
288
+ return;
289
+ active.current = null;
290
+ clearTimeout(current.timer);
291
+ if (current.acquired) {
292
+ if (contact && !current.yielded && !current.longPressed)
293
+ deliver(current, contact, 'end');
294
+ else
295
+ cancel(current);
296
+ }
297
+ if (event) {
298
+ event.persist();
299
+ const at = Date.now();
300
+ enqueue(() => {
301
+ latest.current.press?.onPressOut?.(event);
302
+ if (contact &&
303
+ !current.fromPinch &&
304
+ !current.moved &&
305
+ !current.yielded &&
306
+ !current.longPressed)
307
+ tap(contact, event, at);
308
+ });
309
+ }
310
+ }
311
+ /** Queues the pair in touch order, using the outer frame of that sample. */
312
+ function pair(pinch, rebase) {
313
+ const [first, second] = pinch.contacts.values();
314
+ if (!first || !second)
315
+ return;
316
+ enqueue((bounds) => {
317
+ if (!bounds) {
318
+ pinch.previous = undefined;
319
+ return;
320
+ }
321
+ const at = {
322
+ x: (first.pageX + second.pageX) / 2 - bounds.rootX,
323
+ y: (first.pageY + second.pageY) / 2 - bounds.rootY,
324
+ };
325
+ const distance = Math.hypot(first.pageX - second.pageX, first.pageY - second.pageY);
326
+ const previous = pinch.previous;
327
+ pinch.previous = { at, distance };
328
+ if (rebase || !previous || !(previous.distance > 0) || !(distance > 0))
329
+ return;
330
+ const factor = distance / previous.distance;
331
+ if (factor !== 1 || previous.at.x !== at.x || previous.at.y !== at.y) {
332
+ latest.current.zoom?.onZoom({ kind: 'pinch', from: previous.at, at, factor });
333
+ }
334
+ }, frame?.current ?? undefined);
335
+ }
336
+ /** Refreshes the positions of contacts already accepted by this responder. */
337
+ function updateContacts(pinch, touches) {
338
+ for (const touch of touches) {
339
+ const point = snapshot(touch);
340
+ if (point && pinch.contacts.has(point.identifier))
341
+ pinch.contacts.set(point.identifier, point);
342
+ }
343
+ }
344
+ /** Starts a one-contact session, including a new drag after a pinch. */
345
+ function begin(down, pinch, event) {
346
+ const { drag, press } = latest.current;
347
+ const longPress = drag?.touchStart === 'longPress';
348
+ const both = drag?.axis !== 'horizontal' && drag?.axis !== 'vertical';
349
+ const current = {
350
+ down,
351
+ pinch,
352
+ acquired: false,
353
+ moved: false,
354
+ fromPinch: !event,
355
+ yielded: false,
356
+ longPressed: false,
357
+ axisChosen: Boolean(pinch) || both || longPress,
358
+ };
359
+ active.current = current;
360
+ if (event)
361
+ press?.onPressIn?.(event);
362
+ if (event && drag && !longPress)
363
+ acquire(current);
364
+ if (event && (longPress || press?.onLongPress)) {
365
+ event.persist();
366
+ current.timer = setTimeout(() => {
367
+ if (active.current !== current || (current.pinch && current.pinch.contacts.size > 1))
368
+ return;
369
+ if (current.moved)
370
+ return;
371
+ if (longPress)
372
+ acquire(current);
373
+ else {
374
+ current.longPressed = true;
375
+ latest.current.press?.onLongPress?.(event);
376
+ }
377
+ }, LONG_PRESS_MS);
378
+ }
379
+ return current;
380
+ }
381
+ /** Retires released contacts and establishes a basis for the pair or finger that remains. */
382
+ function lift(current, pinch, ended) {
383
+ const [first, second] = pinch.contacts.keys();
384
+ for (const id of ended)
385
+ pinch.contacts.delete(id);
386
+ if (second === undefined ||
387
+ !(ended.includes(second) || (first !== undefined && ended.includes(first)))) {
388
+ return;
389
+ }
390
+ const [remaining] = pinch.contacts.values();
391
+ if (pinch.contacts.size >= 2)
392
+ pair(pinch, true);
393
+ else if (remaining)
394
+ begin(remaining, pinch);
395
+ }
396
+ return {
397
+ onStartShouldSetResponder: () => active.current === null,
398
+ onResponderGrant(event) {
399
+ const down = changed(event);
400
+ if (!down || active.current)
401
+ return false;
402
+ const { drag, zoom } = latest.current;
403
+ const pinch = zoom ? { contacts: new Map([[down.identifier, down]]) } : undefined;
404
+ const current = begin(down, pinch, event);
405
+ // Reserve the opted-in touch plane from native scroll interception. Other drags
406
+ // retain their existing axis and long-press negotiation.
407
+ return Boolean(zoom || (drag && drag.touchStart !== 'longPress' && current.axisChosen));
408
+ },
409
+ onResponderStart(event) {
410
+ active.current?.lift?.();
411
+ const current = active.current;
412
+ const pinch = current?.pinch;
413
+ if (!current || !pinch)
414
+ return;
415
+ const before = pinch.contacts.size;
416
+ for (const touch of changedTouches(event)) {
417
+ const contact = snapshot(touch);
418
+ if (contact)
419
+ pinch.contacts.set(contact.identifier, contact);
420
+ }
421
+ if (before >= 2 || pinch.contacts.size < 2)
422
+ return;
423
+ updateContacts(pinch, event.nativeEvent.touches);
424
+ clearTimeout(current.timer);
425
+ current.moved = true;
426
+ lastTap.current = null;
427
+ if (current.acquired) {
428
+ current.acquired = false;
429
+ cancel(current, true);
430
+ }
431
+ pair(pinch, true);
432
+ },
433
+ onResponderMove(event) {
434
+ active.current?.lift?.();
435
+ const current = active.current;
436
+ if (!current)
437
+ return;
438
+ const pinch = current.pinch;
439
+ if (pinch) {
440
+ updateContacts(pinch, event.nativeEvent.touches);
441
+ if (pinch.contacts.size >= 2) {
442
+ const [first, second] = pinch.contacts.keys();
443
+ if (changedTouches(event).some(({ identifier }) => identifier === first || identifier === second)) {
444
+ pair(pinch, false);
445
+ }
446
+ return;
447
+ }
448
+ }
449
+ if (current.yielded || current.longPressed)
450
+ return;
451
+ const contact = changed(event, current.down.identifier);
452
+ if (!contact)
453
+ return;
454
+ const dx = Math.abs(contact.pageX - current.down.pageX);
455
+ const dy = Math.abs(contact.pageY - current.down.pageY);
456
+ if (Math.max(dx, dy) > TOUCH_SLOP && !current.moved) {
457
+ current.moved = true;
458
+ clearTimeout(current.timer);
459
+ lastTap.current = null;
460
+ if (!current.axisChosen) {
461
+ current.axisChosen = true;
462
+ const perpendicular = latest.current.drag?.axis === 'horizontal' ? dy > dx : dx > dy;
463
+ if (perpendicular) {
464
+ current.yielded = true;
465
+ if (current.acquired) {
466
+ current.acquired = false;
467
+ cancel(current);
468
+ }
469
+ }
470
+ }
471
+ }
472
+ if (current.fromPinch && !current.acquired && latest.current.drag)
473
+ acquire(current);
474
+ if (current.acquired)
475
+ deliver(current, contact, 'move');
476
+ },
477
+ onResponderEnd(event) {
478
+ active.current?.lift?.();
479
+ const current = active.current;
480
+ if (!current)
481
+ return;
482
+ event.persist();
483
+ const contact = changed(event, current.down.identifier);
484
+ const pinch = current.pinch;
485
+ // RN emits responder-end immediately before terminate on cancellation. Let
486
+ // that terminal event win, while preserving order before any later input.
487
+ const retire = () => {
488
+ if (active.current !== current || current.lift !== retire)
489
+ return;
490
+ current.lift = undefined;
491
+ if (pinch && pinch.contacts.size >= 2) {
492
+ updateContacts(pinch, event.nativeEvent.touches);
493
+ lift(current, pinch, changedTouches(event).map(({ identifier }) => identifier));
494
+ }
495
+ else if (contact)
496
+ finish(current, contact, event);
497
+ };
498
+ current.lift = retire;
499
+ queueMicrotask(retire);
500
+ },
501
+ onResponderRelease(event) {
502
+ const current = active.current;
503
+ if (current)
504
+ finish(current, changed(event, current.down.identifier), event);
505
+ },
506
+ onResponderTerminate(event) {
507
+ const current = active.current;
508
+ if (current)
509
+ finish(current, undefined, event);
510
+ },
511
+ onResponderTerminationRequest: () => {
512
+ const current = active.current;
513
+ return !current || (!current.pinch && (!current.acquired || !current.axisChosen));
514
+ },
515
+ ...(hasDrag && !options.modifiers ? modifiers.handlers : undefined),
516
+ onLayout() {
517
+ cache.current = null;
518
+ },
519
+ };
520
+ }
@@ -0,0 +1,22 @@
1
+ import type { NativeTouchEvent, ViewProps } from 'react-native';
2
+ import type { KitModifierKeys } from '@molecule/app-ui';
3
+ type Phase = 'start' | 'move' | 'end';
4
+ type Contact = Pick<NativeTouchEvent, 'identifier' | 'timestamp' | 'pageX' | 'pageY' | 'locationX' | 'locationY'>;
5
+ /** Modifier metadata captured on the same host as the responder or its SVG viewport. */
6
+ export interface DragModifiers {
7
+ read(contact: Contact, phase: Phase): KitModifierKeys | undefined;
8
+ handlers: Pick<ViewProps, 'onPointerDownCapture' | 'onPointerMoveCapture' | 'onPointerUpCapture'>;
9
+ }
10
+ /**
11
+ * Pairs responder samples with optional React Native 0.79 W3C pointer packets in the same dispatch.
12
+ * Android sends the MotionEvent timestamp and ID on both events. iOS allocates IDs separately,
13
+ * so match the dispatched position in the root or target. Responder motion remains the owner.
14
+ *
15
+ * The object is created once per component, so passing it through context or effect
16
+ * dependencies does not re-render or re-run anything.
17
+ *
18
+ * @returns The pointer capture handlers and a reader for responder contact modifiers.
19
+ */
20
+ export declare function useDragModifiers(): DragModifiers;
21
+ export {};
22
+ //# sourceMappingURL=dragModifiers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dragModifiers.d.ts","sourceRoot":"","sources":["../../src/elements/dragModifiers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAsB,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAEnF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAEvD,KAAK,KAAK,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAA;AACrC,KAAK,OAAO,GAAG,IAAI,CACjB,gBAAgB,EAChB,YAAY,GAAG,WAAW,GAAG,OAAO,GAAG,OAAO,GAAG,WAAW,GAAG,WAAW,CAC3E,CAAA;AAGD,wFAAwF;AACxF,MAAM,WAAW,aAAa;IAC5B,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,GAAG,eAAe,GAAG,SAAS,CAAA;IACjE,QAAQ,EAAE,IAAI,CAAC,SAAS,EAAE,sBAAsB,GAAG,sBAAsB,GAAG,oBAAoB,CAAC,CAAA;CAClG;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAGhD"}
@@ -0,0 +1,62 @@
1
+ import { useState } from 'react';
2
+ /**
3
+ * Pairs responder samples with optional React Native 0.79 W3C pointer packets in the same dispatch.
4
+ * Android sends the MotionEvent timestamp and ID on both events. iOS allocates IDs separately,
5
+ * so match the dispatched position in the root or target. Responder motion remains the owner.
6
+ *
7
+ * The object is created once per component, so passing it through context or effect
8
+ * dependencies does not re-render or re-run anything.
9
+ *
10
+ * @returns The pointer capture handlers and a reader for responder contact modifiers.
11
+ */
12
+ export function useDragModifiers() {
13
+ const [modifiers] = useState(createDragModifiers);
14
+ return modifiers;
15
+ }
16
+ /**
17
+ * Creates the packet store and its handlers for one host.
18
+ *
19
+ * @returns The pointer capture handlers and a reader for responder contact modifiers.
20
+ */
21
+ function createDragModifiers() {
22
+ const packets = [];
23
+ /** Retains a pointer packet for the matching responder notification. */
24
+ function remember(phase, pointer) {
25
+ const packet = { phase, pointer };
26
+ packets.push(packet);
27
+ queueMicrotask(() => {
28
+ const index = packets.indexOf(packet);
29
+ if (index !== -1)
30
+ packets.splice(index, 1);
31
+ });
32
+ }
33
+ return {
34
+ handlers: {
35
+ onPointerDownCapture: ({ nativeEvent }) => remember('start', nativeEvent),
36
+ onPointerMoveCapture: ({ nativeEvent }) => remember('move', nativeEvent),
37
+ onPointerUpCapture: ({ nativeEvent }) => remember('end', nativeEvent),
38
+ },
39
+ read(contact, phase) {
40
+ const matches = packets.filter(({ phase: kind, pointer }) => {
41
+ if (kind !== phase)
42
+ return false;
43
+ if ('timestamp' in pointer && typeof pointer.timestamp === 'number') {
44
+ return (pointer.timestamp === contact.timestamp &&
45
+ String(pointer.pointerId) === String(contact.identifier));
46
+ }
47
+ return (samePosition(pointer.pageX, pointer.pageY, contact.pageX, contact.pageY) ||
48
+ samePosition(pointer.offsetX, pointer.offsetY, contact.locationX, contact.locationY));
49
+ });
50
+ const [match] = matches;
51
+ if (!match || matches.length !== 1)
52
+ return undefined;
53
+ packets.splice(packets.indexOf(match), 1);
54
+ const { altKey, ctrlKey, metaKey, shiftKey } = match.pointer;
55
+ return { altKey, ctrlKey, metaKey, shiftKey };
56
+ },
57
+ };
58
+ }
59
+ /** Matches coordinates through the native float conversion. */
60
+ function samePosition(x, y, otherX, otherY) {
61
+ return Math.abs(x - otherX) < 0.001 && Math.abs(y - otherY) < 0.001;
62
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Web-shaped events for the handlers of native elements.
3
+ *
4
+ * @module
5
+ */
6
+ import type { KitEvent, KitFocusEvent, KitKeyboardEvent, KitMouseEvent } from '@molecule/app-ui';
7
+ /**
8
+ * The members every kit event has, over a native event.
9
+ *
10
+ * @param stop - Stops the native event's propagation.
11
+ * @param target - The field whose value changed.
12
+ * @returns The event members.
13
+ */
14
+ export declare function kitEvent(stop?: () => void, target?: EventTarget | null): KitEvent;
15
+ /** A change event whose target carries the input's current properties. */
16
+ export declare function fieldEvent(properties: object): KitEvent;
17
+ /**
18
+ * A press, hover or click as the mouse event a handler written for the web receives.
19
+ *
20
+ * @param event - The native event.
21
+ * @param button - The mouse button that changed state.
22
+ * @returns The mouse event.
23
+ */
24
+ export declare function mouseEvent(event: {
25
+ nativeEvent: {
26
+ pageX: number;
27
+ pageY: number;
28
+ };
29
+ stopPropagation(): void;
30
+ } | undefined, button?: number): KitMouseEvent;
31
+ /**
32
+ * A key a device's keyboard sent to a text field, as the keyboard event a handler written for the
33
+ * web receives. A device's keyboard holds no modifiers and does not repeat.
34
+ *
35
+ * @param key - The key value React Native reports (`'a'`, `'Enter'`, `'Backspace'`).
36
+ * @returns The keyboard event.
37
+ */
38
+ export declare function keyboardEvent(key: string): KitKeyboardEvent;
39
+ /**
40
+ * A native focus or blur as the focus event a handler written for the web receives.
41
+ *
42
+ * @returns The focus event.
43
+ */
44
+ export declare function focusEvent(): KitFocusEvent;
45
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/elements/events.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAEhG;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACtB,IAAI,GAAE,MAAM,IAAsB,EAClC,MAAM,GAAE,WAAW,GAAG,IAAW,GAChC,QAAQ,CAaV;AAED,0EAA0E;AAC1E,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,QAAQ,CAUvD;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE;IAAE,WAAW,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAAC,eAAe,IAAI,IAAI,CAAA;CAAE,GAAG,SAAS,EAC7F,MAAM,SAAI,GACT,aAAa,CAoBf;AAeD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,CAa3D;AAED;;;;GAIG;AACH,wBAAgB,UAAU,IAAI,aAAa,CAK1C"}