Skip to content

Commit a09df73

Browse files
committed
Move examples to separate files
1 parent 4fd4d8c commit a09df73

7 files changed

Lines changed: 322 additions & 322 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { View, StyleSheet } from 'react-native';
2+
import {
3+
GestureDetector,
4+
usePanGesture,
5+
useLongPressGesture,
6+
GestureHandlerRootView,
7+
useCompetingGestures,
8+
} from 'react-native-gesture-handler';
9+
10+
export default function App() {
11+
const panGesture = usePanGesture({
12+
onUpdate: () => {
13+
console.log('Pan');
14+
},
15+
});
16+
const longPressGesture = useLongPressGesture({
17+
onDeactivate: (_, success) => {
18+
if (success) {
19+
console.log('Long Press');
20+
}
21+
},
22+
});
23+
24+
const gesture = useCompetingGestures(panGesture, longPressGesture);
25+
26+
return (
27+
<GestureHandlerRootView style={styles.container}>
28+
<GestureDetector gesture={gesture}>
29+
<View style={styles.box} />
30+
</GestureDetector>
31+
</GestureHandlerRootView>
32+
);
33+
}
34+
35+
const styles = StyleSheet.create({
36+
container: {
37+
flex: 1,
38+
alignItems: 'center',
39+
justifyContent: 'space-around',
40+
},
41+
box: {
42+
height: 120,
43+
width: 120,
44+
backgroundColor: '#b58df1',
45+
borderRadius: 20,
46+
marginBottom: 30,
47+
},
48+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { StyleSheet, View } from 'react-native';
2+
import {
3+
GestureDetector,
4+
GestureHandlerRootView,
5+
useTapGesture,
6+
useExclusiveGestures,
7+
} from 'react-native-gesture-handler';
8+
9+
export default function App() {
10+
const singleTap = useTapGesture({
11+
onDeactivate: (_, success) => {
12+
if (success) {
13+
console.log('Single tap!');
14+
}
15+
},
16+
});
17+
18+
const doubleTap = useTapGesture({
19+
numberOfTaps: 2,
20+
onDeactivate: (_, success) => {
21+
if (success) {
22+
console.log('Double tap!');
23+
}
24+
},
25+
});
26+
27+
const taps = useExclusiveGestures(doubleTap, singleTap);
28+
29+
return (
30+
<GestureHandlerRootView style={styles.container}>
31+
<GestureDetector gesture={taps}>
32+
<View style={styles.box} />
33+
</GestureDetector>
34+
</GestureHandlerRootView>
35+
);
36+
}
37+
38+
const styles = StyleSheet.create({
39+
container: {
40+
flex: 1,
41+
alignItems: 'center',
42+
justifyContent: 'space-around',
43+
},
44+
box: {
45+
width: 100,
46+
height: 100,
47+
backgroundColor: 'plum',
48+
},
49+
});

packages/docs-gesture-handler/docs/fundamentals/_examples/simultaneous.tsx renamed to packages/docs-gesture-handler/docs/fundamentals/_examples/hooks/Simultaneous.tsx

File renamed without changes.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import React, { useState } from 'react';
2+
import { StyleSheet } from 'react-native';
3+
import {
4+
GestureDetector,
5+
GestureHandlerRootView,
6+
ScrollView,
7+
NativeGesture,
8+
usePinchGesture,
9+
} from 'react-native-gesture-handler';
10+
import Animated, {
11+
useSharedValue,
12+
useAnimatedStyle,
13+
withTiming,
14+
} from 'react-native-reanimated';
15+
16+
const ITEMS = ['red', 'green', 'blue', 'yellow'];
17+
18+
type ItemProps = {
19+
backgroundColor: string;
20+
scrollGesture: NativeGesture | null;
21+
};
22+
23+
function Item({ backgroundColor, scrollGesture }: ItemProps) {
24+
const scale = useSharedValue(1);
25+
const zIndex = useSharedValue(1);
26+
27+
const pinch = usePinchGesture({
28+
onBegin: () => {
29+
zIndex.value = 100;
30+
},
31+
onUpdate: (e) => {
32+
scale.value *= e.scaleChange;
33+
},
34+
onFinalize: () => {
35+
scale.value = withTiming(1, undefined, (finished) => {
36+
if (finished) {
37+
zIndex.value = 1;
38+
}
39+
});
40+
},
41+
block: scrollGesture ?? undefined,
42+
});
43+
44+
const animatedStyles = useAnimatedStyle(() => ({
45+
transform: [{ scale: scale.value }],
46+
zIndex: zIndex.value,
47+
}));
48+
49+
return (
50+
<GestureDetector gesture={pinch}>
51+
<Animated.View
52+
style={[
53+
{ backgroundColor: backgroundColor },
54+
styles.item,
55+
animatedStyles,
56+
]}
57+
/>
58+
</GestureDetector>
59+
);
60+
}
61+
62+
export default function App() {
63+
const [scrollGesture, setScrollGesture] = useState<NativeGesture | null>(
64+
null
65+
);
66+
67+
return (
68+
<GestureHandlerRootView style={styles.container}>
69+
<ScrollView
70+
style={styles.container}
71+
onGestureUpdate_CAN_CAUSE_INFINITE_RERENDER={(gesture) => {
72+
if (!scrollGesture || scrollGesture.tag !== gesture.tag) {
73+
setScrollGesture(gesture);
74+
}
75+
}}>
76+
{ITEMS.map((item) => (
77+
<Item
78+
backgroundColor={item}
79+
key={item}
80+
scrollGesture={scrollGesture}
81+
/>
82+
))}
83+
</ScrollView>
84+
</GestureHandlerRootView>
85+
);
86+
}
87+
88+
const styles = StyleSheet.create({
89+
container: {
90+
flex: 1,
91+
},
92+
item: {
93+
flex: 1,
94+
aspectRatio: 1,
95+
},
96+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { View, StyleSheet } from 'react-native';
3+
import {
4+
GestureDetector,
5+
GestureHandlerRootView,
6+
useTapGesture,
7+
} from 'react-native-gesture-handler';
8+
9+
export default function App() {
10+
const innerTap = useTapGesture({
11+
numberOfTaps: 2,
12+
onDeactivate: (_, success) => {
13+
if (success) {
14+
console.log('inner tap');
15+
}
16+
},
17+
});
18+
19+
const outerTap = useTapGesture({
20+
onDeactivate: (_, success) => {
21+
if (success) {
22+
console.log('outer tap');
23+
}
24+
},
25+
requireToFail: innerTap,
26+
});
27+
28+
return (
29+
<GestureHandlerRootView style={styles.container}>
30+
<GestureDetector gesture={outerTap}>
31+
<View style={styles.outer}>
32+
<GestureDetector gesture={innerTap}>
33+
<View style={styles.inner} />
34+
</GestureDetector>
35+
</View>
36+
</GestureDetector>
37+
</GestureHandlerRootView>
38+
);
39+
}
40+
41+
const styles = StyleSheet.create({
42+
container: {
43+
flex: 1,
44+
alignItems: 'center',
45+
justifyContent: 'center',
46+
},
47+
outer: {
48+
width: 250,
49+
height: 250,
50+
backgroundColor: 'lightblue',
51+
},
52+
inner: {
53+
width: 100,
54+
height: 100,
55+
backgroundColor: 'blue',
56+
alignSelf: 'center',
57+
},
58+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import { View, StyleSheet } from 'react-native';
3+
import {
4+
GestureDetector,
5+
GestureHandlerRootView,
6+
useTapGesture,
7+
} from 'react-native-gesture-handler';
8+
9+
export default function App() {
10+
const innerTap = useTapGesture({
11+
onDeactivate: (_, success) => {
12+
if (success) {
13+
console.log('inner tap');
14+
}
15+
},
16+
});
17+
18+
const outerTap = useTapGesture({
19+
onDeactivate: (_, success) => {
20+
if (success) {
21+
console.log('outer tap');
22+
}
23+
},
24+
simultaneousWith: innerTap,
25+
});
26+
27+
return (
28+
<GestureHandlerRootView style={styles.container}>
29+
<GestureDetector gesture={outerTap}>
30+
<View style={styles.outer}>
31+
<GestureDetector gesture={innerTap}>
32+
<View style={styles.inner} />
33+
</GestureDetector>
34+
</View>
35+
</GestureDetector>
36+
</GestureHandlerRootView>
37+
);
38+
}
39+
40+
const styles = StyleSheet.create({
41+
container: {
42+
flex: 1,
43+
alignItems: 'center',
44+
justifyContent: 'center',
45+
},
46+
outer: {
47+
width: 250,
48+
height: 250,
49+
backgroundColor: 'lightblue',
50+
},
51+
inner: {
52+
width: 100,
53+
height: 100,
54+
backgroundColor: 'blue',
55+
alignSelf: 'center',
56+
},
57+
});

0 commit comments

Comments
 (0)