Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 3ce8bc0

Browse files
committed
Set context in createReduxContainer
Sets `NavigationContext`, `ThemeContext`, and `SafeAreaContext` for downstream consumers
1 parent 4762c02 commit 3ce8bc0

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"peerDependencies": {
2424
"@react-navigation/core": "*",
2525
"react": "*",
26+
"react-native-safe-area-context": "*",
2627
"redux": "*"
2728
}
2829
}

src/create-redux-container.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import type {
66
NavigationNavigator,
77
NavigationScreenProp,
88
NavigationNavigatorProps,
9+
SupportedThemes,
910
} from '@react-navigation/core';
1011

1112
import * as React from 'react';
13+
import { SafeAreaProvider } from 'react-native-safe-area-context';
14+
import {
15+
ThemeProvider,
16+
NavigationProvider,
17+
} from '@react-navigation/core';
1218

1319
import {
1420
initializeListeners,
@@ -35,6 +41,7 @@ function createReduxContainer<
3541
ContainerProps: {
3642
...$Diff<NavigatorProps, InjectedProps<State>>,
3743
...$Exact<RequiredProps<State>>,
44+
theme: SupportedThemes | 'no-preference',
3845
},
3946
>(
4047
Navigator: NavigatorType,
@@ -47,6 +54,7 @@ function createReduxContainer<
4754

4855
static router = Navigator.router;
4956
currentNavProp: ?NavigationScreenProp<State>;
57+
static defaultProps = { theme: 'no-preference' };
5058

5159
componentDidMount() {
5260
initializeListeners(key, this.props.state);
@@ -60,6 +68,21 @@ function createReduxContainer<
6068
return this.currentNavProp;
6169
}
6270

71+
get theme() {
72+
if (this.props.theme === 'light' || this.props.theme === 'dark') {
73+
return this.props.theme;
74+
} else if (this.props.theme === 'no-preference') {
75+
return 'light';
76+
} else {
77+
console.warn(
78+
`Invalid theme provided: ${
79+
this.props.theme
80+
}. Only 'light' and 'dark' are supported. Falling back to 'light'`
81+
);
82+
return 'light';
83+
}
84+
}
85+
6386
render() {
6487
const { dispatch, state, ...props } = this.props;
6588
this.currentNavProp = propConstructor(
@@ -69,10 +92,16 @@ function createReduxContainer<
6992
this.getCurrentNavigation,
7093
);
7194
return (
72-
<Navigator
73-
{...props}
74-
navigation={this.currentNavProp}
75-
/>
95+
<SafeAreaProvider>
96+
<ThemeProvider value={this.theme}>
97+
<NavigationProvider value={this.currentNavProp}>
98+
<Navigator
99+
{...props}
100+
navigation={this.currentNavProp}
101+
/>
102+
</NavigationProvider>
103+
</ThemeProvider>
104+
</SafeAreaProvider>
76105
);
77106
}
78107

0 commit comments

Comments
 (0)