Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit e0f28a4

Browse files
committed
feat: compatibility layer
1 parent 438692d commit e0f28a4

21 files changed

+905
-12
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@react-navigation/core",
77
"@react-navigation/native",
88
"@react-navigation/routers",
9+
"@react-navigation/compat",
910
"@react-navigation/stack",
1011
"@react-navigation/drawer",
1112
"@react-navigation/bottom-tabs",

packages/compat/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `@react-navigation/compat`
2+
3+
Compatibility layer to write navigator definitions in static configuration format.
4+
5+
## Installation
6+
7+
Open a Terminal in your project's folder and run,
8+
9+
```sh
10+
yarn add @react-navigation/core @react-navigation/compat
11+
```
12+
13+
## Usage
14+
15+
```js
16+
import { createCompatNavigatorFactory } from '@react-navigation/compat';
17+
import { createStackNavigator } from '@react-navigation/stack';
18+
19+
const RootStack = createCompatNavigatorFactory(createStackNavigator)(
20+
{
21+
Home: { screen: HomeScreen },
22+
Profile: { screen: ProfileScreen },
23+
},
24+
{
25+
initialRouteName: 'Profile',
26+
}
27+
);
28+
```

packages/compat/package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@react-navigation/compat",
3+
"description": "Compatibility layer to write navigator definitions in static configuration format",
4+
"version": "5.0.0-alpha.0",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/react-navigation/navigation-ex.git",
9+
"directory": "packages/compat"
10+
},
11+
"main": "lib/commonjs/index.js",
12+
"react-native": "src/index.tsx",
13+
"module": "lib/module/index.js",
14+
"types": "lib/typescript/compat/src/index.d.ts",
15+
"files": [
16+
"src",
17+
"lib"
18+
],
19+
"publishConfig": {
20+
"access": "public"
21+
},
22+
"scripts": {
23+
"prepare": "bob build",
24+
"clean": "del lib"
25+
},
26+
"dependencies": {
27+
"@react-navigation/routers": "^5.0.0-alpha.7"
28+
},
29+
"devDependencies": {
30+
"@types/react": "^16.8.19",
31+
"react": "^16.8.3",
32+
"typescript": "^3.5.3"
33+
},
34+
"peerDependencies": {
35+
"react": "^16.8.3",
36+
"@react-navigation/core": "^5.0.0-alpha.0"
37+
},
38+
"@react-native-community/bob": {
39+
"source": "src",
40+
"output": "lib",
41+
"targets": [
42+
"commonjs",
43+
"module",
44+
"typescript"
45+
]
46+
}
47+
}

packages/compat/src/CompatScreen.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
import {
3+
NavigationProp,
4+
ParamListBase,
5+
RouteProp,
6+
} from '@react-navigation/core';
7+
import ScreenPropsContext from './ScreenPropsContext';
8+
import createCompatNavigationProp from './createCompatNavigationProp';
9+
10+
type Props = {
11+
navigation: NavigationProp<ParamListBase>;
12+
route: RouteProp<ParamListBase, string>;
13+
component: React.ComponentType<any>;
14+
};
15+
16+
function ScreenComponent(props: Props) {
17+
const navigation = React.useMemo(
18+
() => createCompatNavigationProp(props.navigation, props.route),
19+
[props.navigation, props.route]
20+
);
21+
22+
const screenProps = React.useContext(ScreenPropsContext);
23+
24+
return <props.component navigation={navigation} screenProps={screenProps} />;
25+
}
26+
27+
export default React.memo(ScreenComponent);

packages/compat/src/DrawerActions.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DrawerActions, DrawerActionType } from '@react-navigation/routers';
2+
3+
export function openDrawer(): DrawerActionType {
4+
return DrawerActions.openDrawer();
5+
}
6+
7+
export function closeDrawer(): DrawerActionType {
8+
return DrawerActions.closeDrawer();
9+
}
10+
11+
export function toggleDrawer(): DrawerActionType {
12+
return DrawerActions.toggleDrawer();
13+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { CommonActions } from '@react-navigation/core';
2+
3+
export function navigate({
4+
routeName,
5+
params,
6+
key,
7+
action,
8+
}: {
9+
routeName: string;
10+
params?: object;
11+
key?: string;
12+
action?: never;
13+
}): CommonActions.Action {
14+
if (action !== undefined) {
15+
throw new Error(
16+
'Sub-actions are not supported for `navigate`. Remove the `action` key from the options.'
17+
);
18+
}
19+
20+
return CommonActions.navigate({
21+
name: routeName,
22+
key: key,
23+
params: params,
24+
});
25+
}
26+
27+
export function back(options?: { key: null | never }): CommonActions.Action {
28+
if (options !== undefined && options.key != null) {
29+
throw new Error(
30+
"The legacy `back` action with a key is not supported. To go back from a specific route, you need to specify both route key and the navigator's state key in the action: `{ ...CommonActions.goBack(), source: route.key, target: state.key }`."
31+
);
32+
}
33+
34+
return CommonActions.goBack();
35+
}
36+
37+
export function setParams({
38+
params,
39+
key,
40+
}: {
41+
params: object;
42+
key?: string;
43+
}): CommonActions.Action {
44+
return {
45+
...CommonActions.setParams(params),
46+
...(key !== undefined ? { source: key } : null),
47+
};
48+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as React from 'react';
2+
3+
export default React.createContext<unknown>(undefined);

packages/compat/src/StackActions.tsx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { CommonActions } from '@react-navigation/core';
2+
import { StackActions, StackActionType } from '@react-navigation/routers';
3+
4+
export function reset(): CommonActions.Action {
5+
throw new Error(
6+
'The legacy `reset` action is not supported. Use the new reset API by accessing the original navigation object at `navigation.original`.'
7+
);
8+
}
9+
10+
export function replace({
11+
routeName,
12+
params,
13+
key,
14+
newKey,
15+
action,
16+
}: {
17+
routeName: string;
18+
params?: object;
19+
key?: string;
20+
newKey?: string;
21+
action?: never;
22+
}): CommonActions.Action {
23+
if (action !== undefined) {
24+
throw new Error(
25+
'Sub-actions are not supported for `replace`. Remove the `action` key from the options.'
26+
);
27+
}
28+
29+
return {
30+
type: 'REPLACE',
31+
payload: {
32+
name: routeName,
33+
key: newKey,
34+
params,
35+
},
36+
...(key !== undefined ? { source: key } : null),
37+
};
38+
}
39+
40+
export function push({
41+
routeName,
42+
params,
43+
action,
44+
}: {
45+
routeName: string;
46+
params?: object;
47+
action?: never;
48+
}): StackActionType {
49+
if (action !== undefined) {
50+
throw new Error(
51+
'Sub-actions are not supported for `push`. Remove the `action` key from the options.'
52+
);
53+
}
54+
55+
return StackActions.push(routeName, params);
56+
}
57+
58+
export function pop({ n = 1 }: { n: number }): StackActionType {
59+
return StackActions.pop(n);
60+
}
61+
62+
export function popToTop(): StackActionType {
63+
return StackActions.popToTop();
64+
}
65+
66+
export function dismiss(): CommonActions.Action {
67+
throw new Error('The legacy `dismiss` action is not supported.');
68+
}

packages/compat/src/SwitchActions.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TabActions, TabActionType } from '@react-navigation/routers';
2+
3+
export function jumpTo({
4+
routeName,
5+
key,
6+
}: {
7+
routeName: string;
8+
key?: string;
9+
}): TabActionType {
10+
if (key === undefined) {
11+
return TabActions.jumpTo(routeName);
12+
} else {
13+
return {
14+
...TabActions.jumpTo(routeName),
15+
target: key,
16+
};
17+
}
18+
}

0 commit comments

Comments
 (0)