@molecule/app-ui-react-native 2.0.1 → 2.1.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 (182) hide show
  1. package/README.md +42 -7
  2. package/dist/elements/Anchor.d.ts +15 -0
  3. package/dist/elements/Anchor.d.ts.map +1 -0
  4. package/dist/elements/Anchor.js +57 -0
  5. package/dist/elements/Box.d.ts +19 -0
  6. package/dist/elements/Box.d.ts.map +1 -0
  7. package/dist/elements/Box.js +150 -0
  8. package/dist/elements/CheckField.d.ts +18 -0
  9. package/dist/elements/CheckField.d.ts.map +1 -0
  10. package/dist/elements/CheckField.js +103 -0
  11. package/dist/elements/Disclosure.d.ts +11 -0
  12. package/dist/elements/Disclosure.d.ts.map +1 -0
  13. package/dist/elements/Disclosure.js +67 -0
  14. package/dist/elements/Fieldset.d.ts +16 -0
  15. package/dist/elements/Fieldset.d.ts.map +1 -0
  16. package/dist/elements/Fieldset.js +34 -0
  17. package/dist/elements/FileField.d.ts +12 -0
  18. package/dist/elements/FileField.d.ts.map +1 -0
  19. package/dist/elements/FileField.js +111 -0
  20. package/dist/elements/FormArea.d.ts +18 -0
  21. package/dist/elements/FormArea.d.ts.map +1 -0
  22. package/dist/elements/FormArea.js +42 -0
  23. package/dist/elements/GlyphIcon.d.ts +19 -0
  24. package/dist/elements/GlyphIcon.d.ts.map +1 -0
  25. package/dist/elements/GlyphIcon.js +43 -0
  26. package/dist/elements/Image.d.ts +17 -0
  27. package/dist/elements/Image.d.ts.map +1 -0
  28. package/dist/elements/Image.js +26 -0
  29. package/dist/elements/Link.d.ts +20 -0
  30. package/dist/elements/Link.d.ts.map +1 -0
  31. package/dist/elements/Link.js +53 -0
  32. package/dist/elements/Pressable.d.ts +18 -0
  33. package/dist/elements/Pressable.d.ts.map +1 -0
  34. package/dist/elements/Pressable.js +107 -0
  35. package/dist/elements/PulseDots.d.ts +15 -0
  36. package/dist/elements/PulseDots.d.ts.map +1 -0
  37. package/dist/elements/PulseDots.js +78 -0
  38. package/dist/elements/RangeField.d.ts +11 -0
  39. package/dist/elements/RangeField.d.ts.map +1 -0
  40. package/dist/elements/RangeField.js +259 -0
  41. package/dist/elements/RichText.d.ts +23 -0
  42. package/dist/elements/RichText.d.ts.map +1 -0
  43. package/dist/elements/RichText.js +55 -0
  44. package/dist/elements/Rule.d.ts +11 -0
  45. package/dist/elements/Rule.d.ts.map +1 -0
  46. package/dist/elements/Rule.js +19 -0
  47. package/dist/elements/ScrollArea.d.ts +21 -0
  48. package/dist/elements/ScrollArea.d.ts.map +1 -0
  49. package/dist/elements/ScrollArea.js +101 -0
  50. package/dist/elements/SelectAction.d.ts +20 -0
  51. package/dist/elements/SelectAction.d.ts.map +1 -0
  52. package/dist/elements/SelectAction.js +113 -0
  53. package/dist/elements/SelectField.d.ts +11 -0
  54. package/dist/elements/SelectField.d.ts.map +1 -0
  55. package/dist/elements/SelectField.js +220 -0
  56. package/dist/elements/SelectMenu.d.ts +19 -0
  57. package/dist/elements/SelectMenu.d.ts.map +1 -0
  58. package/dist/elements/SelectMenu.js +122 -0
  59. package/dist/elements/Svg.d.ts +155 -0
  60. package/dist/elements/Svg.d.ts.map +1 -0
  61. package/dist/elements/Svg.js +525 -0
  62. package/dist/elements/TableBox.d.ts +19 -0
  63. package/dist/elements/TableBox.d.ts.map +1 -0
  64. package/dist/elements/TableBox.js +87 -0
  65. package/dist/elements/TableCell.d.ts +16 -0
  66. package/dist/elements/TableCell.d.ts.map +1 -0
  67. package/dist/elements/TableCell.js +69 -0
  68. package/dist/elements/Text.d.ts +17 -0
  69. package/dist/elements/Text.d.ts.map +1 -0
  70. package/dist/elements/Text.js +96 -0
  71. package/dist/elements/TextField.d.ts +25 -0
  72. package/dist/elements/TextField.d.ts.map +1 -0
  73. package/dist/elements/TextField.js +257 -0
  74. package/dist/elements/accessibility.d.ts +36 -0
  75. package/dist/elements/accessibility.d.ts.map +1 -0
  76. package/dist/elements/accessibility.js +239 -0
  77. package/dist/elements/className.d.ts +45 -0
  78. package/dist/elements/className.d.ts.map +1 -0
  79. package/dist/elements/className.js +8 -0
  80. package/dist/elements/decimal.d.ts +5 -0
  81. package/dist/elements/decimal.d.ts.map +1 -0
  82. package/dist/elements/decimal.js +11 -0
  83. package/dist/elements/documentUrl.d.ts +35 -0
  84. package/dist/elements/documentUrl.d.ts.map +1 -0
  85. package/dist/elements/documentUrl.js +89 -0
  86. package/dist/elements/drag-drop.d.ts +11 -0
  87. package/dist/elements/drag-drop.d.ts.map +1 -0
  88. package/dist/elements/drag-drop.js +277 -0
  89. package/dist/elements/drag.d.ts +47 -0
  90. package/dist/elements/drag.d.ts.map +1 -0
  91. package/dist/elements/drag.js +362 -0
  92. package/dist/elements/events.d.ts +45 -0
  93. package/dist/elements/events.d.ts.map +1 -0
  94. package/dist/elements/events.js +101 -0
  95. package/dist/elements/fieldBounds.d.ts +5 -0
  96. package/dist/elements/fieldBounds.d.ts.map +1 -0
  97. package/dist/elements/fieldBounds.js +60 -0
  98. package/dist/elements/flow.d.ts +36 -0
  99. package/dist/elements/flow.d.ts.map +1 -0
  100. package/dist/elements/flow.js +47 -0
  101. package/dist/elements/form.d.ts +48 -0
  102. package/dist/elements/form.d.ts.map +1 -0
  103. package/dist/elements/form.js +112 -0
  104. package/dist/elements/grid.d.ts +96 -0
  105. package/dist/elements/grid.d.ts.map +1 -0
  106. package/dist/elements/grid.js +272 -0
  107. package/dist/elements/index.d.ts +36 -0
  108. package/dist/elements/index.d.ts.map +1 -0
  109. package/dist/elements/index.js +35 -0
  110. package/dist/elements/inherited.d.ts +181 -0
  111. package/dist/elements/inherited.d.ts.map +1 -0
  112. package/dist/elements/inherited.js +512 -0
  113. package/dist/elements/labels.d.ts +39 -0
  114. package/dist/elements/labels.d.ts.map +1 -0
  115. package/dist/elements/labels.js +163 -0
  116. package/dist/elements/measure.d.ts +27 -0
  117. package/dist/elements/measure.d.ts.map +1 -0
  118. package/dist/elements/measure.js +24 -0
  119. package/dist/elements/outsidePress.d.ts +31 -0
  120. package/dist/elements/outsidePress.d.ts.map +1 -0
  121. package/dist/elements/outsidePress.js +94 -0
  122. package/dist/elements/paste.d.ts +22 -0
  123. package/dist/elements/paste.d.ts.map +1 -0
  124. package/dist/elements/paste.js +49 -0
  125. package/dist/elements/press.d.ts +41 -0
  126. package/dist/elements/press.d.ts.map +1 -0
  127. package/dist/elements/press.js +131 -0
  128. package/dist/elements/rangeKeyboard.d.ts +41 -0
  129. package/dist/elements/rangeKeyboard.d.ts.map +1 -0
  130. package/dist/elements/rangeKeyboard.js +58 -0
  131. package/dist/elements/rangeValues.d.ts +20 -0
  132. package/dist/elements/rangeValues.d.ts.map +1 -0
  133. package/dist/elements/rangeValues.js +101 -0
  134. package/dist/elements/ref.d.ts +16 -0
  135. package/dist/elements/ref.d.ts.map +1 -0
  136. package/dist/elements/ref.js +22 -0
  137. package/dist/elements/reveal.d.ts +8 -0
  138. package/dist/elements/reveal.d.ts.map +1 -0
  139. package/dist/elements/reveal.js +10 -0
  140. package/dist/elements/scrollReveal.d.ts +19 -0
  141. package/dist/elements/scrollReveal.d.ts.map +1 -0
  142. package/dist/elements/scrollReveal.js +52 -0
  143. package/dist/elements/selectOptions.d.ts +25 -0
  144. package/dist/elements/selectOptions.d.ts.map +1 -0
  145. package/dist/elements/selectOptions.js +60 -0
  146. package/dist/elements/style.d.ts +30 -0
  147. package/dist/elements/style.d.ts.map +1 -0
  148. package/dist/elements/style.js +363 -0
  149. package/dist/elements/svgHover.d.ts +41 -0
  150. package/dist/elements/svgHover.d.ts.map +1 -0
  151. package/dist/elements/svgHover.js +152 -0
  152. package/dist/elements/table.d.ts +52 -0
  153. package/dist/elements/table.d.ts.map +1 -0
  154. package/dist/elements/table.js +104 -0
  155. package/dist/elements/types.d.ts +25 -0
  156. package/dist/elements/types.d.ts.map +1 -0
  157. package/dist/elements/types.js +8 -0
  158. package/dist/elements/viewport.d.ts +53 -0
  159. package/dist/elements/viewport.d.ts.map +1 -0
  160. package/dist/elements/viewport.js +62 -0
  161. package/dist/elements/wheelZoom.d.ts +11 -0
  162. package/dist/elements/wheelZoom.d.ts.map +1 -0
  163. package/dist/elements/wheelZoom.js +7 -0
  164. package/dist/index.d.ts +5 -2
  165. package/dist/index.d.ts.map +1 -1
  166. package/dist/index.js +5 -2
  167. package/dist/platform/focus.d.ts +31 -0
  168. package/dist/platform/focus.d.ts.map +1 -0
  169. package/dist/platform/focus.js +86 -0
  170. package/dist/platform/index.d.ts +31 -0
  171. package/dist/platform/index.d.ts.map +1 -0
  172. package/dist/platform/index.js +179 -0
  173. package/dist/platform/layout.d.ts +13 -0
  174. package/dist/platform/layout.d.ts.map +1 -0
  175. package/dist/platform/layout.js +15 -0
  176. package/dist/platform/position.d.ts +5 -0
  177. package/dist/platform/position.d.ts.map +1 -0
  178. package/dist/platform/position.js +177 -0
  179. package/dist/platform/stack.d.ts +86 -0
  180. package/dist/platform/stack.d.ts.map +1 -0
  181. package/dist/platform/stack.js +318 -0
  182. package/package.json +64 -6
@@ -0,0 +1,277 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * React Native drag gestures and accessible destination controls.
4
+ *
5
+ * @module
6
+ */
7
+ import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState, } from 'react';
8
+ import { PanResponder, View } from 'react-native';
9
+ import { DragDropRendererProvider, } from '@molecule/app-drag-drop/react';
10
+ import { measureElement } from './measure.js';
11
+ import { Pressable } from './Pressable.js';
12
+ const ScopeContext = createContext(null);
13
+ const DRAG_THRESHOLD = 8;
14
+ const MEASUREMENT_TIMEOUT_MS = 250;
15
+ /** Measures a snapshot of the targets registered to the current gesture scope. */
16
+ function measureTargets(targets) {
17
+ return new Promise((resolve) => {
18
+ const views = [...targets];
19
+ const positions = [];
20
+ let pending = views.length;
21
+ if (!pending) {
22
+ resolve(positions);
23
+ return;
24
+ }
25
+ // An unmounted view may never call back. Finish with the views that still measured.
26
+ const timeout = setTimeout(() => resolve([...positions]), MEASUREMENT_TIMEOUT_MS);
27
+ for (const [id, measure] of views) {
28
+ measure((box) => {
29
+ if (box.width > 0 && box.height > 0)
30
+ positions.push({ ...box, id, measure });
31
+ pending--;
32
+ if (!pending) {
33
+ clearTimeout(timeout);
34
+ resolve(positions);
35
+ }
36
+ });
37
+ }
38
+ });
39
+ }
40
+ /** Finds a mounted target in the current scope whose measured bounds contain the position. */
41
+ function targetAt(targets, positions, x, y) {
42
+ return positions.find((position) => targets.get(position.id) === position.measure &&
43
+ x >= position.x &&
44
+ x < position.x + position.width &&
45
+ y >= position.y &&
46
+ y < position.y + position.height);
47
+ }
48
+ /** Bounds each drag gesture and the target registry to one feature instance. */
49
+ function DragScope({ children, onDragStart, onDragMove, onDragEnd, retainTarget = false, }) {
50
+ const handlers = useRef({ onDragStart, onDragMove, onDragEnd });
51
+ const active = useRef(null);
52
+ useLayoutEffect(() => {
53
+ handlers.current = { onDragStart, onDragMove, onDragEnd };
54
+ }, [onDragStart, onDragMove, onDragEnd]);
55
+ const scope = useMemo(() => {
56
+ const targets = new Map();
57
+ const end = (gesture, reason, target) => {
58
+ if (active.current !== gesture)
59
+ return;
60
+ active.current = null;
61
+ const over = target ?? (retainTarget && reason !== 'unmount' ? gesture.over : undefined);
62
+ const overId = over && targets.get(over.id) === over.measure ? over.id : undefined;
63
+ handlers.current.onDragEnd?.({ ...gesture.token, overId, reason });
64
+ };
65
+ const visit = (gesture, positions, { x, y }) => {
66
+ const over = targetAt(targets, positions, x, y);
67
+ if ((!over && retainTarget) || over?.measure === gesture.over?.measure)
68
+ return;
69
+ gesture.over = over;
70
+ handlers.current.onDragMove?.({ ...gesture.token, overId: over?.id });
71
+ };
72
+ return {
73
+ register: (id, measure) => {
74
+ targets.set(id, measure);
75
+ return () => {
76
+ if (targets.get(id) === measure)
77
+ targets.delete(id);
78
+ };
79
+ },
80
+ begin: (source) => {
81
+ if (active.current)
82
+ end(active.current, 'cancel');
83
+ // The source keeps only this identity. Measured views belong to the active scope.
84
+ const token = { ...source };
85
+ const gesture = { token, pending: [] };
86
+ active.current = gesture;
87
+ handlers.current.onDragStart?.(source);
88
+ if (retainTarget || handlers.current.onDragMove) {
89
+ void measureTargets(targets).then((positions) => {
90
+ if (active.current !== gesture)
91
+ return;
92
+ gesture.positions = positions;
93
+ for (const point of gesture.pending.splice(0))
94
+ visit(gesture, positions, point);
95
+ if (gesture.ending === 'cancel')
96
+ end(gesture, 'cancel');
97
+ });
98
+ }
99
+ return token;
100
+ },
101
+ hover: (token, x, y) => {
102
+ const gesture = active.current;
103
+ if (gesture?.token !== token || gesture.ending)
104
+ return;
105
+ if (gesture.positions)
106
+ visit(gesture, gesture.positions, { x, y });
107
+ else if (retainTarget)
108
+ gesture.pending.push({ x, y });
109
+ },
110
+ release: (token, x, y) => {
111
+ const gesture = active.current;
112
+ if (gesture?.token !== token || gesture.ending)
113
+ return;
114
+ gesture.ending = 'drop';
115
+ void measureTargets(targets).then((positions) => {
116
+ if (active.current !== gesture || gesture.ending !== 'drop')
117
+ return;
118
+ for (const point of gesture.pending.splice(0))
119
+ visit(gesture, positions, point);
120
+ end(gesture, 'drop', targetAt(targets, positions, x, y));
121
+ });
122
+ },
123
+ cancel: (token, reason = 'cancel') => {
124
+ const gesture = active.current;
125
+ if (gesture?.token !== token)
126
+ return;
127
+ // A fast interruption can arrive before the bridge measures the targets.
128
+ if (reason === 'cancel' && gesture.pending.length)
129
+ gesture.ending = reason;
130
+ else
131
+ end(gesture, reason);
132
+ },
133
+ activate: (source, targetId) => {
134
+ if (!targets.has(targetId))
135
+ return;
136
+ if (active.current)
137
+ end(active.current, 'cancel');
138
+ handlers.current.onDragEnd?.({ ...source, overId: targetId, reason: 'drop' });
139
+ },
140
+ };
141
+ }, [retainTarget]);
142
+ useLayoutEffect(() => () => {
143
+ if (active.current)
144
+ scope.cancel(active.current.token, 'unmount');
145
+ }, [scope]);
146
+ return _jsx(ScopeContext.Provider, { value: scope, children: children });
147
+ }
148
+ /** Reads the interaction scope that owns this source or target. */
149
+ function useGestureScope() {
150
+ const scope = useContext(ScopeContext);
151
+ if (!scope)
152
+ throw new Error('Native drag sources and targets require a drag scope.');
153
+ return scope;
154
+ }
155
+ /** Registers the native host whose measured bounds can accept a drop. */
156
+ function useTargetRef(id, measure) {
157
+ const { register } = useGestureScope();
158
+ const unregister = useRef(null);
159
+ return useCallback((host) => {
160
+ unregister.current?.();
161
+ unregister.current = host ? register(id, (receive) => measure(host, receive)) : null;
162
+ }, [id, measure, register]);
163
+ }
164
+ /** Matches the root coordinates emitted by the legacy PanResponder source. */
165
+ function measureInRoot(view, receive) {
166
+ view.measure((_left, _top, width, height, x, y) => receive({ x, y, width, height }));
167
+ }
168
+ /** Measures and registers the native host that accepts a drop. */
169
+ function DropTarget({ id, children }) {
170
+ const setView = useTargetRef(id, measureInRoot);
171
+ return (_jsx(View, { collapsable: false, ref: setView, children: children }));
172
+ }
173
+ /** Binds a native responder and provides individually accessible move actions. */
174
+ function DragSource({ source, controls, children }) {
175
+ const scope = useGestureScope();
176
+ const currentSource = useRef(source);
177
+ const gesture = useRef(null);
178
+ useLayoutEffect(() => {
179
+ currentSource.current = source;
180
+ }, [source]);
181
+ useLayoutEffect(() => () => {
182
+ if (gesture.current)
183
+ scope.cancel(gesture.current, 'unmount');
184
+ }, [scope]);
185
+ const { panHandlers } = useMemo(() => {
186
+ const cancel = () => {
187
+ if (gesture.current)
188
+ scope.cancel(gesture.current);
189
+ gesture.current = null;
190
+ };
191
+ return PanResponder.create({
192
+ onMoveShouldSetPanResponder: (_event, { dx, dy, numberActiveTouches }) => numberActiveTouches === 1 && Math.hypot(dx, dy) >= DRAG_THRESHOLD,
193
+ onPanResponderGrant: () => {
194
+ gesture.current = scope.begin(currentSource.current);
195
+ },
196
+ onPanResponderStart: (_event, { numberActiveTouches }) => {
197
+ if (numberActiveTouches > 1)
198
+ cancel();
199
+ },
200
+ onPanResponderMove: (_event, { moveX, moveY }) => {
201
+ if (gesture.current)
202
+ scope.hover(gesture.current, moveX, moveY);
203
+ },
204
+ onPanResponderRelease: ({ nativeEvent: { pageX, pageY } }) => {
205
+ if (gesture.current)
206
+ scope.release(gesture.current, pageX, pageY);
207
+ },
208
+ onPanResponderTerminationRequest: () => true,
209
+ onPanResponderReject: cancel,
210
+ onPanResponderTerminate: cancel,
211
+ });
212
+ }, [scope]);
213
+ return (_jsxs(_Fragment, { children: [_jsx(View, { accessible: false, ...panHandlers, children: children }), _jsx(MoveControls, { source: source, controls: controls })] }));
214
+ }
215
+ /** Offers accessible move actions using the same target IDs as a drag. */
216
+ function MoveControls({ source, controls, }) {
217
+ const scope = useGestureScope();
218
+ const [expanded, setExpanded] = useState(false);
219
+ if (!controls || controls.choices.length === 0)
220
+ return null;
221
+ const controlLabel = expanded ? controls.cancelLabel : controls.label;
222
+ return (_jsxs(_Fragment, { children: [_jsx(Pressable, { as: "button", type: "button", className: controls.className, "data-mol-id": controls.id, "aria-label": controlLabel, "aria-expanded": expanded, onClick: () => setExpanded(!expanded), children: controlLabel }), expanded &&
223
+ controls.choices.map(({ targetId, label }) => (_jsx(Pressable, { as: "button", type: "button", className: controls.className, "data-mol-id": `${controls.id}-to-${targetId}`, "aria-label": label, onClick: () => {
224
+ setExpanded(false);
225
+ scope.activate(source, targetId);
226
+ }, children: label }, targetId)))] }));
227
+ }
228
+ /** Keeps the current target when dragging between renders. */
229
+ function ScopedScope(props) {
230
+ return _jsx(DragScope, { ...props, retainTarget: true });
231
+ }
232
+ /** Binds a draggable element and its move controls to the scope. */
233
+ function ScopedSource({ source, controls, disabled, children, }) {
234
+ const scope = useGestureScope();
235
+ const token = useRef(null);
236
+ useLayoutEffect(() => () => {
237
+ if (token.current)
238
+ scope.cancel(token.current, 'unmount');
239
+ token.current = null;
240
+ }, [scope, source.id, disabled]);
241
+ /** Sends the element contact to the scope in window coordinates. */
242
+ function move({ x, y, bounds }, terminal) {
243
+ if (!token.current)
244
+ return;
245
+ const send = terminal ? scope.release : scope.hover;
246
+ send(token.current, bounds.x + x, bounds.y + y);
247
+ }
248
+ const drag = disabled
249
+ ? undefined
250
+ : {
251
+ onStart: () => {
252
+ token.current = scope.begin(source);
253
+ },
254
+ onMove: (point) => move(point, false),
255
+ onEnd: (point) => move(point, true),
256
+ onCancel: () => {
257
+ if (token.current)
258
+ scope.cancel(token.current);
259
+ },
260
+ };
261
+ return (_jsxs(_Fragment, { children: [children({ drag }), !disabled && _jsx(MoveControls, { source: source, controls: controls })] }));
262
+ }
263
+ /** Wires an element as a target, optionally with a drag source. */
264
+ function ScopedTarget({ id, source, children }) {
265
+ const ref = useTargetRef(id, measureElement);
266
+ return source ? (_jsx(ScopedSource, { ...source, source: { id }, children: ({ drag }) => children({ ref, drag }) })) : (_jsx(_Fragment, { children: children({ ref }) }));
267
+ }
268
+ const nativeDragDrop = {
269
+ Scope: DragScope,
270
+ Source: DragSource,
271
+ Target: DropTarget,
272
+ scoped: { Scope: ScopedScope, Source: ScopedSource, Target: ScopedTarget },
273
+ };
274
+ /** Enables React Native drag-and-drop gestures and move controls in the subtree. */
275
+ export function NativeDragDropProvider({ children }) {
276
+ return _jsx(DragDropRendererProvider, { value: nativeDragDrop, children: children });
277
+ }
@@ -0,0 +1,47 @@
1
+ import { type RefCallback, type RefObject } from 'react';
2
+ import type { PressableProps, View, ViewProps } from 'react-native';
3
+ import type { BoxProps, ElementDrag, InteractionProps } from '@molecule/app-ui';
4
+ type FrameHost = Pick<View, 'measure' | 'measureInWindow'>;
5
+ type FrameRef = RefObject<FrameHost | null>;
6
+ type PressHandlers = Pick<PressableProps, 'onPress' | 'onPressIn' | 'onPressOut' | 'onLongPress'>;
7
+ type AccessiblePressHandlers = Pick<PressableProps, 'onFocus' | 'onBlur' | 'focusable' | 'onPointerDown' | 'accessibilityActions' | 'onAccessibilityAction'>;
8
+ type Options = {
9
+ drag?: ElementDrag;
10
+ secondary?: BoxProps['secondaryAction'];
11
+ press?: PressHandlers;
12
+ };
13
+ type Responder = Pick<ViewProps, 'onStartShouldSetResponder' | 'onResponderGrant' | 'onResponderMove' | 'onResponderEnd' | 'onResponderRelease' | 'onResponderTerminate' | 'onResponderTerminationRequest' | 'onLayout'>;
14
+ /** The viewport frame shared by the shapes in the nearest native SVG canvas. */
15
+ export declare const SvgDragFrame: import("react").Context<FrameRef | null>;
16
+ /** Lets a descendant with a secondary tap action negotiate its ancestor's long-press drag. */
17
+ export declare const LongPressDragScope: import("react").Context<{
18
+ frame: FrameRef;
19
+ drag: ElementDrag;
20
+ } | null>;
21
+ /**
22
+ * Preserves hover, focus and assistive activation when a View owns the touch responder.
23
+ *
24
+ * @param props - The element's hover callbacks.
25
+ * @param press - The press interaction's focus and assistive actions.
26
+ * @param activate - The primary assistive activation, if present.
27
+ * @param secondary - The localized secondary action, if present.
28
+ * @returns Props for the same View that owns the drag.
29
+ */
30
+ export declare function dragViewActions(props: InteractionProps, press: AccessiblePressHandlers, activate?: () => void, secondary?: BoxProps['secondaryAction']): ViewProps & Pick<PressableProps, 'onFocus' | 'onBlur'>;
31
+ /**
32
+ * Retains a measurable host and forwards the original ref to its caller.
33
+ *
34
+ * @param forward - The caller's host ref.
35
+ * @returns The frame ref and the callback to attach to the host.
36
+ */
37
+ export declare function useDragFrame(forward?: RefCallback<View>): [RefObject<View | null>, RefCallback<View>];
38
+ /**
39
+ * Owns contact, tap and drag through one native gesture responder on a View or SVG shape.
40
+ *
41
+ * @param frame - The receiving View, or the enclosing SVG viewport.
42
+ * @param options - The drag and tap behavior to dispatch.
43
+ * @returns Responder props, or undefined when ordinary pressability should own the host.
44
+ */
45
+ export declare function useNativeDrag(frame: FrameRef | null, options: Options): Responder | undefined;
46
+ export {};
47
+ //# sourceMappingURL=drag.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drag.d.ts","sourceRoot":"","sources":["../../src/elements/drag.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,SAAS,EAIf,MAAM,OAAO,CAAA;AACd,OAAO,KAAK,EAGV,cAAc,EACd,IAAI,EACJ,SAAS,EACV,MAAM,cAAc,CAAA;AAErB,OAAO,KAAK,EACV,QAAQ,EAER,WAAW,EAEX,gBAAgB,EACjB,MAAM,kBAAkB,CAAA;AAIzB,KAAK,SAAS,GAAG,IAAI,CAAC,IAAI,EAAE,SAAS,GAAG,iBAAiB,CAAC,CAAA;AAC1D,KAAK,QAAQ,GAAG,SAAS,CAAC,SAAS,GAAG,IAAI,CAAC,CAAA;AAK3C,KAAK,aAAa,GAAG,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,WAAW,GAAG,YAAY,GAAG,aAAa,CAAC,CAAA;AACjG,KAAK,uBAAuB,GAAG,IAAI,CACjC,cAAc,EACZ,SAAS,GACT,QAAQ,GACR,WAAW,GACX,eAAe,GACf,sBAAsB,GACtB,uBAAuB,CAC1B,CAAA;AACD,KAAK,OAAO,GAAG;IACb,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB,SAAS,CAAC,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAA;IACvC,KAAK,CAAC,EAAE,aAAa,CAAA;CACtB,CAAA;AACD,KAAK,SAAS,GAAG,IAAI,CACnB,SAAS,EACP,2BAA2B,GAC3B,kBAAkB,GAClB,iBAAiB,GACjB,gBAAgB,GAChB,oBAAoB,GACpB,sBAAsB,GACtB,+BAA+B,GAC/B,UAAU,CACb,CAAA;AAmBD,gFAAgF;AAChF,eAAO,MAAM,YAAY,0CAAuC,CAAA;AAEhE,8FAA8F;AAC9F,eAAO,MAAM,kBAAkB;WAA0B,QAAQ;UAAQ,WAAW;SAAgB,CAAA;AAEpG;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,gBAAgB,EACvB,KAAK,EAAE,uBAAuB,EAC9B,QAAQ,CAAC,EAAE,MAAM,IAAI,EACrB,SAAS,CAAC,EAAE,QAAQ,CAAC,iBAAiB,CAAC,GACtC,SAAS,GAAG,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,QAAQ,CAAC,CAkCxD;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAC1B,OAAO,CAAC,EAAE,WAAW,CAAC,IAAI,CAAC,GAC1B,CAAC,SAAS,CAAC,IAAI,GAAG,IAAI,CAAC,EAAE,WAAW,CAAC,IAAI,CAAC,CAAC,CAU7C;AAwDD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,EAAE,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,CA2N7F"}
@@ -0,0 +1,362 @@
1
+ import { createContext, useCallback, useLayoutEffect, useRef, } from 'react';
2
+ import { mouseEvent } from './events.js';
3
+ const TOUCH_SLOP = 8;
4
+ const LONG_PRESS_MS = 500;
5
+ const DOUBLE_TAP_MS = 300;
6
+ const DOUBLE_TAP_RADIUS = 24;
7
+ /** The viewport frame shared by the shapes in the nearest native SVG canvas. */
8
+ export const SvgDragFrame = createContext(null);
9
+ /** Lets a descendant with a secondary tap action negotiate its ancestor's long-press drag. */
10
+ export const LongPressDragScope = createContext(null);
11
+ /**
12
+ * Preserves hover, focus and assistive activation when a View owns the touch responder.
13
+ *
14
+ * @param props - The element's hover callbacks.
15
+ * @param press - The press interaction's focus and assistive actions.
16
+ * @param activate - The primary assistive activation, if present.
17
+ * @param secondary - The localized secondary action, if present.
18
+ * @returns Props for the same View that owns the drag.
19
+ */
20
+ export function dragViewActions(props, press, activate, secondary) {
21
+ const actions = press.accessibilityActions ?? [];
22
+ const hasActivate = actions.some(({ name }) => name === 'activate');
23
+ return {
24
+ focusable: press.focusable,
25
+ onFocus: press.onFocus,
26
+ onBlur: press.onBlur,
27
+ onPointerDown: press.onPointerDown,
28
+ onPointerEnter: props.onMouseEnter
29
+ ? (event) => {
30
+ if (event.nativeEvent.pointerType !== 'touch')
31
+ props.onMouseEnter?.(mouseEvent(event));
32
+ }
33
+ : undefined,
34
+ onPointerLeave: props.onMouseLeave
35
+ ? (event) => {
36
+ if (event.nativeEvent.pointerType !== 'touch')
37
+ props.onMouseLeave?.(mouseEvent(event));
38
+ }
39
+ : undefined,
40
+ onAccessibilityTap: activate,
41
+ accessibilityActions: activate || secondary || actions.length
42
+ ? [
43
+ ...actions,
44
+ ...(activate && !hasActivate ? [{ name: 'activate' }] : []),
45
+ ...(secondary ? [{ name: 'secondaryAction', label: secondary.label }] : []),
46
+ ]
47
+ : undefined,
48
+ onAccessibilityAction(event) {
49
+ const { actionName } = event.nativeEvent;
50
+ if (actionName === 'activate' && activate)
51
+ activate();
52
+ else if (actionName === 'secondaryAction')
53
+ secondary?.run();
54
+ else
55
+ press.onAccessibilityAction?.(event);
56
+ },
57
+ };
58
+ }
59
+ /**
60
+ * Retains a measurable host and forwards the original ref to its caller.
61
+ *
62
+ * @param forward - The caller's host ref.
63
+ * @returns The frame ref and the callback to attach to the host.
64
+ */
65
+ export function useDragFrame(forward) {
66
+ const frame = useRef(null);
67
+ const attach = useCallback((host) => {
68
+ frame.current = host;
69
+ forward?.(host);
70
+ }, [forward]);
71
+ return [frame, attach];
72
+ }
73
+ /** Pairs the touch root with the host bounds in screen coordinates. */
74
+ function measureFrame(host, receive) {
75
+ let root;
76
+ let bounds;
77
+ const finish = () => {
78
+ if (root === undefined || bounds === undefined)
79
+ return;
80
+ receive(root && bounds ? { rootX: root.x, rootY: root.y, bounds } : null);
81
+ };
82
+ // Responder page coordinates and measure's page coordinates both use the touch root.
83
+ // Pair that measurement with the window frame to account for modal and embedded roots.
84
+ host.measure((_x, _y, _width, _height, pageX, pageY) => {
85
+ root = Number.isFinite(pageX) && Number.isFinite(pageY) ? { x: pageX, y: pageY } : null;
86
+ finish();
87
+ });
88
+ host.measureInWindow((x, y, width, height) => {
89
+ bounds = [x, y, width, height].every(Number.isFinite) ? { x, y, width, height } : null;
90
+ finish();
91
+ });
92
+ }
93
+ /** Looks up the active contact in a responder update. */
94
+ function changed(event, id) {
95
+ const { nativeEvent } = event;
96
+ const touches = nativeEvent.changedTouches?.length ? nativeEvent.changedTouches : [nativeEvent];
97
+ const touch = id === undefined ? touches[0] : touches.find((item) => item.identifier === id);
98
+ if (!touch || !Number.isFinite(touch.pageX) || !Number.isFinite(touch.pageY))
99
+ return undefined;
100
+ const { identifier, pageX, pageY } = touch;
101
+ return { identifier, pageX, pageY };
102
+ }
103
+ /** Converts a touch root coordinate to a point in the window. */
104
+ function windowPoint(contact, frame) {
105
+ return {
106
+ x: contact.pageX - frame.rootX + frame.bounds.x,
107
+ y: contact.pageY - frame.rootY + frame.bounds.y,
108
+ };
109
+ }
110
+ /** Describes the contact relative to the host and to the drag origin. */
111
+ function sample(contact, frame, origin) {
112
+ const position = windowPoint(contact, frame);
113
+ return {
114
+ x: position.x - frame.bounds.x,
115
+ y: position.y - frame.bounds.y,
116
+ dx: position.x - origin.x,
117
+ dy: position.y - origin.y,
118
+ bounds: frame.bounds,
119
+ altKey: false,
120
+ ctrlKey: false,
121
+ metaKey: false,
122
+ shiftKey: false,
123
+ };
124
+ }
125
+ /**
126
+ * Owns contact, tap and drag through one native gesture responder on a View or SVG shape.
127
+ *
128
+ * @param frame - The receiving View, or the enclosing SVG viewport.
129
+ * @param options - The drag and tap behavior to dispatch.
130
+ * @returns Responder props, or undefined when ordinary pressability should own the host.
131
+ */
132
+ export function useNativeDrag(frame, options) {
133
+ const latest = useRef(options);
134
+ const active = useRef(null);
135
+ const started = useRef(null);
136
+ const cache = useRef(null);
137
+ const pending = useRef([]);
138
+ const lastTap = useRef(null);
139
+ const hasDrag = options.drag !== undefined;
140
+ const enabled = hasDrag || options.secondary !== undefined;
141
+ useLayoutEffect(() => {
142
+ latest.current = options;
143
+ });
144
+ useLayoutEffect(() => () => {
145
+ const current = active.current;
146
+ active.current = null;
147
+ const last = started.current?.last;
148
+ started.current = null;
149
+ cache.current = null;
150
+ lastTap.current = null;
151
+ pending.current = [];
152
+ clearTimeout(current?.timer);
153
+ if (last)
154
+ latest.current.drag?.onCancel?.(last);
155
+ }, [enabled, hasDrag, frame]);
156
+ if (!enabled)
157
+ return undefined;
158
+ /** Delivers responder updates in order even when measurement completes out of order. */
159
+ function enqueue(send, host) {
160
+ const queue = pending.current;
161
+ let measured = null;
162
+ const delivery = {
163
+ ready: false,
164
+ send() {
165
+ const bounds = measured ?? cache.current;
166
+ if (measured)
167
+ cache.current = measured;
168
+ send(bounds);
169
+ },
170
+ };
171
+ queue.push(delivery);
172
+ const ready = (bounds) => {
173
+ if (pending.current !== queue)
174
+ return;
175
+ measured = bounds;
176
+ delivery.ready = true;
177
+ // A fast release can arrive before the first bridge measurement. Deliver in touch order.
178
+ while (queue[0]?.ready && pending.current === queue)
179
+ queue.shift()?.send();
180
+ };
181
+ if (host)
182
+ measureFrame(host, ready);
183
+ else
184
+ ready(null);
185
+ }
186
+ /** Dispatches the drag phase using the measured host frame. */
187
+ function deliver(current, contact, phase) {
188
+ enqueue((bounds) => {
189
+ const drag = latest.current.drag;
190
+ if (!bounds || !drag)
191
+ return;
192
+ if (!current.origin) {
193
+ current.origin = windowPoint(current.down, bounds);
194
+ current.last = sample(current.down, bounds, current.origin);
195
+ started.current = current;
196
+ drag.onStart(current.last);
197
+ }
198
+ if (phase === 'start' || started.current !== current)
199
+ return;
200
+ const previous = current.last;
201
+ const next = sample(contact, bounds, current.origin);
202
+ current.last = next;
203
+ if (phase === 'end') {
204
+ started.current = null;
205
+ drag.onEnd?.(next);
206
+ }
207
+ else if (previous?.x !== next.x ||
208
+ previous.y !== next.y ||
209
+ previous.dx !== next.dx ||
210
+ previous.dy !== next.dy ||
211
+ previous.bounds.width !== next.bounds.width ||
212
+ previous.bounds.height !== next.bounds.height) {
213
+ drag.onMove?.(next);
214
+ }
215
+ }, frame?.current ?? undefined);
216
+ }
217
+ /** Records that this contact crossed the drag threshold. */
218
+ function acquire(current) {
219
+ current.acquired = true;
220
+ deliver(current, current.down, 'start');
221
+ }
222
+ /** Activates an ordinary press or recognizes a second tap. */
223
+ function tap(contact, event, at) {
224
+ const { secondary, press } = latest.current;
225
+ const previous = lastTap.current;
226
+ if (secondary &&
227
+ previous &&
228
+ at - previous.at <= DOUBLE_TAP_MS &&
229
+ Math.hypot(contact.pageX - previous.contact.pageX, contact.pageY - previous.contact.pageY) <=
230
+ DOUBLE_TAP_RADIUS) {
231
+ lastTap.current = null;
232
+ secondary.run();
233
+ }
234
+ else {
235
+ lastTap.current = secondary ? { contact, at } : null;
236
+ press?.onPress?.(event);
237
+ }
238
+ }
239
+ /** Cancels the active drag after already queued updates are delivered. */
240
+ function cancel(current) {
241
+ enqueue(() => {
242
+ if (started.current !== current)
243
+ return;
244
+ started.current = null;
245
+ if (current.last)
246
+ latest.current.drag?.onCancel?.(current.last);
247
+ });
248
+ }
249
+ /** Ends contact and dispatches the terminal gesture and press callbacks. */
250
+ function finish(current, contact, event) {
251
+ if (active.current !== current)
252
+ return;
253
+ active.current = null;
254
+ clearTimeout(current.timer);
255
+ if (current.acquired) {
256
+ if (contact && !current.yielded && !current.longPressed)
257
+ deliver(current, contact, 'end');
258
+ else
259
+ cancel(current);
260
+ }
261
+ if (event) {
262
+ event.persist();
263
+ const at = Date.now();
264
+ enqueue(() => {
265
+ latest.current.press?.onPressOut?.(event);
266
+ if (contact && !current.moved && !current.yielded && !current.longPressed)
267
+ tap(contact, event, at);
268
+ });
269
+ }
270
+ }
271
+ return {
272
+ onStartShouldSetResponder: () => active.current === null,
273
+ onResponderGrant(event) {
274
+ const down = changed(event);
275
+ if (!down || active.current)
276
+ return;
277
+ const { drag, press } = latest.current;
278
+ const longPress = drag?.touchStart === 'longPress';
279
+ const both = drag?.axis !== 'horizontal' && drag?.axis !== 'vertical';
280
+ const current = {
281
+ down,
282
+ acquired: false,
283
+ moved: false,
284
+ yielded: false,
285
+ longPressed: false,
286
+ axisChosen: both || longPress,
287
+ };
288
+ active.current = current;
289
+ press?.onPressIn?.(event);
290
+ if (drag && !longPress)
291
+ acquire(current);
292
+ if (longPress || press?.onLongPress) {
293
+ event.persist();
294
+ current.timer = setTimeout(() => {
295
+ if (active.current !== current || current.moved)
296
+ return;
297
+ if (longPress)
298
+ acquire(current);
299
+ else {
300
+ current.longPressed = true;
301
+ latest.current.press?.onLongPress?.(event);
302
+ }
303
+ }, LONG_PRESS_MS);
304
+ }
305
+ // On Android, returning true prevents native ancestors from taking an immediate two-axis drag.
306
+ return Boolean(drag && !longPress && both);
307
+ },
308
+ onResponderMove(event) {
309
+ const current = active.current;
310
+ if (!current || current.yielded || current.longPressed)
311
+ return;
312
+ const contact = changed(event, current.down.identifier);
313
+ if (!contact)
314
+ return;
315
+ const dx = Math.abs(contact.pageX - current.down.pageX);
316
+ const dy = Math.abs(contact.pageY - current.down.pageY);
317
+ if (Math.max(dx, dy) > TOUCH_SLOP && !current.moved) {
318
+ current.moved = true;
319
+ clearTimeout(current.timer);
320
+ lastTap.current = null;
321
+ if (!current.axisChosen) {
322
+ current.axisChosen = true;
323
+ const perpendicular = latest.current.drag?.axis === 'horizontal' ? dy > dx : dx > dy;
324
+ if (perpendicular) {
325
+ current.yielded = true;
326
+ if (current.acquired) {
327
+ current.acquired = false;
328
+ cancel(current);
329
+ }
330
+ }
331
+ }
332
+ }
333
+ if (current.acquired)
334
+ deliver(current, contact, 'move');
335
+ },
336
+ onResponderEnd(event) {
337
+ const current = active.current;
338
+ if (!current)
339
+ return;
340
+ const contact = changed(event, current.down.identifier);
341
+ if (!contact)
342
+ return;
343
+ event.persist();
344
+ // RN also emits responder-end just before a cancel/terminate. Wait for that dispatch.
345
+ queueMicrotask(() => finish(current, contact, event));
346
+ },
347
+ onResponderRelease(event) {
348
+ const current = active.current;
349
+ if (current)
350
+ finish(current, changed(event, current.down.identifier), event);
351
+ },
352
+ onResponderTerminate(event) {
353
+ const current = active.current;
354
+ if (current)
355
+ finish(current, undefined, event);
356
+ },
357
+ onResponderTerminationRequest: () => !active.current?.acquired || !active.current.axisChosen,
358
+ onLayout() {
359
+ cache.current = null;
360
+ },
361
+ };
362
+ }