UNPKG

19 kBTypeScriptView Raw
1import type { DefaultRouterOptions, InitialState, NavigationAction, NavigationState, ParamListBase, PartialState, Route } from '@react-navigation/routers';
2import type * as React from 'react';
3declare global {
4 namespace ReactNavigation {
5 interface RootParamList {
6 }
7 }
8}
9declare type Keyof<T extends {}> = Extract<keyof T, string>;
10export declare type DefaultNavigatorOptions<ParamList extends ParamListBase, State extends NavigationState, ScreenOptions extends {}, EventMap extends EventMapBase> = DefaultRouterOptions<Keyof<ParamList>> & {
11 /**
12 * Children React Elements to extract the route configuration from.
13 * Only `Screen`, `Group` and `React.Fragment` are supported as children.
14 */
15 children: React.ReactNode;
16 /**
17 * Event listeners for all the screens in the navigator.
18 */
19 screenListeners?: ScreenListeners<State, EventMap> | ((props: {
20 route: RouteProp<ParamList>;
21 navigation: any;
22 }) => ScreenListeners<State, EventMap>);
23 /**
24 * Default options for all screens under this navigator.
25 */
26 screenOptions?: ScreenOptions | ((props: {
27 route: RouteProp<ParamList>;
28 navigation: any;
29 }) => ScreenOptions);
30 /**
31 * Default options specified by the navigator.
32 * It receives the custom options in the arguments if a function is specified.
33 */
34 defaultScreenOptions?: ScreenOptions | ((props: {
35 route: RouteProp<ParamList>;
36 navigation: any;
37 options: ScreenOptions;
38 }) => ScreenOptions);
39};
40export declare type EventMapBase = Record<string, {
41 data?: any;
42 canPreventDefault?: boolean;
43}>;
44export declare type EventMapCore<State extends NavigationState> = {
45 focus: {
46 data: undefined;
47 };
48 blur: {
49 data: undefined;
50 };
51 state: {
52 data: {
53 state: State;
54 };
55 };
56 beforeRemove: {
57 data: {
58 action: NavigationAction;
59 };
60 canPreventDefault: true;
61 };
62};
63export declare type EventArg<EventName extends string, CanPreventDefault extends boolean | undefined = false, Data = undefined> = {
64 /**
65 * Type of the event (e.g. `focus`, `blur`)
66 */
67 readonly type: EventName;
68 readonly target?: string;
69} & (CanPreventDefault extends true ? {
70 /**
71 * Whether `event.preventDefault()` was called on this event object.
72 */
73 readonly defaultPrevented: boolean;
74 /**
75 * Prevent the default action which happens on this event.
76 */
77 preventDefault(): void;
78} : {}) & (undefined extends Data ? {
79 readonly data?: Readonly<Data>;
80} : {
81 readonly data: Readonly<Data>;
82});
83export declare type EventListenerCallback<EventMap extends EventMapBase, EventName extends keyof EventMap> = (e: EventArg<Extract<EventName, string>, EventMap[EventName]['canPreventDefault'], EventMap[EventName]['data']>) => void;
84export declare type EventConsumer<EventMap extends EventMapBase> = {
85 /**
86 * Subscribe to events from the parent navigator.
87 *
88 * @param type Type of the event (e.g. `focus`, `blur`)
89 * @param callback Callback listener which is executed upon receiving the event.
90 */
91 addListener<EventName extends Keyof<EventMap>>(type: EventName, callback: EventListenerCallback<EventMap, EventName>): () => void;
92 removeListener<EventName extends Keyof<EventMap>>(type: EventName, callback: EventListenerCallback<EventMap, EventName>): void;
93};
94export declare type EventEmitter<EventMap extends EventMapBase> = {
95 /**
96 * Emit an event to child screens.
97 *
98 * @param options.type Type of the event (e.g. `focus`, `blur`)
99 * @param [options.data] Optional information regarding the event.
100 * @param [options.target] Key of the target route which should receive the event.
101 * If not specified, all routes receive the event.
102 */
103 emit<EventName extends Keyof<EventMap>>(options: {
104 type: EventName;
105 target?: string;
106 } & (EventMap[EventName]['canPreventDefault'] extends true ? {
107 canPreventDefault: true;
108 } : {}) & (undefined extends EventMap[EventName]['data'] ? {
109 data?: EventMap[EventName]['data'];
110 } : {
111 data: EventMap[EventName]['data'];
112 })): EventArg<EventName, EventMap[EventName]['canPreventDefault'], EventMap[EventName]['data']>;
113};
114export declare class PrivateValueStore<A, B, C> {
115 /**
116 * UGLY HACK! DO NOT USE THE TYPE!!!
117 *
118 * TypeScript requires a type to be used to be able to infer it.
119 * The type should exist as its own without any operations such as union.
120 * So we need to figure out a way to store this type in a property.
121 * The problem with a normal property is that it shows up in intelliSense.
122 * Adding private keyword works, but the annotation is stripped away in declaration.
123 * Turns out if we use an empty string, it doesn't show up in intelliSense.
124 */
125 protected ''?: {
126 a: A;
127 b: B;
128 c: C;
129 };
130}
131declare type NavigationHelpersCommon<ParamList extends ParamListBase, State extends NavigationState = NavigationState> = {
132 /**
133 * Dispatch an action or an update function to the router.
134 * The update function will receive the current state,
135 *
136 * @param action Action object or update function.
137 */
138 dispatch(action: NavigationAction | ((state: State) => NavigationAction)): void;
139 /**
140 * Navigate to a route in current navigation tree.
141 *
142 * @param name Name of the route to navigate to.
143 * @param [params] Params object for the route.
144 */
145 navigate<RouteName extends keyof ParamList>(...args: undefined extends ParamList[RouteName] ? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]] : [screen: RouteName, params: ParamList[RouteName]]): void;
146 /**
147 * Navigate to a route in current navigation tree.
148 *
149 * @param route Object with `key` or `name` for the route to navigate to, and a `params` object.
150 */
151 navigate<RouteName extends keyof ParamList>(options: {
152 key: string;
153 params?: ParamList[RouteName];
154 merge?: boolean;
155 } | {
156 name: RouteName;
157 key?: string;
158 params: ParamList[RouteName];
159 merge?: boolean;
160 }): void;
161 /**
162 * Reset the navigation state to the provided state.
163 *
164 * @param state Navigation state object.
165 */
166 reset(state: PartialState<State> | State): void;
167 /**
168 * Go back to the previous route in history.
169 */
170 goBack(): void;
171 /**
172 * Check if the screen is focused. The method returns `true` if focused, `false` otherwise.
173 * Note that this method doesn't re-render screen when the focus changes. So don't use it in `render`.
174 * To get notified of focus changes, use `addListener('focus', cb)` and `addListener('blur', cb)`.
175 * To conditionally render content based on focus state, use the `useIsFocused` hook.
176 */
177 isFocused(): boolean;
178 /**
179 * Check if dispatching back action will be handled by navigation.
180 * Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
181 */
182 canGoBack(): boolean;
183 /**
184 * Returns the navigation prop from the parent navigator,
185 */
186 getParent<T = NavigationProp<ParamListBase> | undefined>(): T;
187 /**
188 * Returns the navigator's state.
189 * Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
190 */
191 getState(): State;
192} & PrivateValueStore<ParamList, keyof ParamList, {}>;
193export declare type NavigationHelpers<ParamList extends ParamListBase, EventMap extends EventMapBase = {}> = NavigationHelpersCommon<ParamList> & EventEmitter<EventMap> & {
194 /**
195 * Update the param object for the route.
196 * The new params will be shallow merged with the old one.
197 *
198 * @param params Params object for the current route.
199 */
200 setParams<RouteName extends keyof ParamList>(params: Partial<ParamList[RouteName]>): void;
201};
202export declare type NavigationContainerProps = {
203 /**
204 * Initial navigation state for the child navigators.
205 */
206 initialState?: InitialState;
207 /**
208 * Callback which is called with the latest navigation state when it changes.
209 */
210 onStateChange?: (state: NavigationState | undefined) => void;
211 /**
212 * Callback which is called when an action is not handled.
213 */
214 onUnhandledAction?: (action: NavigationAction) => void;
215 /**
216 * Whether this navigation container should be independent of parent containers.
217 * If this is not set to `true`, this container cannot be nested inside another container.
218 * Setting it to `true` disconnects any children navigators from parent container.
219 */
220 independent?: boolean;
221 /**
222 * Children elements to render.
223 */
224 children: React.ReactNode;
225};
226export declare type NavigationProp<ParamList extends {}, RouteName extends keyof ParamList = Keyof<ParamList>, State extends NavigationState = NavigationState<ParamList>, ScreenOptions extends {} = {}, EventMap extends EventMapBase = {}> = NavigationHelpersCommon<ParamList, State> & {
227 /**
228 * Update the param object for the route.
229 * The new params will be shallow merged with the old one.
230 *
231 * @param params Params object for the current route.
232 */
233 setParams(params: Partial<ParamList[RouteName]>): void;
234 /**
235 * Update the options for the route.
236 * The options object will be shallow merged with default options object.
237 *
238 * @param options Options object for the route.
239 */
240 setOptions(options: Partial<ScreenOptions>): void;
241} & EventConsumer<EventMap & EventMapCore<State>> & PrivateValueStore<ParamList, RouteName, EventMap>;
242export declare type RouteProp<ParamList extends ParamListBase, RouteName extends keyof ParamList = Keyof<ParamList>> = Route<Extract<RouteName, string>, ParamList[RouteName]>;
243export declare type CompositeNavigationProp<A extends NavigationProp<ParamListBase, string, any, any>, B extends NavigationHelpersCommon<ParamListBase, any>> = Omit<A & B, keyof NavigationProp<any>> & NavigationProp<
244/**
245 * Param list from both navigation objects needs to be combined
246 * For example, we should be able to navigate to screens in both A and B
247 */
248(A extends NavigationHelpersCommon<infer T> ? T : never) & (B extends NavigationHelpersCommon<infer U> ? U : never),
249/**
250 * The route name should refer to the route name specified in the first type
251 * Ideally it should work for any of them, but it's not possible to infer that way
252 */
253A extends NavigationProp<any, infer R> ? R : string,
254/**
255 * The type of state should refer to the state specified in the first type
256 */
257A extends NavigationProp<any, any, infer S> ? S : NavigationState,
258/**
259 * Screen options from both navigation objects needs to be combined
260 * This allows typechecking `setOptions`
261 */
262(A extends NavigationProp<any, any, any, infer O> ? O : {}) & (B extends NavigationProp<any, any, any, infer P> ? P : {}),
263/**
264 * Event consumer config should refer to the config specified in the first type
265 * This allows typechecking `addListener`/`removeListener`
266 */
267A extends NavigationProp<any, any, any, any, infer E> ? E : {}>;
268export declare type CompositeScreenProps<A extends {
269 navigation: NavigationProp<ParamListBase, string, any, any>;
270 route: RouteProp<ParamListBase>;
271}, B extends {
272 navigation: NavigationHelpersCommon<ParamListBase, any>;
273}> = {
274 navigation: CompositeNavigationProp<A['navigation'], B['navigation']>;
275 route: A['route'];
276};
277export declare type Descriptor<ScreenOptions extends {}, Navigation extends NavigationProp<any, any, any, any, any>, Route extends RouteProp<any, any>> = {
278 /**
279 * Render the component associated with this route.
280 */
281 render(): JSX.Element;
282 /**
283 * Options for the route.
284 */
285 options: ScreenOptions;
286 /**
287 * Route object for the screen
288 */
289 route: Route;
290 /**
291 * Navigation object for the screen
292 */
293 navigation: Navigation;
294};
295export declare type ScreenListeners<State extends NavigationState, EventMap extends EventMapBase> = Partial<{
296 [EventName in keyof (EventMap & EventMapCore<State>)]: EventListenerCallback<EventMap, EventName>;
297}>;
298export declare type RouteConfigComponent<ParamList extends ParamListBase, RouteName extends keyof ParamList> = {
299 /**
300 * React component to render for this screen.
301 */
302 component: React.ComponentType<any>;
303 getComponent?: never;
304 children?: never;
305} | {
306 /**
307 * Lazily get a React component to render for this screen.
308 */
309 getComponent: () => React.ComponentType<any>;
310 component?: never;
311 children?: never;
312} | {
313 /**
314 * Render callback to render content of this screen.
315 */
316 children: (props: {
317 route: RouteProp<ParamList, RouteName>;
318 navigation: any;
319 }) => React.ReactNode;
320 component?: never;
321 getComponent?: never;
322};
323export declare type RouteConfig<ParamList extends ParamListBase, RouteName extends keyof ParamList, State extends NavigationState, ScreenOptions extends {}, EventMap extends EventMapBase> = {
324 /**
325 * Route name of this screen.
326 */
327 name: RouteName;
328 /**
329 * Navigator options for this screen.
330 */
331 options?: ScreenOptions | ((props: {
332 route: RouteProp<ParamList, RouteName>;
333 navigation: any;
334 }) => ScreenOptions);
335 /**
336 * Event listeners for this screen.
337 */
338 listeners?: ScreenListeners<State, EventMap> | ((props: {
339 route: RouteProp<ParamList, RouteName>;
340 navigation: any;
341 }) => ScreenListeners<State, EventMap>);
342 /**
343 * Function to return an unique ID for this screen.
344 * Receives an object with the route params.
345 * For a given screen name, there will always be only one screen corresponding to an ID.
346 * If `undefined` is returned, it acts same as no `getId` being specified.
347 */
348 getId?: ({ params }: {
349 params: ParamList[RouteName];
350 }) => string | undefined;
351 /**
352 * Initial params object for the route.
353 */
354 initialParams?: Partial<ParamList[RouteName]>;
355} & RouteConfigComponent<ParamList, RouteName>;
356export declare type RouteGroupConfig<ParamList extends ParamListBase, ScreenOptions extends {}> = {
357 /**
358 * Navigator options for this screen.
359 */
360 screenOptions?: ScreenOptions | ((props: {
361 route: RouteProp<ParamList, keyof ParamList>;
362 navigation: any;
363 }) => ScreenOptions);
364 /**
365 * Children React Elements to extract the route configuration from.
366 * Only `Screen`, `Group` and `React.Fragment` are supported as children.
367 */
368 children: React.ReactNode;
369};
370export declare type NavigationContainerEventMap = {
371 /**
372 * Event which fires when the navigation state changes.
373 */
374 state: {
375 data: {
376 /**
377 * The updated state object after the state change.
378 */
379 state: NavigationState | PartialState<NavigationState> | undefined;
380 };
381 };
382 /**
383 * Event which fires when current options changes.
384 */
385 options: {
386 data: {
387 options: object;
388 };
389 };
390 /**
391 * Event which fires when an action is dispatched.
392 * Only intended for debugging purposes, don't use it for app logic.
393 * This event will be emitted before state changes have been applied.
394 */
395 __unsafe_action__: {
396 data: {
397 /**
398 * The action object which was dispatched.
399 */
400 action: NavigationAction;
401 /**
402 * Whether the action was a no-op, i.e. resulted any state changes.
403 */
404 noop: boolean;
405 /**
406 * Stack trace of the action, this will only be available during development.
407 */
408 stack: string | undefined;
409 };
410 };
411};
412export declare type NavigationContainerRef<ParamList extends {}> = NavigationHelpers<ParamList> & EventConsumer<NavigationContainerEventMap> & {
413 /**
414 * Reset the navigation state of the root navigator to the provided state.
415 *
416 * @param state Navigation state object.
417 */
418 resetRoot(state?: PartialState<NavigationState> | NavigationState): void;
419 /**
420 * Get the rehydrated navigation state of the navigation tree.
421 */
422 getRootState(): NavigationState;
423 /**
424 * Get the currently focused navigation route.
425 */
426 getCurrentRoute(): Route<string> | undefined;
427 /**
428 * Get the currently focused route's options.
429 */
430 getCurrentOptions(): object | undefined;
431 /**
432 * Whether the navigation container is ready to handle actions.
433 */
434 isReady(): boolean;
435};
436export declare type NavigationContainerRefWithCurrent<ParamList extends {}> = NavigationContainerRef<ParamList> & {
437 current: NavigationContainerRef<ParamList> | null;
438};
439export declare type TypedNavigator<ParamList extends ParamListBase, State extends NavigationState, ScreenOptions extends {}, EventMap extends EventMapBase, Navigator extends React.ComponentType<any>> = {
440 /**
441 * Navigator component which manages the child screens.
442 */
443 Navigator: React.ComponentType<Omit<React.ComponentProps<Navigator>, keyof DefaultNavigatorOptions<any, any, any, any>> & DefaultNavigatorOptions<ParamList, State, ScreenOptions, EventMap>>;
444 /**
445 * Component used for grouping multiple route configuration.
446 */
447 Group: React.ComponentType<RouteGroupConfig<ParamList, ScreenOptions>>;
448 /**
449 * Component used for specifying route configuration.
450 */
451 Screen: <RouteName extends keyof ParamList>(_: RouteConfig<ParamList, RouteName, State, ScreenOptions, EventMap>) => null;
452};
453export declare type NavigatorScreenParams<ParamList, State extends NavigationState = NavigationState> = {
454 screen?: never;
455 params?: never;
456 initial?: never;
457 path?: string;
458 state: PartialState<State> | State | undefined;
459} | {
460 [RouteName in keyof ParamList]: undefined extends ParamList[RouteName] ? {
461 screen: RouteName;
462 params?: ParamList[RouteName];
463 initial?: boolean;
464 path?: string;
465 state?: never;
466 } : {
467 screen: RouteName;
468 params: ParamList[RouteName];
469 initial?: boolean;
470 path?: string;
471 state?: never;
472 };
473}[keyof ParamList];
474export declare type PathConfig<ParamList extends {}> = {
475 path?: string;
476 exact?: boolean;
477 parse?: Record<string, (value: string) => any>;
478 stringify?: Record<string, (value: any) => string>;
479 screens?: PathConfigMap<ParamList>;
480 initialRouteName?: keyof ParamList;
481};
482export declare type PathConfigMap<ParamList extends {}> = {
483 [RouteName in keyof ParamList]?: NonNullable<ParamList[RouteName]> extends NavigatorScreenParams<infer T, any> ? string | PathConfig<T> : string | Omit<PathConfig<{}>, 'screens' | 'initialRouteName'>;
484};
485export {};