Skip to content

Commit 965f0bd

Browse files
author
Sanjeev Yadav
committed
refactor: Support dark mode in react-native-elements & in app
1 parent 05008ee commit 965f0bd

File tree

14 files changed

+150
-61
lines changed

14 files changed

+150
-61
lines changed

src/App.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import React, { useEffect, useState } from 'react';
2-
import { Appearance, AppearanceProvider } from 'react-native-appearance';
2+
import {
3+
Appearance,
4+
AppearanceProvider,
5+
ColorSchemeName,
6+
} from 'react-native-appearance';
37
import { ThemeProvider } from 'react-native-elements';
48
import { OverflowMenuProvider } from 'react-navigation-header-buttons';
59
import Navigator from './navigation';
610
import { ApolloClient, ApolloProvider } from '@apollo/client';
711
import { getApolloClient } from './apollo/client';
8-
import { theme } from './theme';
12+
import { lightTheme, darkTheme } from './theme';
913
import { Spinner } from './components';
1014

1115
const App = (): React.ReactElement => {
12-
const [colorScheme, setColorScheme] = useState('light');
16+
const [ready, setReady] = useState(false);
17+
const [colorScheme, setColorScheme] = useState<ColorSchemeName>();
1318
const [client, setClient] = useState<ApolloClient<any>>();
1419

15-
console.log(colorScheme);
16-
1720
useEffect(() => {
1821
getApolloClient()
1922
.then(setClient)
@@ -29,11 +32,19 @@ const App = (): React.ReactElement => {
2932
return () => subscription.remove();
3033
}, []);
3134

32-
if (client) {
35+
useEffect(() => {
36+
setColorScheme(Appearance.getColorScheme());
37+
setReady(true);
38+
}, []);
39+
40+
if (ready && client) {
3341
return (
3442
<ApolloProvider client={client}>
3543
<AppearanceProvider>
36-
<ThemeProvider useDark={colorScheme === 'dark'}>
44+
<ThemeProvider
45+
useDark={colorScheme === 'dark'}
46+
theme={colorScheme === 'dark' ? darkTheme : lightTheme}
47+
>
3748
<OverflowMenuProvider>
3849
<Navigator />
3950
</OverflowMenuProvider>

src/components/ProductListItem/ProductListItem.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ interface Props {
1717

1818
const COLUMN_WIDTH = DIMENS.common.WINDOW_WIDTH / 2;
1919

20-
// TODO: remove hard-coded color & dimension vaues
2120
const ProductListItem = ({
2221
item,
2322
index,
@@ -35,7 +34,10 @@ const ProductListItem = ({
3534
<View
3635
style={[
3736
styles.container,
38-
{ borderColor: theme.colors?.divider },
37+
{
38+
borderColor: theme.colors?.divider,
39+
backgroundColor: theme.colors?.white,
40+
},
3941
horizontalMode && styles.topBorder,
4042
(horizontalMode || index % 2 !== 0) && styles.leftBorder,
4143
]}
@@ -55,7 +57,6 @@ const styles = StyleSheet.create({
5557
flex: 1,
5658
width: COLUMN_WIDTH,
5759
borderBottomWidth: DIMENS.common.borderWidth,
58-
backgroundColor: 'white',
5960
overflow: 'hidden',
6061
},
6162
leftBorder: {

src/constants/dimens.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const DIMENS = Object.freeze({
2121
imageWidth: moderateScale(70),
2222
imageHeight: moderateScale(70),
2323
},
24+
featuredProductList: {
25+
titleFontSize: moderateScale(16),
26+
},
2427
homeScreen: {
2528
carouselHeight: moderateScale(200),
2629
},

src/navigation/RootNavigator.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import React from 'react';
2-
import {
3-
DarkTheme,
4-
DefaultTheme,
5-
NavigationContainer,
6-
} from '@react-navigation/native';
2+
import { NavigationContainer } from '@react-navigation/native';
73
import { createDrawerNavigator } from '@react-navigation/drawer';
84
import { useColorScheme } from 'react-native-appearance';
95
import StackNavigator from './StackNavigator';
106
import { DrawerScreen } from '../screens';
117
import { useCart } from '../logic/cart/useCart';
8+
import { navigationLightTheme, navigationDarkTheme } from '../theme';
129

1310
const Drawer = createDrawerNavigator();
1411

1512
const RootNavigator = () => {
1613
const scheme = useColorScheme();
1714
const { cartId } = useCart();
1815
return (
19-
<NavigationContainer theme={scheme === 'dark' ? DarkTheme : DefaultTheme}>
16+
<NavigationContainer
17+
theme={scheme === 'dark' ? navigationDarkTheme : navigationLightTheme}
18+
>
2019
<Drawer.Navigator drawerContent={props => <DrawerScreen {...props} />}>
2120
<Drawer.Screen
2221
name="drawer"

src/screens/CartScreen/CartScreen.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { View, Text } from 'react-native';
2+
import { View } from 'react-native';
3+
import { Text } from 'react-native-elements';
34

45
const CartScreen = (): React.ReactElement => (
56
<View>

src/screens/HomeScreen/FeaturedProductList.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import { useNavigation } from '@react-navigation/native';
33
import { View, FlatList, StyleSheet } from 'react-native';
4-
import { Text } from 'react-native-elements';
4+
import { Text, ThemeContext } from 'react-native-elements';
55
import { ProductInListType } from '../../apollo/queries/productsFragment';
66
import { ProductListItem, Spinner } from '../../components';
77
import { DIMENS, SPACING } from '../../constants';
@@ -19,6 +19,7 @@ const FeaturedProductList = ({
1919
categoryId,
2020
}: Props): React.ReactElement => {
2121
const { data, networkStatus, error } = useCategoryProducts({ categoryId });
22+
const { theme } = useContext(ThemeContext);
2223
const navigation = useNavigation();
2324

2425
const onProductItemClicked = (index: number) => {
@@ -57,14 +58,14 @@ const FeaturedProductList = ({
5758

5859
if (networkStatus === NetworkStatus.loading) {
5960
return (
60-
<View style={styles.loadingBox}>
61+
<View style={styles.loadingBox(theme)}>
6162
<Spinner />
6263
</View>
6364
);
6465
}
6566

6667
return (
67-
<View style={styles.container}>
68+
<View style={styles.container(theme)}>
6869
{name && (
6970
<Text h4 h4Style={styles.title}>
7071
{name}
@@ -81,25 +82,24 @@ const FeaturedProductList = ({
8182
);
8283
};
8384

84-
// TODO: Extract hard coded values
8585
const styles = StyleSheet.create({
86-
container: {
86+
container: theme => ({
8787
marginBottom: SPACING.large,
88-
backgroundColor: '#fff',
89-
},
88+
backgroundColor: theme.colors?.white,
89+
}),
9090
title: {
9191
marginStart: SPACING.large,
9292
paddingVertical: SPACING.small,
93-
fontSize: 16,
93+
fontSize: DIMENS.featuredProductList.titleFontSize,
9494
},
95-
loadingBox: {
95+
loadingBox: theme => ({
9696
alignContent: 'center',
9797
justifyContent: 'center',
9898
width: '100%',
99-
backgroundColor: '#fff',
99+
backgroundColor: theme.colors?.white,
100100
marginBottom: SPACING.large,
101101
height: (DIMENS.common.WINDOW_WIDTH / 3) * 2, // This is linked to ProductListItem height
102-
},
102+
}),
103103
});
104104

105105
export default FeaturedProductList;

src/screens/ProductDetailsScreen/ProductDetailsScreen.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useEffect } from 'react';
1+
import React, { useContext, useEffect } from 'react';
22
import { View, StyleSheet, Dimensions } from 'react-native';
3-
import { Text } from 'react-native-elements';
3+
import { Text, ThemeContext } from 'react-native-elements';
44
import { RouteProp } from '@react-navigation/native';
55
import { StackNavigationProp } from '@react-navigation/stack';
66
import HTML from 'react-native-render-html';
@@ -35,6 +35,7 @@ const ProductDetailsScreen = ({
3535
} = useProductDetails({
3636
sku,
3737
});
38+
const { theme } = useContext(ThemeContext);
3839

3940
useEffect(() => {
4041
getProductDetails();
@@ -57,6 +58,7 @@ const ProductDetailsScreen = ({
5758
source={{ html: productDetails.description.html }}
5859
contentWidth={Dimensions.get('window').width}
5960
containerStyle={styles.description}
61+
baseFontStyle={{ color: theme.colors?.black }}
6062
/>
6163
)}
6264
</View>

src/screens/ProfileScreen/ProfileScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect } from 'react';
2-
import { View, Text } from 'react-native';
2+
import { View } from 'react-native';
33
import { BottomTabNavigationProp } from '@react-navigation/bottom-tabs';
4-
import { Button } from 'react-native-elements';
4+
import { Button, Text } from 'react-native-elements';
55
import { GenericTemplate } from '../../components';
66
import { translate } from '../../i18n';
77
import { useCustomer, useLogout } from '../../logic';

src/screens/SearchScreen/SearchScreen.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
9090
}}
9191
loadingProps={loadingProps}
9292
containerStyle={styles.searchBarContainer}
93-
inputContainerStyle={styles.searchBarInputContainer}
9493
/>
9594
<FlatList
9695
numColumns={2}
@@ -105,7 +104,6 @@ const SearchScreen = ({ navigation }: Props): React.ReactElement => {
105104
);
106105
};
107106

108-
// TODO: Remove hard-coded values
109107
const styles = StyleSheet.create({
110108
center: {
111109
flex: 1,
@@ -114,7 +112,6 @@ const styles = StyleSheet.create({
114112
padding: SPACING.large,
115113
},
116114
searchBarContainer: {
117-
padding: 0,
118115
borderTopWidth: 0,
119116
borderBottomWidth: StyleSheet.hairlineWidth,
120117
borderBottomColor: 'rgba(0,0,0,.1)',
@@ -129,10 +126,6 @@ const styles = StyleSheet.create({
129126
shadowRadius: 2,
130127
elevation: 1,
131128
},
132-
searchBarInputContainer: {
133-
borderRadius: 0,
134-
backgroundColor: 'white',
135-
},
136129
footerContainer: {
137130
alignItems: 'center',
138131
marginVertical: SPACING.small,

src/theme/colors.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Note: Don't use the variables directly in code
3+
* use colors from lightTheme and darkTheme
4+
*/
5+
6+
interface Colors {
7+
primary: string;
8+
secondary: string;
9+
background: string;
10+
}
11+
12+
const WHITE = '#fff';
13+
const BLACK = '#000';
14+
const PRIMARY_COLOR = '#333333';
15+
const SECONDARY_COLOR = '#FF7900';
16+
const BACKGROUND_COLOR = '#fbfbfb';
17+
18+
export const lightColors: Colors = {
19+
primary: PRIMARY_COLOR,
20+
secondary: SECONDARY_COLOR,
21+
background: BACKGROUND_COLOR,
22+
};
23+
24+
export const darkColors: Colors = {
25+
primary: PRIMARY_COLOR,
26+
secondary: SECONDARY_COLOR,
27+
background: BLACK,
28+
};

0 commit comments

Comments
 (0)