Skip to content

Commit e7ae750

Browse files
authoredMar 15, 2022
Added Support for dark/light modes (#14)
1 parent be0424a commit e7ae750

File tree

5 files changed

+124
-16
lines changed

5 files changed

+124
-16
lines changed
 

‎App.tsx

+98-6
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,119 @@
11
import { StatusBar } from "expo-status-bar";
22
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context";
3-
import { StyleSheet, FlatList } from "react-native";
3+
import {
4+
Platform,
5+
PlatformColor,
6+
StyleSheet,
7+
FlatList,
8+
useColorScheme,
9+
} from "react-native";
410
import { useState } from "react";
11+
import { white, black } from "./constants/colors";
512
import TaskListItem from "./components/TaskListItem";
613
import TaskCreator from "./components/TaskCreator";
714
import Task from "./utils/Task";
815
import removeFromArray from "./utils/arrayHelpers";
916

10-
const white = "#fff";
1117
const styles = StyleSheet.create({
1218
container: {
1319
alignItems: "flex-start",
14-
backgroundColor: white,
1520
flex: 1,
1621
justifyContent: "flex-start",
1722
marginHorizontal: 10,
1823
},
24+
containerDark: {
25+
...Platform.select({
26+
ios: {
27+
backgroundColor: PlatformColor("systemBackground"),
28+
},
29+
android: {
30+
backgroundColor: PlatformColor(
31+
"@android:color/background_dark"
32+
),
33+
},
34+
default: { backgroundColor: black },
35+
}),
36+
},
37+
containerLight: {
38+
...Platform.select({
39+
ios: {
40+
backgroundColor: PlatformColor("systemBackground"),
41+
},
42+
android: {
43+
backgroundColor: PlatformColor(
44+
"@android:color/background_light"
45+
),
46+
},
47+
default: { backgroundColor: white },
48+
}),
49+
},
1950
creator: {
2051
flex: 1,
2152
},
53+
creatorDark: {
54+
borderColor: white, // PlatformColor crashes the app when used with borderColor
55+
...Platform.select({
56+
ios: {
57+
color: PlatformColor("label"),
58+
},
59+
android: {
60+
color: PlatformColor("@android:color/primary_text_dark"),
61+
},
62+
default: { color: white },
63+
}),
64+
},
65+
creatorLight: {
66+
borderColor: black,
67+
...Platform.select({
68+
ios: {
69+
color: PlatformColor("label"),
70+
},
71+
android: {
72+
color: PlatformColor("@android:color/primary_text_light"),
73+
},
74+
default: { color: black },
75+
}),
76+
},
2277
task: {
2378
marginBottom: 5,
2479
},
80+
taskDark: {
81+
...Platform.select({
82+
ios: {
83+
color: PlatformColor("label"),
84+
},
85+
android: {
86+
color: PlatformColor("@android:color/primary_text_dark"),
87+
},
88+
default: { color: "white" },
89+
}),
90+
},
91+
taskLight: {
92+
...Platform.select({
93+
ios: {
94+
color: PlatformColor("label"),
95+
},
96+
android: {
97+
color: PlatformColor("@android:color/primary_text_light"),
98+
},
99+
default: { color: "black" },
100+
}),
101+
},
25102
taskList: {
26103
flex: 3,
27104
marginTop: 10,
28105
},
29106
});
30107

31108
export default function App(): JSX.Element {
109+
const colourScheme = useColorScheme();
110+
const containerColor =
111+
colourScheme === "dark" ? styles.containerDark : styles.containerLight;
112+
const creatorColor =
113+
colourScheme === "dark" ? styles.creatorDark : styles.creatorLight;
114+
const taskColor =
115+
colourScheme === "dark" ? styles.taskDark : styles.taskLight;
116+
32117
const initialTask = new Task("Welcome to my minimal todo list!");
33118
const [tasks, setTasks] = useState([initialTask]);
34119

@@ -41,13 +126,20 @@ export default function App(): JSX.Element {
41126
};
42127

43128
const renderItem = ({ item }: { item: Task }) => (
44-
<TaskListItem task={item} onClick={removeTask} style={styles.task} />
129+
<TaskListItem
130+
task={item}
131+
onClick={removeTask}
132+
style={[styles.task, taskColor]}
133+
/>
45134
);
46135

47136
return (
48-
<SafeAreaProvider>
137+
<SafeAreaProvider style={containerColor}>
49138
<SafeAreaView style={styles.container}>
50-
<TaskCreator onAdd={addTask} style={styles.creator} />
139+
<TaskCreator
140+
onAdd={addTask}
141+
style={[styles.creator, creatorColor]}
142+
/>
51143
<FlatList
52144
data={tasks}
53145
renderItem={renderItem}

‎app.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"updates": {
1414
"fallbackToCacheTimeout": 0
1515
},
16+
"userInterfaceStyle": "automatic",
1617
"assetBundlePatterns": ["**/*"],
1718
"ios": {
1819
"supportsTablet": true

‎components/TaskCreator.tsx

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { useState } from "react";
2-
import { StyleSheet, TextInput, Button, View, ViewStyle } from "react-native";
2+
import {
3+
StyleSheet,
4+
TextInput,
5+
Button,
6+
View,
7+
ViewStyle,
8+
TextStyle,
9+
} from "react-native";
310
import Task from "../utils/Task";
411

512
const styles = StyleSheet.create({
@@ -17,13 +24,10 @@ const styles = StyleSheet.create({
1724

1825
interface Props {
1926
onAdd: (newTask: Task) => void;
20-
style?: ViewStyle;
27+
style?: (ViewStyle & TextStyle)[];
2128
}
2229

23-
export default function TaskCreator({
24-
onAdd,
25-
style = styles.container,
26-
}: Props): JSX.Element {
30+
export default function TaskCreator({ onAdd, style }: Props): JSX.Element {
2731
const [text, setText] = useState("");
2832

2933
const addTask = () => {
@@ -34,7 +38,7 @@ export default function TaskCreator({
3438
return (
3539
<View style={[styles.container, style]}>
3640
<TextInput
37-
style={styles.input}
41+
style={[styles.input, style]}
3842
onChangeText={setText}
3943
onSubmitEditing={addTask}
4044
value={text}

‎components/TaskListItem.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { StyleSheet, Text, ViewStyle, Button, View } from "react-native";
1+
import {
2+
StyleSheet,
3+
Text,
4+
TextStyle,
5+
ViewStyle,
6+
Button,
7+
View,
8+
} from "react-native";
29
import Task from "../utils/Task";
310

411
const styles = StyleSheet.create({
@@ -19,13 +26,13 @@ const styles = StyleSheet.create({
1926

2027
interface Props {
2128
task: Task;
22-
style?: ViewStyle;
29+
style?: (ViewStyle & TextStyle)[];
2330
onClick: (newTask: Task) => void;
2431
}
2532

2633
export default function TaskListItem({
2734
task,
28-
style = styles.view,
35+
style,
2936
onClick,
3037
}: Props): JSX.Element {
3138
return (
@@ -34,6 +41,7 @@ export default function TaskListItem({
3441
style={[
3542
task.done ? styles.disabledStyle : undefined,
3643
styles.text,
44+
style,
3745
]}
3846
>
3947
- {task.name}

‎constants/colors.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const white = "#FFF";
2+
3+
export const black = "#000";

0 commit comments

Comments
 (0)
Please sign in to comment.