Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
336 lines (317 loc) · 9.31 KB
/
Copy pathApp.tsx
File metadata and controls
336 lines (317 loc) · 9.31 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
import { type ComponentType } from 'react'
import { ScrollView, StyleSheet, Text, View } from 'react-native'
import { StatusBar } from 'expo-status-bar'
import {
CommonActions,
NavigationContainer,
useNavigation,
} from '@react-navigation/native'
import { createNativeStackNavigator } from '@react-navigation/native-stack'
import { GestureHandlerRootView } from 'react-native-gesture-handler'
import { TapScreen } from './screens/TapScreen'
import { DoubleTapScreen } from './screens/DoubleTapScreen'
import { LongPressScreen } from './screens/LongPressScreen'
import { DragScreen } from './screens/DragScreen'
import { PanScreen } from './screens/PanScreen'
import { SwipeScreen } from './screens/SwipeScreen'
import { PinchScreen } from './screens/PinchScreen'
import { RotateScreen } from './screens/RotateScreen'
import {
SafeAreaProvider,
useSafeAreaInsets,
} from 'react-native-safe-area-context'
// Read the version from the package itself so the footer cannot drift behind
// a release the way a hardcoded string does.
import { version as impulseVersion } from '@rootnative/impulse/package.json'
/**
* One route per intent, plus `home`. Add the intent's name here and to
* `SCREENS` in the same commit as the hook — a hook without a screen cannot
* pass its graduation gate, because a test runner cannot tell you whether a
* gesture feels right on a device.
*/
type Route =
| 'home'
| 'tap'
| 'double-tap'
| 'long-press'
| 'drag'
| 'pan'
| 'swipe'
| 'pinch'
| 'rotate'
/** Every route takes no params — the gallery is a flat list of demos. */
type RootStackParamList = Record<Route, undefined>
/** Every route except the gallery itself. */
type IntentRoute = Exclude<Route, 'home'>
type ScreenComponent = ComponentType<{ onBack: () => void }>
type ScreenEntry = readonly [route: IntentRoute, Screen: ScreenComponent]
/**
* Every intent screen, in gallery order. Screens keep a plain
* `({ onBack }) => …` signature, so this list is the only place that knows
* they live in a navigator.
*
* A tuple list rather than a record: `Object.entries` widens its keys back to
* `string`, and the cast that recovers them would be needed to narrow them
* again.
*
* A route belongs in `Route` and here, in the same edit. A name in one and
* not the other is a crash on navigate, and neither TypeScript nor the
* navigator will tell you first.
*/
const SCREENS: readonly ScreenEntry[] = [
['tap', TapScreen],
['double-tap', DoubleTapScreen],
['long-press', LongPressScreen],
['drag', DragScreen],
['pan', PanScreen],
['swipe', SwipeScreen],
['pinch', PinchScreen],
['rotate', RotateScreen],
]
/**
* Bind a screen to the navigator's back action. Built once at module scope so
* every `Stack.Screen` gets a stable component identity — an inline arrow
* here would remount the screen on every render of the navigator.
*/
function createRoute(Screen: ScreenComponent): ComponentType {
return function RouteScreen() {
const navigation = useNavigation()
return <Screen onBack={navigation.goBack} />
}
}
const ROUTES = SCREENS.map(
([name, Screen]) => [name, createRoute(Screen)] as const,
)
type HomeLink = {
route: IntentRoute
label: string
description: string
}
type HomeSection = {
title: string
blurb: string
links: ReadonlyArray<HomeLink>
}
const SECTIONS: ReadonlyArray<HomeSection> = [
{
title: 'Discrete',
blurb:
'One touch, one meaning. These recognize and are done, rather than streaming a value.',
links: [
{
route: 'tap',
label: 'useTap',
description: 'a single tap, with a pressed state driven by isActive',
},
{
route: 'double-tap',
label: 'useDoubleTap',
description:
'two taps, paired with a single tap — and what that pairing costs',
},
{
route: 'long-press',
label: 'useLongPress',
description: 'a held press that reports while the finger is down',
},
],
},
{
title: 'Continuous',
blurb:
'The finger stays down and the hook streams a value. These are the ones that have to coexist with a scroll view.',
links: [
{
route: 'drag',
label: 'useDrag',
description: 'x and y that follow the finger, with bounds and elastic',
},
{
route: 'pan',
label: 'usePan',
description:
'movement rather than position — a per-frame change the screen adds up',
},
{
route: 'swipe',
label: 'useSwipe',
description:
'a pan judged at release, with the direction that decides it',
},
{
route: 'pinch',
label: 'usePinch',
description:
'a scale that accumulates, about the point between the fingers',
},
{
route: 'rotate',
label: 'useRotate',
description: 'an angle in degrees, turned about the anchor',
},
],
},
]
const SCREEN_OPTIONS = { headerShown: false } as const
function HomeScreen() {
const navigation = useNavigation()
const insets = useSafeAreaInsets()
// `navigate`'s overloads distribute over the param list, so a union of
// route names satisfies none of them. Dispatching the action takes a plain
// string and keeps the call site cast-free.
const open = (route: IntentRoute) =>
navigation.dispatch(CommonActions.navigate(route))
return (
<ScrollView
style={styles.container}
contentContainerStyle={[styles.content, { paddingTop: insets.top + 16 }]}
>
<StatusBar style="auto" />
<View style={styles.hero}>
<Text style={styles.eyebrow}>@rootnative/impulse</Text>
<Text style={styles.title}>Example gallery</Text>
<Text style={styles.subtitle}>
One screen per intent. A gesture cannot be validated by a test runner
alone, so every hook lands here and gets a pass on real hardware
before it ships.
</Text>
</View>
{SECTIONS.map((section) => (
<View key={section.title} style={styles.section}>
<View style={styles.sectionHeader}>
<Text style={styles.sectionTitle}>{section.title}</Text>
<View style={styles.countChip}>
<Text style={styles.countChipLabel}>{section.links.length}</Text>
</View>
</View>
<Text style={styles.sectionBlurb}>{section.blurb}</Text>
<View style={styles.linkList}>
{section.links.map((link) => (
<Text
key={link.route}
style={styles.link}
onPress={() => open(link.route)}
>
{link.label} — {link.description}
</Text>
))}
</View>
</View>
))}
<Text style={styles.footer}>
{impulseVersion} · React Native · Gesture Handler 3
</Text>
</ScrollView>
)
}
const Stack = createNativeStackNavigator<RootStackParamList>()
export default function App() {
return (
// Required, and its absence is silent: without it a gesture simply never
// fires. It wraps the whole app rather than one screen so a gesture in
// any screen — and in the navigator's own transitions — is served.
<GestureHandlerRootView style={styles.root}>
<SafeAreaProvider>
<NavigationContainer>
<Stack.Navigator screenOptions={SCREEN_OPTIONS}>
{/*
Every screen renders its own header via `ScreenShell`, so the
navigator's is hidden. The stack keeps `home` mounted
underneath, which preserves the gallery's scroll offset on the
way back.
*/}
<Stack.Screen name="home" component={HomeScreen} />
{ROUTES.map(([name, Screen]) => (
<Stack.Screen key={name} name={name} component={Screen} />
))}
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
</GestureHandlerRootView>
)
}
const styles = StyleSheet.create({
root: {
flex: 1,
},
container: {
flex: 1,
backgroundColor: '#f8fafc',
},
content: {
paddingHorizontal: 12,
paddingBottom: 40,
gap: 28,
},
hero: {
backgroundColor: '#111827',
borderRadius: 24,
paddingVertical: 28,
paddingHorizontal: 24,
gap: 8,
},
eyebrow: {
fontSize: 13,
fontWeight: '600',
letterSpacing: 1.2,
textTransform: 'uppercase',
color: '#c4b5fd',
},
title: {
fontSize: 32,
fontWeight: '700',
color: '#ffffff',
},
subtitle: {
fontSize: 15,
lineHeight: 22,
color: '#9ca3af',
},
section: {
gap: 10,
},
sectionHeader: {
flexDirection: 'row',
alignItems: 'center',
gap: 8,
},
sectionTitle: {
fontSize: 20,
fontWeight: '700',
color: '#111827',
},
countChip: {
backgroundColor: '#ede9fe',
borderRadius: 10,
paddingHorizontal: 8,
paddingVertical: 2,
},
countChipLabel: {
fontSize: 12,
fontWeight: '700',
color: '#6b4fbb',
},
sectionBlurb: {
fontSize: 14,
lineHeight: 20,
color: '#6b7280',
},
linkList: {
gap: 8,
},
link: {
backgroundColor: '#ffffff',
borderRadius: 14,
borderWidth: 1,
borderColor: '#e5e7eb',
paddingHorizontal: 16,
paddingVertical: 14,
fontSize: 15,
color: '#111827',
},
footer: {
textAlign: 'center',
fontSize: 12,
color: '#9ca3af',
},
})