Skip to content

Commit 15c65fb

Browse files
authored
Add TypeDoc to new API (#2874)
## Description I thought that adding TypeDocs might be a good idea and it could enhance developer experience (I've come up with this idea when I discovered that we have `withRef` modifier). This PR adds TypeDocs to new API (but we can also think of filling missing docs in old API) ## Test plan Hover functions/components and read docs.
1 parent d20ee86 commit 15c65fb

10 files changed

Lines changed: 350 additions & 1 deletion

src/handlers/gestures/GestureDetector.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,32 @@ const applyTouchActionProp = (
630630
};
631631

632632
interface GestureDetectorProps {
633+
/**
634+
* A gesture object containing the configuration and callbacks.
635+
* Can be any of:
636+
* - base gestures (`Tap`, `Pan`, ...)
637+
* - `ComposedGesture` (`Race`, `Simultaneous`, `Exclusive`)
638+
*/
633639
gesture: ComposedGesture | GestureType;
634640
children?: React.ReactNode;
641+
642+
/**
643+
* #### Web only
644+
* This parameter allows to specify which `userSelect` property should be applied to underlying view.
645+
* Possible values are `"none" | "auto" | "text"`. Default value is set to `"none"`.
646+
*/
635647
userSelect?: UserSelect;
648+
/**
649+
* #### Web only
650+
* Specifies whether context menu should be enabled after clicking on underlying view with right mouse button.
651+
* Default value is set to `false`.
652+
*/
636653
enableContextMenu?: boolean;
654+
/**
655+
* #### Web only
656+
* This parameter allows to specify which `touchAction` property should be applied to underlying view.
657+
* Supports all CSS touch-action values (e.g. `"none"`, `"pan-y"`). Default value is set to `"none"`.
658+
*/
637659
touchAction?: TouchAction;
638660
}
639661
interface GestureDetectorState {
@@ -642,6 +664,22 @@ interface GestureDetectorState {
642664
previousViewTag: number;
643665
forceReattach: boolean;
644666
}
667+
668+
/**
669+
* `GestureDetector` is responsible for creating and updating native gesture handlers based on the config of provided gesture.
670+
*
671+
* ### Props
672+
* - `gesture`
673+
* - `userSelect` (**Web only**)
674+
* - `enableContextMenu` (**Web only**)
675+
* - `touchAction` (**Web only**)
676+
*
677+
* ### Remarks
678+
* - Gesture Detector will use first native view in its subtree to recognize gestures, however if this view is used only to group its children it may get automatically collapsed.
679+
* - Using the same instance of a gesture across multiple Gesture Detectors is not possible.
680+
*
681+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/gesture-detector
682+
*/
645683
export const GestureDetector = (props: GestureDetectorProps) => {
646684
const rootViewContext = useContext(GestureHandlerRootViewContext);
647685
if (__DEV__ && !rootViewContext && !isJestEnv() && Platform.OS !== 'web') {

src/handlers/gestures/flingGesture.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ export class FlingGesture extends BaseGesture<FlingGestureHandlerEventPayload> {
1313
this.handlerName = 'FlingGestureHandler';
1414
}
1515

16+
/**
17+
* Determine exact number of points required to handle the fling gesture.
18+
* @param pointers
19+
*/
1620
numberOfPointers(pointers: number) {
1721
this.config.numberOfPointers = pointers;
1822
return this;
1923
}
2024

25+
/**
26+
* Expressed allowed direction of movement.
27+
* Expected values are exported as constants in the Directions object.
28+
* Arguments can be combined using `|` operator. Default value is set to `MouseButton.LEFT`.
29+
* @param direction
30+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/fling-gesture/#directionvalue-directions
31+
*/
2132
direction(direction: number) {
2233
this.config.direction = direction;
2334
return this;

src/handlers/gestures/forceTouchGesture.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,30 @@ export class ForceTouchGesture extends ContinousBaseGesture<
4040
this.handlerName = 'ForceTouchGestureHandler';
4141
}
4242

43+
/**
44+
* A minimal pressure that is required before gesture can activate.
45+
* Should be a value from range [0.0, 1.0]. Default is 0.2.
46+
* @param force
47+
*/
4348
minForce(force: number) {
4449
this.config.minForce = force;
4550
return this;
4651
}
4752

53+
/**
54+
* A maximal pressure that could be applied for gesture.
55+
* If the pressure is greater, gesture fails. Should be a value from range [0.0, 1.0].
56+
* @param force
57+
*/
4858
maxForce(force: number) {
4959
this.config.maxForce = force;
5060
return this;
5161
}
5262

63+
/**
64+
* Value defining if haptic feedback has to be performed on activation.
65+
* @param value
66+
*/
5367
feedbackOnActivation(value: boolean) {
5468
this.config.feedbackOnActivation = value;
5569
return this;

src/handlers/gestures/gesture.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ export abstract class BaseGesture<
155155
: [gesture];
156156
}
157157

158+
/**
159+
* Sets a `ref` to the gesture object, allowing for interoperability with the old API.
160+
* @param ref
161+
*/
158162
withRef(ref: React.MutableRefObject<GestureType | undefined>) {
159163
this.config.ref = ref;
160164
return this;
@@ -166,18 +170,32 @@ export abstract class BaseGesture<
166170
return callback.__workletHash !== undefined;
167171
}
168172

173+
/**
174+
* Set the callback that is being called when given gesture handler starts receiving touches.
175+
* 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.
176+
* @param callback
177+
*/
169178
onBegin(callback: (event: GestureStateChangeEvent<EventPayloadT>) => void) {
170179
this.handlers.onBegin = callback;
171180
this.handlers.isWorklet[CALLBACK_TYPE.BEGAN] = this.isWorklet(callback);
172181
return this;
173182
}
174183

184+
/**
185+
* Set the callback that is being called when the gesture is recognized by the handler and it transitions to the `ACTIVE` state.
186+
* @param callback
187+
*/
175188
onStart(callback: (event: GestureStateChangeEvent<EventPayloadT>) => void) {
176189
this.handlers.onStart = callback;
177190
this.handlers.isWorklet[CALLBACK_TYPE.START] = this.isWorklet(callback);
178191
return this;
179192
}
180193

194+
/**
195+
* Set the callback that is being called when the gesture that was recognized by the handler finishes and handler reaches `END` state.
196+
* It will be called only if the handler was previously in the `ACTIVE` state.
197+
* @param callback
198+
*/
181199
onEnd(
182200
callback: (
183201
event: GestureStateChangeEvent<EventPayloadT>,
@@ -190,6 +208,10 @@ export abstract class BaseGesture<
190208
return this;
191209
}
192210

211+
/**
212+
* 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.
213+
* @param callback
214+
*/
193215
onFinalize(
194216
callback: (
195217
event: GestureStateChangeEvent<EventPayloadT>,
@@ -202,6 +224,10 @@ export abstract class BaseGesture<
202224
return this;
203225
}
204226

227+
/**
228+
* Set the `onTouchesDown` callback which is called every time a pointer is placed on the screen.
229+
* @param callback
230+
*/
205231
onTouchesDown(callback: TouchEventHandlerType) {
206232
this.config.needsPointerData = true;
207233
this.handlers.onTouchesDown = callback;
@@ -211,6 +237,10 @@ export abstract class BaseGesture<
211237
return this;
212238
}
213239

240+
/**
241+
* Set the `onTouchesMove` callback which is called every time a pointer is moved on the screen.
242+
* @param callback
243+
*/
214244
onTouchesMove(callback: TouchEventHandlerType) {
215245
this.config.needsPointerData = true;
216246
this.handlers.onTouchesMove = callback;
@@ -220,6 +250,10 @@ export abstract class BaseGesture<
220250
return this;
221251
}
222252

253+
/**
254+
* Set the `onTouchesUp` callback which is called every time a pointer is lifted from the screen.
255+
* @param callback
256+
*/
223257
onTouchesUp(callback: TouchEventHandlerType) {
224258
this.config.needsPointerData = true;
225259
this.handlers.onTouchesUp = callback;
@@ -229,6 +263,10 @@ export abstract class BaseGesture<
229263
return this;
230264
}
231265

266+
/**
267+
* Set the `onTouchesCancelled` callback which is called every time a pointer stops being tracked, for example when the gesture finishes.
268+
* @param callback
269+
*/
232270
onTouchesCancelled(callback: TouchEventHandlerType) {
233271
this.config.needsPointerData = true;
234272
this.handlers.onTouchesCancelled = callback;
@@ -238,62 +276,123 @@ export abstract class BaseGesture<
238276
return this;
239277
}
240278

279+
/**
280+
* Indicates whether the given handler should be analyzing stream of touch events or not.
281+
* @param enabled
282+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#enabledvalue-boolean
283+
*/
241284
enabled(enabled: boolean) {
242285
this.config.enabled = enabled;
243286
return this;
244287
}
245288

289+
/**
290+
* When true the handler will cancel or fail recognition (depending on its current state) whenever the finger leaves the area of the connected view.
291+
* @param value
292+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#shouldcancelwhenoutsidevalue-boolean
293+
*/
246294
shouldCancelWhenOutside(value: boolean) {
247295
this.config.shouldCancelWhenOutside = value;
248296
return this;
249297
}
250298

299+
/**
300+
* This parameter enables control over what part of the connected view area can be used to begin recognizing the gesture.
301+
* 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.
302+
* @param hitSlop
303+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#hitslopsettings
304+
*/
251305
hitSlop(hitSlop: HitSlop) {
252306
this.config.hitSlop = hitSlop;
253307
return this;
254308
}
255309

310+
/**
311+
* #### Web only
312+
* This parameter allows to specify which `cursor` should be used when gesture activates.
313+
* Supports all CSS cursor values (e.g. `"grab"`, `"zoom-in"`). Default value is set to `"auto"`.
314+
* @param activeCursor
315+
*/
256316
activeCursor(activeCursor: ActiveCursor) {
257317
this.config.activeCursor = activeCursor;
258318
return this;
259319
}
260320

321+
/**
322+
* #### Web & Android only
323+
* Allows users to choose which mouse button should handler respond to.
324+
* Arguments can be combined using `|` operator, e.g. `mouseButton(MouseButton.LEFT | MouseButton.RIGHT)`.
325+
* Default value is set to `MouseButton.LEFT`.
326+
* @param mouseButton
327+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/pan-gesture#mousebuttonvalue-mousebutton-web--android-only
328+
*/
261329
mouseButton(mouseButton: MouseButton) {
262330
this.config.mouseButton = mouseButton;
263331
return this;
264332
}
265333

334+
/**
335+
* When `react-native-reanimated` is installed, the callbacks passed to the gestures are automatically workletized and run on the UI thread when called.
336+
* 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.
337+
* Defaults to `false`.
338+
* @param runOnJS
339+
*/
266340
runOnJS(runOnJS: boolean) {
267341
this.config.runOnJS = runOnJS;
268342
return this;
269343
}
270344

345+
/**
346+
* Allows gestures across different components to be recognized simultaneously.
347+
* @param gestures
348+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/gesture-composition/#simultaneouswithexternalgesture
349+
*/
271350
simultaneousWithExternalGesture(...gestures: Exclude<GestureRef, number>[]) {
272351
for (const gesture of gestures) {
273352
this.addDependency('simultaneousWith', gesture);
274353
}
275354
return this;
276355
}
277356

357+
/**
358+
* Allows to delay activation of the handler until all handlers passed as arguments to this method fail (or don't begin at all).
359+
* @param gestures
360+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/gesture-composition/#requireexternalgesturetofail
361+
*/
278362
requireExternalGestureToFail(...gestures: Exclude<GestureRef, number>[]) {
279363
for (const gesture of gestures) {
280364
this.addDependency('requireToFail', gesture);
281365
}
282366
return this;
283367
}
284368

369+
/**
370+
* Works similarily to `requireExternalGestureToFail` but the direction of the relation is reversed - instead of being one-to-many relation, it's many-to-one.
371+
* @param gestures
372+
* @see https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/gesture-composition/#blocksexternalgesture
373+
*/
285374
blocksExternalGesture(...gestures: Exclude<GestureRef, number>[]) {
286375
for (const gesture of gestures) {
287376
this.addDependency('blocksHandlers', gesture);
288377
}
289378
return this;
290379
}
291380

381+
/**
382+
* Sets a `testID` property for gesture object, allowing for querying for it in tests.
383+
* @param id
384+
*/
292385
withTestId(id: string) {
293386
this.config.testId = id;
294387
return this;
295388
}
296389

390+
/**
391+
* #### iOS only
392+
* When `true`, the handler will cancel touches for native UI components (`UIButton`, `UISwitch`, etc) it's attached to when it becomes `ACTIVE`.
393+
* Default value is `true`.
394+
* @param value
395+
*/
297396
cancelsTouchesInView(value: boolean) {
298397
this.config.cancelsTouchesInView = value;
299398
return this;
@@ -332,12 +431,21 @@ export abstract class ContinousBaseGesture<
332431
EventPayloadT extends Record<string, unknown>,
333432
EventChangePayloadT extends Record<string, unknown>
334433
> extends BaseGesture<EventPayloadT> {
434+
/**
435+
* Set the callback that is being called every time the gesture receives an update while it's active.
436+
* @param callback
437+
*/
335438
onUpdate(callback: (event: GestureUpdateEvent<EventPayloadT>) => void) {
336439
this.handlers.onUpdate = callback;
337440
this.handlers.isWorklet[CALLBACK_TYPE.UPDATE] = this.isWorklet(callback);
338441
return this;
339442
}
340443

444+
/**
445+
* Set the callback that is being called every time the gesture receives an update while it's active.
446+
* This callback will receive information about change in value in relation to the last received event.
447+
* @param callback
448+
*/
341449
onChange(
342450
callback: (
343451
event: GestureUpdateEvent<EventPayloadT & EventChangePayloadT>
@@ -348,6 +456,11 @@ export abstract class ContinousBaseGesture<
348456
return this;
349457
}
350458

459+
/**
460+
* When `true` the handler will not activate by itself even if its activation criteria are met.
461+
* Instead you can manipulate its state using state manager.
462+
* @param manualActivation
463+
*/
351464
manualActivation(manualActivation: boolean) {
352465
this.config.manualActivation = manualActivation;
353466
return this;

0 commit comments

Comments
 (0)