1 | import type {
|
2 | HeaderBackButton,
|
3 | HeaderBackButtonProps,
|
4 | HeaderButtonProps,
|
5 | HeaderOptions,
|
6 | HeaderTitleProps,
|
7 | } from '@react-navigation/elements';
|
8 | import type {
|
9 | Descriptor,
|
10 | NavigationHelpers,
|
11 | NavigationProp,
|
12 | ParamListBase,
|
13 | Route,
|
14 | RouteProp,
|
15 | StackActionHelpers,
|
16 | StackNavigationState,
|
17 | } from '@react-navigation/native';
|
18 | import type * as React from 'react';
|
19 | import type { Animated, StyleProp, TextStyle, ViewStyle } from 'react-native';
|
20 |
|
21 | export type StackNavigationEventMap = {
|
22 | /**
|
23 | * Event which fires when a transition animation starts.
|
24 | */
|
25 | transitionStart: { data: { closing: boolean } };
|
26 | /**
|
27 | * Event which fires when a transition animation ends.
|
28 | */
|
29 | transitionEnd: { data: { closing: boolean } };
|
30 | /**
|
31 | * Event which fires when navigation gesture starts.
|
32 | */
|
33 | gestureStart: { data: undefined };
|
34 | /**
|
35 | * Event which fires when navigation gesture is completed.
|
36 | */
|
37 | gestureEnd: { data: undefined };
|
38 | /**
|
39 | * Event which fires when navigation gesture is canceled.
|
40 | */
|
41 | gestureCancel: { data: undefined };
|
42 | };
|
43 |
|
44 | export type StackNavigationHelpers = NavigationHelpers<
|
45 | ParamListBase,
|
46 | StackNavigationEventMap
|
47 | > &
|
48 | StackActionHelpers<ParamListBase>;
|
49 |
|
50 | export type StackNavigationProp<
|
51 | ParamList extends ParamListBase,
|
52 | RouteName extends keyof ParamList = keyof ParamList,
|
53 | NavigatorID extends string | undefined = undefined
|
54 | > = NavigationProp<
|
55 | ParamList,
|
56 | RouteName,
|
57 | NavigatorID,
|
58 | StackNavigationState<ParamList>,
|
59 | StackNavigationOptions,
|
60 | StackNavigationEventMap
|
61 | > &
|
62 | StackActionHelpers<ParamList>;
|
63 |
|
64 | export type StackScreenProps<
|
65 | ParamList extends ParamListBase,
|
66 | RouteName extends keyof ParamList = keyof ParamList,
|
67 | NavigatorID extends string | undefined = undefined
|
68 | > = {
|
69 | navigation: StackNavigationProp<ParamList, RouteName, NavigatorID>;
|
70 | route: RouteProp<ParamList, RouteName>;
|
71 | };
|
72 |
|
73 | export type Layout = { width: number; height: number };
|
74 |
|
75 | export type GestureDirection =
|
76 | | 'horizontal'
|
77 | | 'horizontal-inverted'
|
78 | | 'vertical'
|
79 | | 'vertical-inverted';
|
80 |
|
81 | type SceneOptionsDefaults = TransitionPreset & {
|
82 | animationEnabled: boolean;
|
83 | gestureEnabled: boolean;
|
84 | cardOverlayEnabled: boolean;
|
85 | headerMode: StackHeaderMode;
|
86 | };
|
87 |
|
88 | export type Scene = {
|
89 | /**
|
90 | * Route object for the current screen.
|
91 | */
|
92 | route: Route<string>;
|
93 | /**
|
94 | * Descriptor object for the screen.
|
95 | */
|
96 | descriptor: Omit<StackDescriptor, 'options'> & {
|
97 | options: Omit<StackDescriptor['options'], keyof SceneOptionsDefaults> &
|
98 | SceneOptionsDefaults;
|
99 | };
|
100 | /**
|
101 | * Animated nodes representing the progress of the animation.
|
102 | */
|
103 | progress: SceneProgress;
|
104 | };
|
105 |
|
106 | export type SceneProgress = {
|
107 | /**
|
108 | * Progress value of the current screen.
|
109 | */
|
110 | current: Animated.AnimatedInterpolation;
|
111 | /**
|
112 | * Progress value for the screen after this one in the stack.
|
113 | * This can be `undefined` in case the screen animating is the last one.
|
114 | */
|
115 | next?: Animated.AnimatedInterpolation;
|
116 | /**
|
117 | * Progress value for the screen before this one in the stack.
|
118 | * This can be `undefined` in case the screen animating is the first one.
|
119 | */
|
120 | previous?: Animated.AnimatedInterpolation;
|
121 | };
|
122 |
|
123 | export type StackHeaderMode = 'float' | 'screen';
|
124 |
|
125 | export type StackPresentationMode = 'card' | 'modal';
|
126 |
|
127 | export type StackHeaderOptions = Omit<
|
128 | HeaderOptions,
|
129 | 'headerLeft' | 'headerTitle' | 'headerRight'
|
130 | > & {
|
131 | /**
|
132 | * String or a function that returns a React Element to be used by the header.
|
133 | * Defaults to screen `title` or route name.
|
134 | *
|
135 | * It receives `allowFontScaling`, `tintColor`, `style` and `children` in the options object as an argument.
|
136 | * The title string is passed in `children`.
|
137 | */
|
138 | headerTitle?: string | ((props: HeaderTitleProps) => React.ReactNode);
|
139 | /**
|
140 | * Function which returns a React Element to display on the left side of the header.
|
141 | */
|
142 | headerLeft?: (props: HeaderBackButtonProps) => React.ReactNode;
|
143 | /**
|
144 | * Function which returns a React Element to display on the right side of the header.
|
145 | */
|
146 | headerRight?: (props: HeaderButtonProps) => React.ReactNode;
|
147 | /**
|
148 | * Whether back button title font should scale to respect Text Size accessibility settings. Defaults to `false`.
|
149 | */
|
150 | headerBackAllowFontScaling?: boolean;
|
151 | /**
|
152 | * Accessibility label for the header back button.
|
153 | */
|
154 | headerBackAccessibilityLabel?: string;
|
155 | /**
|
156 | * ID to locate this back button in tests.
|
157 | */
|
158 | headerBackTestID?: string;
|
159 | /**
|
160 | * Title string used by the back button on iOS.
|
161 | * Defaults to the previous screen's title, or "Back" if there's not enough space.
|
162 | * Use `headerBackTitleVisible: false` to hide it.
|
163 | */
|
164 | headerBackTitle?: string;
|
165 | /**
|
166 | * Whether the back button title should be visible or not.
|
167 | *
|
168 | * Defaults to `true` on iOS, `false` on Android.
|
169 | */
|
170 | headerBackTitleVisible?: boolean;
|
171 | /**
|
172 | * Style object for the back title.
|
173 | */
|
174 | headerBackTitleStyle?: StyleProp<TextStyle>;
|
175 | /**
|
176 | * Title string used by the back button when `headerBackTitle` doesn't fit on the screen. `"Back"` by default.
|
177 | */
|
178 | headerTruncatedBackTitle?: string;
|
179 | /**
|
180 | * Function which returns a React Element to display custom image in header's back button.
|
181 | * It receives the `tintColor` in in the options object as an argument. object.
|
182 | * Defaults to Image component with a the default back icon image for the platform (a chevron on iOS and an arrow on Android).
|
183 | */
|
184 | headerBackImage?: React.ComponentProps<typeof HeaderBackButton>['backImage'];
|
185 | };
|
186 |
|
187 | export type StackHeaderProps = {
|
188 | /**
|
189 | * Layout of the screen.
|
190 | */
|
191 | layout: Layout;
|
192 | /**
|
193 | * Options for the back button.
|
194 | */
|
195 | back?: {
|
196 | /**
|
197 | * Title of the previous screen.
|
198 | */
|
199 | title: string;
|
200 | };
|
201 | /**
|
202 | * Animated nodes representing the progress of the animation.
|
203 | */
|
204 | progress: SceneProgress;
|
205 | /**
|
206 | * Options for the current screen.
|
207 | */
|
208 | options: StackNavigationOptions;
|
209 | /**
|
210 | * Route object for the current screen.
|
211 | */
|
212 | route: Route<string>;
|
213 | /**
|
214 | * Navigation prop for the header.
|
215 | */
|
216 | navigation: StackNavigationProp<ParamListBase>;
|
217 | /**
|
218 | * Interpolated styles for various elements in the header.
|
219 | */
|
220 | styleInterpolator: StackHeaderStyleInterpolator;
|
221 | };
|
222 |
|
223 | export type StackDescriptor = Descriptor<
|
224 | StackNavigationOptions,
|
225 | StackNavigationProp<ParamListBase>,
|
226 | RouteProp<ParamListBase>
|
227 | >;
|
228 |
|
229 | export type StackDescriptorMap = Record<string, StackDescriptor>;
|
230 |
|
231 | export type StackNavigationOptions = StackHeaderOptions &
|
232 | Partial<TransitionPreset> & {
|
233 | /**
|
234 | * String that can be displayed in the header as a fallback for `headerTitle`.
|
235 | */
|
236 | title?: string;
|
237 | /**
|
238 | * Function that given `HeaderProps` returns a React Element to display as a header.
|
239 | */
|
240 | header?: (props: StackHeaderProps) => React.ReactNode;
|
241 | /**
|
242 | * Whether the header floats above the screen or part of the screen.
|
243 | * Defaults to `float` on iOS for non-modals, and `screen` for the rest.
|
244 | */
|
245 | headerMode?: StackHeaderMode;
|
246 | /**
|
247 | * Whether to show the header. The header is shown by default.
|
248 | * Setting this to `false` hides the header.
|
249 | */
|
250 | headerShown?: boolean;
|
251 | /**
|
252 | * Whether a shadow is visible for the card during transitions. Defaults to `true`.
|
253 | */
|
254 | cardShadowEnabled?: boolean;
|
255 | /**
|
256 | * Whether to have a semi-transparent dark overlay visible under the card during transitions.
|
257 | * Defaults to `true` on Android and `false` on iOS.
|
258 | */
|
259 | cardOverlayEnabled?: boolean;
|
260 | /**
|
261 | * Function that returns a React Element to display as a overlay for the card.
|
262 | */
|
263 | cardOverlay?: (props: {
|
264 | style: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
|
265 | }) => React.ReactNode;
|
266 | /**
|
267 | * Style object for the card in stack.
|
268 | * You can provide a custom background color to use instead of the default background here.
|
269 | *
|
270 | * You can also specify `{ backgroundColor: 'transparent' }` to make the previous screen visible underneath.
|
271 | * This is useful to implement things like modal dialogs.
|
272 | *
|
273 | * You should also specify `detachPreviousScreen: false` in options when using a transparent background
|
274 | * so that the previous screen isn't detached and stays below the current screen.
|
275 | *
|
276 | * You might also need to change the animation of the screen using `cardStyleInterpolator`
|
277 | * so that the previous screen isn't transformed or invisible.
|
278 | */
|
279 | cardStyle?: StyleProp<ViewStyle>;
|
280 | /**
|
281 | * Whether this screen should be presented as a modal or a regular card.
|
282 | *
|
283 | * Specifying this will configure several options:
|
284 | * - `card`: Use the default OS animations for iOS and Android screen transitions.
|
285 | * - `modal`: Use Modal animations. This changes a few things:
|
286 | * - Sets `headerMode` to `screen` for the screen unless specified otherwise.
|
287 | * - Changes the screen animation to match the platform behavior for modals.
|
288 | * - `transparentModal`: Similar to `modal`. This changes following things:
|
289 | * - Sets `headerMode` to `screen` for the screen unless specified otherwise.
|
290 | * - Sets background color of the screen to transparent, so previous screen is visible
|
291 | * - Adjusts the `detachPreviousScreen` option so that the previous screen stays rendered.
|
292 | * - Prevents the previous screen from animating from its last position.
|
293 | * - Changes the screen animation to a vertical slide animation.
|
294 | *
|
295 | * Defaults to 'card'.
|
296 | */
|
297 | presentation?: 'card' | 'modal' | 'transparentModal';
|
298 | /**
|
299 | * Whether transition animation should be enabled the screen.
|
300 | * If you set it to `false`, the screen won't animate when pushing or popping.
|
301 | * Defaults to `true` on Android and iOS, `false` on Web.
|
302 | */
|
303 | animationEnabled?: boolean;
|
304 | /**
|
305 | * The type of animation to use when this screen replaces another screen. Defaults to `push`.
|
306 | * When `pop` is used, the `pop` animation is applied to the screen being replaced.
|
307 | */
|
308 | animationTypeForReplace?: 'push' | 'pop';
|
309 | /**
|
310 | * Whether you can use gestures to dismiss this screen. Defaults to `true` on iOS, `false` on Android.
|
311 | * Not supported on Web.
|
312 | */
|
313 | gestureEnabled?: boolean;
|
314 | /**
|
315 | * Distance of touch start from the edge of the screen to recognize gestures.
|
316 | * Not supported on Web.
|
317 | */
|
318 | gestureResponseDistance?: number;
|
319 | /**
|
320 | * Number which determines the relevance of velocity for the gesture. Defaults to 0.3.
|
321 | * Not supported on Web.
|
322 | */
|
323 | gestureVelocityImpact?: number;
|
324 | /**
|
325 | * Whether to detach the previous screen from the view hierarchy to save memory.
|
326 | * Set it to `false` if you need the previous screen to be seen through the active screen.
|
327 | * Only applicable if `detachInactiveScreens` isn't set to `false`.
|
328 | * Defaults to `false` for the last screen for modals, otherwise `true`.
|
329 | */
|
330 | detachPreviousScreen?: boolean;
|
331 | /**
|
332 | * If `false`, the keyboard will NOT automatically dismiss when navigating to a new screen from this screen.
|
333 | * Defaults to `true`.
|
334 | */
|
335 | keyboardHandlingEnabled?: boolean;
|
336 | /**
|
337 | * Whether inactive screens should be suspended from re-rendering. Defaults to `false`.
|
338 | * Defaults to `true` when `enableFreeze()` is run at the top of the application.
|
339 | * Requires `react-native-screens` version >=3.16.0.
|
340 | *
|
341 | * Only supported on iOS and Android.
|
342 | */
|
343 | freezeOnBlur?: boolean;
|
344 | };
|
345 |
|
346 | export type StackNavigationConfig = {
|
347 | /**
|
348 | * Whether inactive screens should be detached from the view hierarchy to save memory.
|
349 | * This will have no effect if you disable `react-native-screens`.
|
350 | *
|
351 | * Defaults to `true`.
|
352 | */
|
353 | detachInactiveScreens?: boolean;
|
354 | };
|
355 |
|
356 | export type TransitionSpec =
|
357 | | {
|
358 | animation: 'spring';
|
359 | config: Omit<
|
360 | Animated.SpringAnimationConfig,
|
361 | 'toValue' | keyof Animated.AnimationConfig
|
362 | >;
|
363 | }
|
364 | | {
|
365 | animation: 'timing';
|
366 | config: Omit<
|
367 | Animated.TimingAnimationConfig,
|
368 | 'toValue' | keyof Animated.AnimationConfig
|
369 | >;
|
370 | };
|
371 |
|
372 | export type StackCardInterpolationProps = {
|
373 | /**
|
374 | * Values for the current screen.
|
375 | */
|
376 | current: {
|
377 | /**
|
378 | * Animated node representing the progress value of the current screen.
|
379 | */
|
380 | progress: Animated.AnimatedInterpolation;
|
381 | };
|
382 | /**
|
383 | * Values for the screen after this one in the stack.
|
384 | * This can be `undefined` in case the screen animating is the last one.
|
385 | */
|
386 | next?: {
|
387 | /**
|
388 | * Animated node representing the progress value of the next screen.
|
389 | */
|
390 | progress: Animated.AnimatedInterpolation;
|
391 | };
|
392 | /**
|
393 | * The index of the card with this interpolation in the stack.
|
394 | */
|
395 | index: number;
|
396 | /**
|
397 | * Animated node representing whether the card is closing (1 - closing, 0 - not closing).
|
398 | */
|
399 | closing: Animated.AnimatedInterpolation;
|
400 | /**
|
401 | * Animated node representing whether the card is being swiped (1 - swiping, 0 - not swiping).
|
402 | */
|
403 | swiping: Animated.AnimatedInterpolation;
|
404 | /**
|
405 | * Animated node representing multiplier when direction is inverted (-1 - inverted, 1 - normal).
|
406 | */
|
407 | inverted: Animated.AnimatedInterpolation;
|
408 | /**
|
409 | * Layout measurements for various items we use for animation.
|
410 | */
|
411 | layouts: {
|
412 | /**
|
413 | * Layout of the whole screen.
|
414 | */
|
415 | screen: Layout;
|
416 | };
|
417 | /**
|
418 | * Safe area insets
|
419 | */
|
420 | insets: {
|
421 | top: number;
|
422 | right: number;
|
423 | bottom: number;
|
424 | left: number;
|
425 | };
|
426 | };
|
427 |
|
428 | export type StackCardInterpolatedStyle = {
|
429 | /**
|
430 | * Interpolated style for the container view wrapping the card.
|
431 | */
|
432 | containerStyle?: any;
|
433 | /**
|
434 | * Interpolated style for the view representing the card.
|
435 | */
|
436 | cardStyle?: any;
|
437 | /**
|
438 | * Interpolated style for the view representing the semi-transparent overlay below the card.
|
439 | */
|
440 | overlayStyle?: any;
|
441 | /**
|
442 | * Interpolated style representing the card shadow.
|
443 | */
|
444 | shadowStyle?: any;
|
445 | };
|
446 |
|
447 | export type StackCardStyleInterpolator = (
|
448 | props: StackCardInterpolationProps
|
449 | ) => StackCardInterpolatedStyle;
|
450 |
|
451 | export type StackHeaderInterpolationProps = {
|
452 | /**
|
453 | * Values for the current screen (the screen which owns this header).
|
454 | */
|
455 | current: {
|
456 | /**
|
457 | * Animated node representing the progress value of the current screen.
|
458 | */
|
459 | progress: Animated.AnimatedInterpolation;
|
460 | };
|
461 | /**
|
462 | * Values for the screen after this one in the stack.
|
463 | * This can be `undefined` in case the screen animating is the last one.
|
464 | */
|
465 | next?: {
|
466 | /**
|
467 | * Animated node representing the progress value of the next screen.
|
468 | */
|
469 | progress: Animated.AnimatedInterpolation;
|
470 | };
|
471 | /**
|
472 | * Layout measurements for various items we use for animation.
|
473 | */
|
474 | layouts: {
|
475 | /**
|
476 | * Layout of the header
|
477 | */
|
478 | header: Layout;
|
479 | /**
|
480 | * Layout of the whole screen.
|
481 | */
|
482 | screen: Layout;
|
483 | /**
|
484 | * Layout of the title element.
|
485 | */
|
486 | title?: Layout;
|
487 | /**
|
488 | * Layout of the back button label.
|
489 | */
|
490 | leftLabel?: Layout;
|
491 | };
|
492 | };
|
493 |
|
494 | export type StackHeaderInterpolatedStyle = {
|
495 | /**
|
496 | * Interpolated style for the label of the left button (back button label).
|
497 | */
|
498 | leftLabelStyle?: any;
|
499 | /**
|
500 | * Interpolated style for the left button (usually the back button).
|
501 | */
|
502 | leftButtonStyle?: any;
|
503 | /**
|
504 | * Interpolated style for the right button.
|
505 | */
|
506 | rightButtonStyle?: any;
|
507 | /**
|
508 | * Interpolated style for the header title text.
|
509 | */
|
510 | titleStyle?: any;
|
511 | /**
|
512 | * Interpolated style for the header background.
|
513 | */
|
514 | backgroundStyle?: any;
|
515 | };
|
516 |
|
517 | export type StackHeaderStyleInterpolator = (
|
518 | props: StackHeaderInterpolationProps
|
519 | ) => StackHeaderInterpolatedStyle;
|
520 |
|
521 | export type TransitionPreset = {
|
522 | /**
|
523 | * The direction of swipe gestures, `horizontal` or `vertical`.
|
524 | */
|
525 | gestureDirection: GestureDirection;
|
526 | /**
|
527 | * Object which specifies the animation type (timing or spring) and their options (such as duration for timing).
|
528 | */
|
529 | transitionSpec: {
|
530 | /**
|
531 | * Transition configuration when adding a screen.
|
532 | */
|
533 | open: TransitionSpec;
|
534 | /**
|
535 | * Transition configuration when removing a screen.
|
536 | */
|
537 | close: TransitionSpec;
|
538 | };
|
539 | /**
|
540 | * Function which specifies interpolated styles for various parts of the card, e.g. the overlay, shadow etc.
|
541 | */
|
542 | cardStyleInterpolator: StackCardStyleInterpolator;
|
543 | /**
|
544 | * Function which specifies interpolated styles for various parts of the header, e.g. the title, left button etc.
|
545 | */
|
546 | headerStyleInterpolator: StackHeaderStyleInterpolator;
|
547 | };
|