Skip to content

Commit af2b3bf

Browse files
committed
Interaction with Reanimated
1 parent d7e38d4 commit af2b3bf

4 files changed

Lines changed: 121 additions & 7 deletions

File tree

packages/docs-gesture-handler/docs/fundamentals/gesture-composition.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: gesture-composition
33
title: Gesture composition & interactions
44
sidebar_label: Gesture composition & interactions
5-
sidebar_position: 4
5+
sidebar_position: 5
66
---
77

88
RNGH3 simplifies gesture interaction through dedicated composition hooks and configuration properties. To choose the right approach, simply ask: Are all the gestures attached to the same component?

packages/docs-gesture-handler/docs/fundamentals/gesture-detector.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar_position: 3
77

88
## Gesture Detector
99

10-
The `GestureDetector` is a key component of RNGH3. It supports gestures created either using the hooks API or the builder pattern. Additionally, it allows for the recognition of multiple gestures through [gesture composition](/docs/fundamentals/gesture-composition).
10+
The `GestureDetector` is a key component of RNGH3. It supports gestures created either using the hooks API or the builder pattern. Additionally, it allows for the recognition of multiple gestures through [gesture composition](/docs/fundamentals/gesture-composition). `GestureDetector` interacts closely with [`Reanimated`](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/getting-started/). For more details, refer to the [Integration with Reanimated](/docs/fundamentals/reanimated-interactions) section.
1111

1212
When using hook API, you can also integrate it directly with the [Animated API](https://reactnative.dev/docs/animated).
1313

@@ -160,10 +160,6 @@ const styles = StyleSheet.create({
160160
});
161161
```
162162

163-
## Interaction with Reanimated
164-
165-
`GestureDetector` will decide whether to use [Reanimated](https://docs.swmansion.com/react-native-reanimated/) to process provided gestures based on their configuration. If any of the callbacks is a worklet and Reanimated is not explicitly turned off, tools provided by the Reanimated will be utilized bringing ability to handle gestures synchronously.
166-
167163
## Properties
168164

169165
### gesture
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
id: reanimated-interactions
3+
title: Integration with Reanimated
4+
sidebar_label: Integration with Reanimated
5+
sidebar_position: 4
6+
---
7+
8+
import CollapsibleCode from '@site/src/components/CollapsibleCode';
9+
10+
`GestureDetector` will decide whether to use [Reanimated](https://docs.swmansion.com/react-native-reanimated/) to process provided gestures based on their configuration. If any of the callbacks is a [worklet](<(https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary/#worklet)>) and Reanimated is not explicitly turned off, tools provided by the Reanimated will be utilized bringing ability to handle gestures synchronously.
11+
12+
## Automatic [workletization](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary/#to-workletize) of gesture callbacks
13+
14+
[Worklets' Babel plugin](https://docs.swmansion.com/react-native-worklets/docs/worklets-babel-plugin/about) is setup in a way that automatically marks callbacks passed to gestures in the configuration chain as worklets. This means that you don't need to add a `'worklet';` directive at the beginning of the functions. Here is an example that will be automatically workletized:
15+
16+
```jsx
17+
const gesture = useTapGesture({
18+
onBegin: () => {
19+
console.log(_WORKLET);
20+
},
21+
});
22+
```
23+
24+
And here is one that won't:
25+
26+
```jsx
27+
const callback = () => {
28+
console.log(_WORKLET);
29+
};
30+
31+
const gesture = useTapGesture({
32+
onBegin: callback,
33+
});
34+
```
35+
36+
It also won't work when wrapped with hooks like `useCallback` or `useMemo`, e.g:
37+
38+
```jsx
39+
const callback = useCallback(() => {
40+
console.log(_WORKLET);
41+
}, []);
42+
43+
const gesture = useTapGesture({
44+
onBegin: callback,
45+
});
46+
```
47+
48+
In the above cases, you should add a `"worklet";` directive at the beginning of the callbacks, like so:
49+
50+
```jsx
51+
const callback = () => {
52+
// highlight-next-line
53+
'worklet';
54+
console.log(_WORKLET);
55+
};
56+
57+
const gesture = useTapGesture({
58+
onBegin: callback,
59+
});
60+
```
61+
62+
```jsx
63+
const callback = useCallback(() => {
64+
// highlight-next-line
65+
'worklet;';
66+
console.log(_WORKLET);
67+
}, []);
68+
69+
const gesture = useTapGesture({
70+
onBegin: callback,
71+
});
72+
```
73+
74+
## Using SharedValue in gesture config
75+
76+
RNGH3 allows to pass [`SharedValue`](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#shared-value) to gestures' configurations. This allows to react to configuration changes without unnecessary rerenders.
77+
78+
<CollapsibleCode
79+
label="Show full example"
80+
expandedLabel="Hide full example"
81+
lineBounds={[10, 19]}
82+
src={`
83+
import * as React from 'react';
84+
import { Animated } from 'react-native';
85+
import {
86+
GestureHandlerRootView,
87+
GestureDetector,
88+
useTapGesture,
89+
} from 'react-native-gesture-handler';
90+
import { useSharedValue } from 'react-native-reanimated';
91+
92+
export default function App() {
93+
// highlight-next-line
94+
const taps = useSharedValue(2);
95+
96+
const gesture = useTapGesture({
97+
// highlight-next-line
98+
numberOfTaps: taps,
99+
onDeactivate: () => {
100+
taps.value += 1;
101+
},
102+
});
103+
104+
return (
105+
<GestureHandlerRootView>
106+
<GestureDetector gesture={gesture}>
107+
<Animated.View
108+
style={{
109+
width: 150,
110+
height: 150,
111+
backgroundColor: 'blue',
112+
}}
113+
/>
114+
</GestureDetector>
115+
</GestureHandlerRootView>
116+
);
117+
}
118+
`}/>

packages/docs-gesture-handler/docs/fundamentals/states-events.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: states-events
33
title: Gesture states & events
44
sidebar_label: Gesture states & events
5-
sidebar_position: 5
5+
sidebar_position: 6
66
---
77

88
Every gesture can be treated as ["state machine"](https://en.wikipedia.org/wiki/Finite-state_machine).

0 commit comments

Comments
 (0)