UNPKG

11.6 kBTypeScriptView Raw
1/// <reference types="react" />
2import { HitSlop, CommonGestureConfig, GestureTouchEvent, GestureStateChangeEvent, GestureUpdateEvent, ActiveCursor, MouseButton } from '../gestureHandlerCommon';
3import { GestureStateManagerType } from './gestureStateManager';
4import type { FlingGestureHandlerEventPayload, ForceTouchGestureHandlerEventPayload, LongPressGestureHandlerEventPayload, PanGestureHandlerEventPayload, PinchGestureHandlerEventPayload, RotationGestureHandlerEventPayload, TapGestureHandlerEventPayload, NativeViewGestureHandlerPayload } from '../GestureHandlerEventPayload';
5export type GestureType = BaseGesture<Record<string, unknown>> | BaseGesture<Record<string, never>> | BaseGesture<TapGestureHandlerEventPayload> | BaseGesture<PanGestureHandlerEventPayload> | BaseGesture<LongPressGestureHandlerEventPayload> | BaseGesture<RotationGestureHandlerEventPayload> | BaseGesture<PinchGestureHandlerEventPayload> | BaseGesture<FlingGestureHandlerEventPayload> | BaseGesture<ForceTouchGestureHandlerEventPayload> | BaseGesture<NativeViewGestureHandlerPayload>;
6export type GestureRef = number | GestureType | React.RefObject<GestureType | undefined> | React.RefObject<React.ComponentType | undefined>;
7export interface BaseGestureConfig extends CommonGestureConfig, Record<string, unknown> {
8 ref?: React.MutableRefObject<GestureType | undefined>;
9 requireToFail?: GestureRef[];
10 simultaneousWith?: GestureRef[];
11 blocksHandlers?: GestureRef[];
12 needsPointerData?: boolean;
13 manualActivation?: boolean;
14 runOnJS?: boolean;
15 testId?: string;
16 cancelsTouchesInView?: boolean;
17}
18type TouchEventHandlerType = (event: GestureTouchEvent, stateManager: GestureStateManagerType) => void;
19export type HandlerCallbacks<EventPayloadT extends Record<string, unknown>> = {
20 gestureId: number;
21 handlerTag: number;
22 onBegin?: (event: GestureStateChangeEvent<EventPayloadT>) => void;
23 onStart?: (event: GestureStateChangeEvent<EventPayloadT>) => void;
24 onEnd?: (event: GestureStateChangeEvent<EventPayloadT>, success: boolean) => void;
25 onFinalize?: (event: GestureStateChangeEvent<EventPayloadT>, success: boolean) => void;
26 onUpdate?: (event: GestureUpdateEvent<EventPayloadT>) => void;
27 onChange?: (event: any) => void;
28 onTouchesDown?: TouchEventHandlerType;
29 onTouchesMove?: TouchEventHandlerType;
30 onTouchesUp?: TouchEventHandlerType;
31 onTouchesCancelled?: TouchEventHandlerType;
32 changeEventCalculator?: (current: GestureUpdateEvent<Record<string, unknown>>, previous?: GestureUpdateEvent<Record<string, unknown>>) => GestureUpdateEvent<Record<string, unknown>>;
33 isWorklet: boolean[];
34};
35export declare const CALLBACK_TYPE: {
36 readonly UNDEFINED: 0;
37 readonly BEGAN: 1;
38 readonly START: 2;
39 readonly UPDATE: 3;
40 readonly CHANGE: 4;
41 readonly END: 5;
42 readonly FINALIZE: 6;
43 readonly TOUCHES_DOWN: 7;
44 readonly TOUCHES_MOVE: 8;
45 readonly TOUCHES_UP: 9;
46 readonly TOUCHES_CANCELLED: 10;
47};
48export type CALLBACK_TYPE = typeof CALLBACK_TYPE[keyof typeof CALLBACK_TYPE];
49export declare abstract class Gesture {
50 /**
51 * Return array of gestures, providing the same interface for creating and updating
52 * handlers, no matter which object was used to create gesture instance.
53 */
54 abstract toGestureArray(): GestureType[];
55 /**
56 * Assign handlerTag to the gesture instance and set ref.current (if a ref is set)
57 */
58 abstract initialize(): void;
59 /**
60 * Make sure that values of properties defining relations are arrays. Do any necessary
61 * preprocessing required to configure relations between handlers. Called just before
62 * updating the handler on the native side.
63 */
64 abstract prepare(): void;
65}
66export declare abstract class BaseGesture<EventPayloadT extends Record<string, unknown>> extends Gesture {
67 private gestureId;
68 handlerTag: number;
69 handlerName: string;
70 config: BaseGestureConfig;
71 handlers: HandlerCallbacks<EventPayloadT>;
72 constructor();
73 private addDependency;
74 /**
75 * Sets a `ref` to the gesture object, allowing for interoperability with the old API.
76 * @param ref
77 */
78 withRef(ref: React.MutableRefObject<GestureType | undefined>): this;
79 protected isWorklet(callback: Function): boolean;
80 /**
81 * Set the callback that is being called when given gesture handler starts receiving touches.
82 * At the moment of this callback the handler is in `BEGAN` state and we don't know yet if it will recognize the gesture at all.
83 * @param callback
84 */
85 onBegin(callback: (event: GestureStateChangeEvent<EventPayloadT>) => void): this;
86 /**
87 * Set the callback that is being called when the gesture is recognized by the handler and it transitions to the `ACTIVE` state.
88 * @param callback
89 */
90 onStart(callback: (event: GestureStateChangeEvent<EventPayloadT>) => void): this;
91 /**
92 * Set the callback that is being called when the gesture that was recognized by the handler finishes and handler reaches `END` state.
93 * It will be called only if the handler was previously in the `ACTIVE` state.
94 * @param callback
95 */
96 onEnd(callback: (event: GestureStateChangeEvent<EventPayloadT>, success: boolean) => void): this;
97 /**
98 * Set the callback that is being called when the handler finalizes handling gesture - the gesture was recognized and has finished or it failed to recognize.
99 * @param callback
100 */
101 onFinalize(callback: (event: GestureStateChangeEvent<EventPayloadT>, success: boolean) => void): this;
102 /**
103 * Set the `onTouchesDown` callback which is called every time a pointer is placed on the screen.
104 * @param callback
105 */
106 onTouchesDown(callback: TouchEventHandlerType): this;
107 /**
108 * Set the `onTouchesMove` callback which is called every time a pointer is moved on the screen.
109 * @param callback
110 */
111 onTouchesMove(callback: TouchEventHandlerType): this;
112 /**
113 * Set the `onTouchesUp` callback which is called every time a pointer is lifted from the screen.
114 * @param callback
115 */
116 onTouchesUp(callback: TouchEventHandlerType): this;
117 /**
118 * Set the `onTouchesCancelled` callback which is called every time a pointer stops being tracked, for example when the gesture finishes.
119 * @param callback
120 */
121 onTouchesCancelled(callback: TouchEventHandlerType): this;
122 /**
123 * Indicates whether the given handler should be analyzing stream of touch events or not.
124 * @param enabled
125 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#enabledvalue-boolean
126 */
127 enabled(enabled: boolean): this;
128 /**
129 * When true the handler will cancel or fail recognition (depending on its current state) whenever the finger leaves the area of the connected view.
130 * @param value
131 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#shouldcancelwhenoutsidevalue-boolean
132 */
133 shouldCancelWhenOutside(value: boolean): this;
134 /**
135 * This parameter enables control over what part of the connected view area can be used to begin recognizing the gesture.
136 * When a negative number is provided the bounds of the view will reduce the area by the given number of points in each of the sides evenly.
137 * @param hitSlop
138 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#hitslopsettings
139 */
140 hitSlop(hitSlop: HitSlop): this;
141 /**
142 * #### Web only
143 * This parameter allows to specify which `cursor` should be used when gesture activates.
144 * Supports all CSS cursor values (e.g. `"grab"`, `"zoom-in"`). Default value is set to `"auto"`.
145 * @param activeCursor
146 */
147 activeCursor(activeCursor: ActiveCursor): this;
148 /**
149 * #### Web & Android only
150 * Allows users to choose which mouse button should handler respond to.
151 * Arguments can be combined using `|` operator, e.g. `mouseButton(MouseButton.LEFT | MouseButton.RIGHT)`.
152 * Default value is set to `MouseButton.LEFT`.
153 * @param mouseButton
154 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#mousebuttonvalue-mousebutton-web--android-only
155 */
156 mouseButton(mouseButton: MouseButton): this;
157 /**
158 * When `react-native-reanimated` is installed, the callbacks passed to the gestures are automatically workletized and run on the UI thread when called.
159 * This option allows for changing this behavior: when `true`, all the callbacks will be run on the JS thread instead of the UI thread, regardless of whether they are worklets or not.
160 * Defaults to `false`.
161 * @param runOnJS
162 */
163 runOnJS(runOnJS: boolean): this;
164 /**
165 * Allows gestures across different components to be recognized simultaneously.
166 * @param gestures
167 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/gesture-composition/#simultaneouswithexternalgesture
168 */
169 simultaneousWithExternalGesture(...gestures: Exclude<GestureRef, number>[]): this;
170 /**
171 * Allows to delay activation of the handler until all handlers passed as arguments to this method fail (or don't begin at all).
172 * @param gestures
173 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/gesture-composition/#requireexternalgesturetofail
174 */
175 requireExternalGestureToFail(...gestures: Exclude<GestureRef, number>[]): this;
176 /**
177 * Works similarily to `requireExternalGestureToFail` but the direction of the relation is reversed - instead of being one-to-many relation, it's many-to-one.
178 * @param gestures
179 * @see https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/gesture-composition/#blocksexternalgesture
180 */
181 blocksExternalGesture(...gestures: Exclude<GestureRef, number>[]): this;
182 /**
183 * Sets a `testID` property for gesture object, allowing for querying for it in tests.
184 * @param id
185 */
186 withTestId(id: string): this;
187 /**
188 * #### iOS only
189 * When `true`, the handler will cancel touches for native UI components (`UIButton`, `UISwitch`, etc) it's attached to when it becomes `ACTIVE`.
190 * Default value is `true`.
191 * @param value
192 */
193 cancelsTouchesInView(value: boolean): this;
194 initialize(): void;
195 toGestureArray(): GestureType[];
196 prepare(): void;
197 get shouldUseReanimated(): boolean;
198}
199export declare abstract class ContinousBaseGesture<EventPayloadT extends Record<string, unknown>, EventChangePayloadT extends Record<string, unknown>> extends BaseGesture<EventPayloadT> {
200 /**
201 * Set the callback that is being called every time the gesture receives an update while it's active.
202 * @param callback
203 */
204 onUpdate(callback: (event: GestureUpdateEvent<EventPayloadT>) => void): this;
205 /**
206 * Set the callback that is being called every time the gesture receives an update while it's active.
207 * This callback will receive information about change in value in relation to the last received event.
208 * @param callback
209 */
210 onChange(callback: (event: GestureUpdateEvent<EventPayloadT & EventChangePayloadT>) => void): this;
211 /**
212 * When `true` the handler will not activate by itself even if its activation criteria are met.
213 * Instead you can manipulate its state using state manager.
214 * @param manualActivation
215 */
216 manualActivation(manualActivation: boolean): this;
217}
218export {};