-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathindex.tsx
More file actions
164 lines (146 loc) · 4.52 KB
/
index.tsx
File metadata and controls
164 lines (146 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { useEffect, useMemo, useReducer } from 'react';
import { StyleSheet, View, useWindowDimensions, Text } from 'react-native';
import Animated, {
useAnimatedStyle,
useSharedValue,
withSpring,
withTiming,
} from 'react-native-reanimated';
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
} from 'react-native-gesture-handler';
import ChartManager from './ChartManager';
import FlowChart from './FlowChart';
// widths pulled from CSS
const MIN_DESKTOP_WIDTH = 1298;
export default function App() {
const [, forceUpdate] = useReducer((x: number) => x + 1, 0);
const chartManager = useMemo(() => new ChartManager(), []);
const [panHandle, capturedPan, resetPan] = useMemo(
() => chartManager.newGesture(Gesture.Pan()),
[chartManager]
);
const [pressHandle, capturedPress, resetLongPress] = useMemo(
() => chartManager.newGesture(Gesture.LongPress()),
[chartManager]
);
const dimensions = useWindowDimensions();
const isDesktopMode = dimensions.width > MIN_DESKTOP_WIDTH;
useEffect(() => {
// Timing issue, neither useEffect, useLayoutEffect or requestAnimationFrame work
const timeout = setTimeout(() => {
resetPan();
resetLongPress();
}, 300);
return () => {
clearTimeout(timeout);
};
}, [resetLongPress, resetPan]);
useEffect(() => {
const panIds = panHandle.idObject;
const pressIds = pressHandle.idObject;
// prettier-ignore
const desktopLayout = [
[panIds.undetermined, ChartManager.EMPTY_SPACE_ID, pressIds.undetermined, ChartManager.EMPTY_SPACE_ID],
[panIds.began, panIds.failed, pressIds.began, pressIds.failed],
[panIds.active, panIds.cancelled, pressIds.active, pressIds.cancelled],
[panIds.end, ChartManager.EMPTY_SPACE_ID, pressIds.end, ChartManager.EMPTY_SPACE_ID],
];
// prettier-ignore
const phoneLayout = [
[panIds.undetermined],
[panIds.began, panIds.failed],
[panIds.active, panIds.cancelled],
[panIds.end, ChartManager.EMPTY_SPACE_ID],
];
chartManager.layout = isDesktopMode ? desktopLayout : phoneLayout;
forceUpdate();
}, [chartManager, isDesktopMode, panHandle, pressHandle]);
const pressed = useSharedValue(false);
const offset = useSharedValue(0);
const scale = useSharedValue(1);
const pan = Gesture.Pan()
.onBegin(() => {
pressed.value = true;
})
.onStart(() => {
scale.value = withSpring(0.7);
})
.onFinalize(() => {
offset.value = withSpring(0, { damping: 20, stiffness: 150 });
scale.value = withTiming(1);
pressed.value = false;
})
.onUpdate((event) => {
offset.value = event.translationX;
});
const press = Gesture.LongPress()
.onStart(() => {
scale.value = withSpring(1.3, { stiffness: 175 });
})
.onFinalize(() => {
scale.value = withTiming(1);
});
const composedPan = Gesture.Simultaneous(pan, capturedPan);
const composedPress = Gesture.Simultaneous(press, capturedPress);
const composed = Gesture.Race(composedPan, composedPress);
const animatedStyles = useAnimatedStyle(() => ({
transform: [
{ translateX: withSpring(offset.value, { duration: 1000 }) },
{ scale: scale.value },
],
backgroundColor: pressed.value ? '#ffe04b' : '#b58df1',
}));
return (
<>
<View style={[styles.container, styles.chartContainer]}>
<View style={styles.row}>
<Text style={styles.label}>Gesture.Pan()</Text>
{isDesktopMode && (
<Text style={styles.label}>Gesture.LongPress()</Text>
)}
</View>
<FlowChart chartManager={chartManager} />
</View>
<GestureHandlerRootView style={styles.container}>
<View style={styles.container}>
<GestureDetector gesture={composed}>
<Animated.View style={[styles.circle, animatedStyles]} />
</GestureDetector>
</View>
</GestureHandlerRootView>
</>
);
}
const styles = StyleSheet.create({
chartContainer: {
marginBottom: 60,
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
height: '100%',
},
circle: {
height: 120,
width: 120,
borderRadius: 500,
cursor: 'grab',
},
row: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '100%',
marginBottom: 20,
},
label: {
fontSize: 24,
fontWeight: 'bold',
marginTop: 24,
marginBottom: 14,
color: 'var(--ifm-font-color-base)',
},
});