UNPKG

5.92 kBTypeScriptView Raw
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10import {GestureResponderHandlers} from '../../types/public/ReactNativeRenderer';
11import {GestureResponderEvent} from '../Types/CoreEventTypes';
12
13export interface PanResponderGestureState {
14 /**
15 * ID of the gestureState- persisted as long as there at least one touch on
16 */
17 stateID: number;
18
19 /**
20 * the latest screen coordinates of the recently-moved touch
21 */
22 moveX: number;
23
24 /**
25 * the latest screen coordinates of the recently-moved touch
26 */
27 moveY: number;
28
29 /**
30 * the screen coordinates of the responder grant
31 */
32 x0: number;
33
34 /**
35 * the screen coordinates of the responder grant
36 */
37 y0: number;
38
39 /**
40 * accumulated distance of the gesture since the touch started
41 */
42 dx: number;
43
44 /**
45 * accumulated distance of the gesture since the touch started
46 */
47 dy: number;
48
49 /**
50 * current velocity of the gesture
51 */
52 vx: number;
53
54 /**
55 * current velocity of the gesture
56 */
57 vy: number;
58
59 /**
60 * Number of touches currently on screen
61 */
62 numberActiveTouches: number;
63
64 // All `gestureState` accounts for timeStamps up until:
65 _accountsForMovesUpTo: number;
66}
67
68/**
69 * @see documentation of GestureResponderHandlers
70 */
71export interface PanResponderCallbacks {
72 onMoveShouldSetPanResponder?:
73 | ((
74 e: GestureResponderEvent,
75 gestureState: PanResponderGestureState,
76 ) => boolean)
77 | undefined;
78 onStartShouldSetPanResponder?:
79 | ((
80 e: GestureResponderEvent,
81 gestureState: PanResponderGestureState,
82 ) => boolean)
83 | undefined;
84 onPanResponderGrant?:
85 | ((
86 e: GestureResponderEvent,
87 gestureState: PanResponderGestureState,
88 ) => void)
89 | undefined;
90 onPanResponderMove?:
91 | ((
92 e: GestureResponderEvent,
93 gestureState: PanResponderGestureState,
94 ) => void)
95 | undefined;
96 onPanResponderRelease?:
97 | ((
98 e: GestureResponderEvent,
99 gestureState: PanResponderGestureState,
100 ) => void)
101 | undefined;
102 onPanResponderTerminate?:
103 | ((
104 e: GestureResponderEvent,
105 gestureState: PanResponderGestureState,
106 ) => void)
107 | undefined;
108
109 onMoveShouldSetPanResponderCapture?:
110 | ((
111 e: GestureResponderEvent,
112 gestureState: PanResponderGestureState,
113 ) => boolean)
114 | undefined;
115 onStartShouldSetPanResponderCapture?:
116 | ((
117 e: GestureResponderEvent,
118 gestureState: PanResponderGestureState,
119 ) => boolean)
120 | undefined;
121 onPanResponderReject?:
122 | ((
123 e: GestureResponderEvent,
124 gestureState: PanResponderGestureState,
125 ) => void)
126 | undefined;
127 onPanResponderStart?:
128 | ((
129 e: GestureResponderEvent,
130 gestureState: PanResponderGestureState,
131 ) => void)
132 | undefined;
133 onPanResponderEnd?:
134 | ((
135 e: GestureResponderEvent,
136 gestureState: PanResponderGestureState,
137 ) => void)
138 | undefined;
139 onPanResponderTerminationRequest?:
140 | ((
141 e: GestureResponderEvent,
142 gestureState: PanResponderGestureState,
143 ) => boolean)
144 | undefined;
145 onShouldBlockNativeResponder?:
146 | ((
147 e: GestureResponderEvent,
148 gestureState: PanResponderGestureState,
149 ) => boolean)
150 | undefined;
151}
152
153export interface PanResponderInstance {
154 panHandlers: GestureResponderHandlers;
155}
156
157/**
158 * PanResponder reconciles several touches into a single gesture.
159 * It makes single-touch gestures resilient to extra touches,
160 * and can be used to recognize simple multi-touch gestures.
161 *
162 * It provides a predictable wrapper of the responder handlers provided by the gesture responder system.
163 * For each handler, it provides a new gestureState object alongside the normal event.
164 */
165export interface PanResponderStatic {
166 /**
167 * @param config Enhanced versions of all of the responder callbacks
168 * that provide not only the typical `ResponderSyntheticEvent`, but also the
169 * `PanResponder` gesture state. Simply replace the word `Responder` with
170 * `PanResponder` in each of the typical `onResponder*` callbacks. For
171 * example, the `config` object would look like:
172 *
173 * - `onMoveShouldSetPanResponder: (e, gestureState) => {...}`
174 * - `onMoveShouldSetPanResponderCapture: (e, gestureState) => {...}`
175 * - `onStartShouldSetPanResponder: (e, gestureState) => {...}`
176 * - `onStartShouldSetPanResponderCapture: (e, gestureState) => {...}`
177 * - `onPanResponderReject: (e, gestureState) => {...}`
178 * - `onPanResponderGrant: (e, gestureState) => {...}`
179 * - `onPanResponderStart: (e, gestureState) => {...}`
180 * - `onPanResponderEnd: (e, gestureState) => {...}`
181 * - `onPanResponderRelease: (e, gestureState) => {...}`
182 * - `onPanResponderMove: (e, gestureState) => {...}`
183 * - `onPanResponderTerminate: (e, gestureState) => {...}`
184 * - `onPanResponderTerminationRequest: (e, gestureState) => {...}`
185 * - `onShouldBlockNativeResponder: (e, gestureState) => {...}`
186 *
187 * In general, for events that have capture equivalents, we update the
188 * gestureState once in the capture phase and can use it in the bubble phase
189 * as well.
190 *
191 * Be careful with onStartShould* callbacks. They only reflect updated
192 * `gestureState` for start/end events that bubble/capture to the Node.
193 * Once the node is the responder, you can rely on every start/end event
194 * being processed by the gesture and `gestureState` being updated
195 * accordingly. (numberActiveTouches) may not be totally accurate unless you
196 * are the responder.
197 */
198 create(config: PanResponderCallbacks): PanResponderInstance;
199}
200
201export const PanResponder: PanResponderStatic;
202export type PanResponder = PanResponderStatic;