-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathApp.tsx
More file actions
409 lines (389 loc) · 12.8 KB
/
App.tsx
File metadata and controls
409 lines (389 loc) · 12.8 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import React, { useEffect } from 'react';
import {
Text,
View,
StyleSheet,
Platform,
Dimensions,
StatusBar,
SafeAreaView,
} from 'react-native';
import {
createStackNavigator,
StackNavigationProp,
StackScreenProps,
} from '@react-navigation/stack';
import { NavigationContainer, ParamListBase } from '@react-navigation/native';
import {
GestureHandlerRootView,
RectButton,
Switch,
} from 'react-native-gesture-handler';
import AsyncStorage from '@react-native-async-storage/async-storage';
import OverflowParent from './src/release_tests/overflowParent';
import DoublePinchRotate from './src/release_tests/doubleScalePinchAndRotate';
import DoubleDraggable from './src/release_tests/doubleDraggable';
import GesturizedPressable from './src/release_tests/gesturizedPressable';
import { ComboWithGHScroll } from './src/release_tests/combo';
import {
TouchablesIndex,
TouchableExample,
} from './src/release_tests/touchables';
import Rows from './src/release_tests/rows';
import NestedFling from './src/release_tests/nestedFling';
import MouseButtons from './src/release_tests/mouseButtons';
import ContextMenu from './src/release_tests/contextMenu';
import NestedTouchables from './src/release_tests/nestedTouchables';
import NestedPressables from './src/release_tests/nestedPressables';
import NestedButtons from './src/release_tests/nestedButtons';
import PointerType from './src/release_tests/pointerType';
import SwipeableReanimation from './src/release_tests/swipeableReanimation';
import NestedGestureHandlerRootViewWithModal from './src/release_tests/nestedGHRootViewWithModal';
import { PinchableBox } from './src/recipes/scaleAndRotate';
import PanAndScroll from './src/recipes/panAndScroll';
import { BottomSheet } from './src/showcase/bottomSheet';
import Swipeables from './src/showcase/swipeable';
import ChatHeads from './src/showcase/chatHeads';
import Draggable from './src/basic/draggable';
import MultiTap from './src/basic/multitap';
import BouncingBox from './src/basic/bouncing';
import PanResponder from './src/basic/panResponder';
import HorizontalDrawer from './src/basic/horizontalDrawer';
import PagerAndDrawer from './src/basic/pagerAndDrawer';
import ForceTouch from './src/basic/forcetouch';
import Fling from './src/basic/fling';
import WebStylesResetExample from './src/release_tests/webStylesReset';
import StylusData from './src/release_tests/StylusData';
import Camera from './src/new_api/camera';
import Transformations from './src/new_api/transformations';
import Overlap from './src/new_api/overlap';
import Calculator from './src/new_api/calculator';
import BottomSheetNewApi from './src/new_api/bottom_sheet';
import ChatHeadsNewApi from './src/new_api/chat_heads';
import DragNDrop from './src/new_api/drag_n_drop';
import BetterHorizontalDrawer from './src/new_api/betterHorizontalDrawer';
import ManualGestures from './src/new_api/manualGestures/index';
import Hover from './src/new_api/hover';
import HoverableIcons from './src/new_api/hoverable_icons';
import VelocityTest from './src/new_api/velocityTest';
import Swipeable from './src/new_api/swipeable';
import Pressable from './src/new_api/pressable';
import EmptyExample from './src/empty/EmptyExample';
import RectButtonBorders from './src/release_tests/rectButton';
import { ListWithHeader } from './src/ListWithHeader';
import { COLORS } from './src/common';
import MacosDraggable from './src/simple/draggable';
import Tap from './src/simple/tap';
import LongPressExample from './src/simple/longPress';
import ManualExample from './src/simple/manual';
import SimpleFling from './src/simple/fling';
import { Icon } from '@swmansion/icons';
interface Example {
name: string;
component: React.ComponentType;
}
interface ExamplesSection {
sectionTitle: string;
data: Example[];
}
const EXAMPLES: ExamplesSection[] = [
{
sectionTitle: 'Empty',
data: [{ name: 'Empty Example', component: EmptyExample }],
},
{
sectionTitle: 'New api',
data: [
{ name: 'Ball with velocity', component: VelocityTest },
{ name: 'Camera', component: Camera },
{ name: 'Transformations', component: Transformations },
{ name: 'Overlap', component: Overlap },
{ name: 'Bottom Sheet', component: BottomSheetNewApi },
{ name: 'Calculator', component: Calculator },
{ name: 'Chat Heads', component: ChatHeadsNewApi },
{ name: 'Drag and drop', component: DragNDrop },
{ name: 'New Swipeable', component: Swipeable },
{ name: 'Pressable', component: Pressable },
{ name: 'Hover', component: Hover },
{ name: 'Hoverable icons', component: HoverableIcons },
{
name: 'Horizontal Drawer (Reanimated 2 & RNGH 2)',
component: BetterHorizontalDrawer,
},
{
name: 'Manual gestures',
component: ManualGestures,
},
],
},
{
sectionTitle: 'Basic examples',
data: [
{ name: 'Draggable', component: Draggable },
{ name: 'Multitap', component: MultiTap },
{ name: 'Bouncing box', component: BouncingBox },
{ name: 'Pan responder', component: PanResponder },
{ name: 'Horizontal drawer', component: HorizontalDrawer },
{ name: 'Pager & drawer', component: PagerAndDrawer },
{ name: 'Force touch', component: ForceTouch },
{ name: 'Fling', component: Fling },
],
},
{
sectionTitle: 'Recipes',
data: [
{ name: 'Pinch & rotate', component: PinchableBox },
{ name: 'Pan & scroll', component: PanAndScroll },
],
},
{
sectionTitle: 'Showcase',
data: [
{ name: 'Bottom sheet', component: BottomSheet },
{ name: 'Swipeables', component: Swipeables },
{ name: 'Chat heads', component: ChatHeads },
],
},
{
sectionTitle: 'Release tests',
data: [
{
name: 'Views overflowing parents - issue #1532',
component: OverflowParent,
},
{
name: 'Modals with nested GHRootViews - issue #139',
component: NestedGestureHandlerRootViewWithModal,
},
{
name: 'Nested Touchables - issue #784',
component: NestedTouchables as React.ComponentType,
},
{
name: 'Nested Pressables - issue #2980',
component: NestedPressables as React.ComponentType,
},
{
name: 'Nested buttons (sound & ripple on Android)',
component: NestedButtons,
},
{ name: 'Double pinch & rotate', component: DoublePinchRotate },
{ name: 'Double draggable', component: DoubleDraggable },
{ name: 'Rows', component: Rows },
{ name: 'Nested Fling', component: NestedFling },
{ name: 'Combo', component: ComboWithGHScroll },
{ name: 'Touchables', component: TouchablesIndex as React.ComponentType },
{ name: 'MouseButtons', component: MouseButtons },
{ name: 'ContextMenu (web only)', component: ContextMenu },
{ name: 'PointerType', component: PointerType },
{ name: 'Swipeable Reanimation', component: SwipeableReanimation },
{ name: 'RectButton (borders)', component: RectButtonBorders },
{ name: 'Gesturized pressable', component: GesturizedPressable },
{ name: 'Web styles reset', component: WebStylesResetExample },
{ name: 'Stylus data', component: StylusData },
],
},
{
sectionTitle: 'Simple',
data: [
{ name: 'Simple Draggable', component: MacosDraggable },
{ name: 'Tap', component: Tap },
{ name: 'LongPress', component: LongPressExample },
{ name: 'Manual', component: ManualExample },
{ name: 'Simple Fling', component: SimpleFling },
],
},
];
const OPEN_LAST_EXAMPLE_KEY = 'openLastExample';
const LAST_EXAMPLE_KEY = 'lastExample';
type RootStackParamList = {
Home: undefined;
TouchableExample: { item: string };
} & {
[Screen: string]: undefined;
};
const Stack = createStackNavigator<RootStackParamList>();
export default function App() {
return (
<GestureHandlerRootView>
<StatusBar barStyle="dark-content" />
<NavigationContainer>
<Stack.Navigator
screenOptions={{
cardStyle: {
// It's important to set height for the screen, without it scroll doesn't work on web platform.
height: Dimensions.get('window').height,
backgroundColor: COLORS.offWhite,
},
headerStyle: {
backgroundColor: COLORS.offWhite,
borderBottomColor: COLORS.headerSeparator,
borderBottomWidth: 1,
},
}}>
<Stack.Screen
name="Home"
options={{ headerShown: false }}
component={MainScreen}
/>
{EXAMPLES.flatMap(({ data }) => data).flatMap(
({ name, component }) => (
<Stack.Screen
key={name}
name={name}
getComponent={() => component}
options={{ title: name }}
/>
)
)}
<Stack.Screen name="TouchableExample" component={TouchableExample} />
</Stack.Navigator>
</NavigationContainer>
</GestureHandlerRootView>
);
}
function navigate(
navigation: StackNavigationProp<ParamListBase>,
dest: string
) {
AsyncStorage.setItem(LAST_EXAMPLE_KEY, dest);
navigation.navigate(dest);
}
function MainScreen({ navigation }: StackScreenProps<ParamListBase>) {
useEffect(() => {
AsyncStorage.multiGet([OPEN_LAST_EXAMPLE_KEY, LAST_EXAMPLE_KEY]).then(
([openLastExample, lastExample]) => {
if (openLastExample[1] === 'true' && lastExample[1]) {
navigate(navigation, lastExample[1]);
}
}
);
// we only want to run this effect once
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<SafeAreaView style={styles.container}>
<ListWithHeader
style={styles.list}
sections={EXAMPLES}
keyExtractor={(example) => example.name}
ListHeaderComponent={OpenLastExampleSetting}
renderItem={({ item }) => (
<MainScreenItem
name={item.name}
onPressItem={(name) => navigate(navigation, name)}
/>
)}
renderSectionHeader={({ section: { sectionTitle } }) => (
<Text style={styles.sectionTitle}>{sectionTitle}</Text>
)}
ItemSeparatorComponent={() => <View style={styles.separator} />}
/>
</SafeAreaView>
);
}
function OpenLastExampleSetting() {
const [openLastExample, setOpenLastExample] = React.useState(false);
useEffect(() => {
AsyncStorage.getItem(OPEN_LAST_EXAMPLE_KEY).then((value) => {
setOpenLastExample(value === 'true');
});
}, []);
function updateSetting(value: boolean) {
AsyncStorage.setItem(OPEN_LAST_EXAMPLE_KEY, value.toString());
setOpenLastExample(value);
}
return (
<RectButton
style={[styles.button, styles.autoOpenSetting]}
onPress={() => {
updateSetting(!openLastExample);
}}>
<View
style={styles.buttonContent}
pointerEvents={Platform.OS === 'web' ? 'box-only' : 'auto'}>
<Text style={styles.text}>Open last example on launch</Text>
<Switch
value={openLastExample}
onValueChange={() => {
updateSetting(!openLastExample);
}}
/>
</View>
</RectButton>
);
}
interface MainScreenItemProps {
name: string;
onPressItem: (name: string) => void;
}
function MainScreenItem({ name, onPressItem }: MainScreenItemProps) {
return (
<RectButton style={[styles.button]} onPress={() => onPressItem(name)}>
<Text style={styles.text}>{name}</Text>
{Platform.OS !== 'macos' && (
<Icon name="chevron-small-right" size={24} color="#bbb" />
)}
</RectButton>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: COLORS.offWhite,
},
sectionTitle: {
...(Platform.OS !== 'macos' ? { backgroundColor: '#f8f9ff' } : {}),
...Platform.select({
ios: {
fontSize: 17,
fontWeight: '500',
},
android: {
fontSize: 19,
fontFamily: 'sans-serif-medium',
},
}),
padding: 16,
color: 'black',
},
text: {
color: 'black',
},
list: {},
separator: {
height: 2,
},
button: {
flex: 1,
height: 50,
paddingVertical: 10,
paddingHorizontal: 20,
flexDirection: 'row',
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'space-between',
},
buttonContent: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
autoOpenSetting: {
margin: 16,
borderRadius: 16,
backgroundColor: '#eef0ff',
paddingHorizontal: 16,
justifyContent: 'space-between',
elevation: 8,
...(Platform.OS !== 'macos'
? {
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.25,
shadowRadius: 3.84,
}
: {}),
},
});