Skip to content

Commit 68fd9a1

Browse files
authored
feat: Add iOS 13 dark-mode support (#288)
* Handled iOS dark-mode * Update CustomDatePickerIOS.js
1 parent 27ce732 commit 68fd9a1

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export default class DateTimePickerTester extends Component {
8787
| dismissOnBackdropPressIOS | bool | true | Dismiss the picker on backdrop press (on iOS)? |
8888
| hideTitleContainerIOS | bool | false | If true, hide the modal title container on iOS |
8989
| is24Hour | bool | true | If false, the picker shows an AM/PM chooser on Android |
90+
| isDarkModeEnabled | bool | false | Is the dark mode enabled? |
9091
| isVisible | bool | false | Show the datetime picker? |
9192
| maximumDate | Date | undefined | Max Date. Does not work with 'time' picker on Android |
9293
| minimumDate | Date | undefined | Min Date. Does not work with 'time' picker on Android |
@@ -168,7 +169,13 @@ Edit your `AppDelegate.m` file, and add:
168169
// Force DatePicker locale to current language (for: 24h or 12h format, full day names etc...)
169170
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
170171
[[UIDatePicker appearance] setLocale:[[NSLocale alloc]initWithLocaleIdentifier:currentLanguage]];
171-
```
172+
173+
### Is the iOS dark mode supported?
174+
175+
iOS 13 dark mode is not supported out-of-the-box yet and requires a bit of manual setup:
176+
1. Install and link [react-native-appearance](https://github.com/expo/react-native-appearance)
177+
2. Use it to detect the device color scheme: `const colorScheme = Appearance.getColorScheme();`
178+
3. Use the color scheme to enable/disable the `react-native-modal-datetime-picker` dark mode trough the `isDarkModeEnabled` prop: `isDarkModeEnabled: colorScheme === 'dark'`
172179

173180
### How do I make it work with snapshot testing?
174181

src/CustomDatePickerIOS.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default class CustomDatePickerIOS extends React.PureComponent {
2525
customTitleContainerIOS: PropTypes.node,
2626
dismissOnBackdropPressIOS: PropTypes.bool,
2727
hideTitleContainerIOS: PropTypes.bool,
28+
isDarkModeEnabled: PropTypes.bool,
2829
isVisible: PropTypes.bool,
2930
date: PropTypes.instanceOf(Date),
3031
datePickerContainerStyleIOS: PropTypes.any,
@@ -46,6 +47,7 @@ export default class CustomDatePickerIOS extends React.PureComponent {
4647
date: new Date(),
4748
dismissOnBackdropPressIOS: true,
4849
hideTitleContainerIOS: false,
50+
isDarkModeEnabled: false,
4951
isVisible: false,
5052
mode: "date",
5153
neverDisableConfirmIOS: false,
@@ -131,6 +133,7 @@ export default class CustomDatePickerIOS extends React.PureComponent {
131133
datePickerContainerStyleIOS,
132134
dismissOnBackdropPressIOS,
133135
hideTitleContainerIOS,
136+
isDarkModeEnabled,
134137
isVisible,
135138
minuteInterval,
136139
mode,
@@ -185,6 +188,8 @@ export default class CustomDatePickerIOS extends React.PureComponent {
185188
...reactNativeModalPropsIOS
186189
};
187190

191+
const backgroundColor = isDarkModeEnabled ? BACKGROUND_COLOR_DARK : BACKGROUND_COLOR_LIGHT;
192+
188193
return (
189194
<ReactNativeModal
190195
isVisible={isVisible}
@@ -194,7 +199,7 @@ export default class CustomDatePickerIOS extends React.PureComponent {
194199
backdropOpacity={0.4}
195200
{...reactNativeModalProps}
196201
>
197-
<View style={[styles.datepickerContainer, datePickerContainerStyleIOS]}>
202+
<View style={[styles.datepickerContainer, { backgroundColor }, datePickerContainerStyleIOS]}>
198203
{!hideTitleContainerIOS &&
199204
(customTitleContainerIOS || titleContainer)}
200205
<View
@@ -224,7 +229,7 @@ export default class CustomDatePickerIOS extends React.PureComponent {
224229
</View>
225230

226231
<TouchableHighlight
227-
style={[styles.cancelButton, cancelButtonContainerStyleIOS]}
232+
style={[styles.cancelButton, { backgroundColor }, cancelButtonContainerStyleIOS]}
228233
underlayColor={HIGHLIGHT_COLOR}
229234
onPress={this.handleCancel}
230235
>
@@ -236,7 +241,8 @@ export default class CustomDatePickerIOS extends React.PureComponent {
236241
}
237242

238243
const BORDER_RADIUS = 13;
239-
const BACKGROUND_COLOR = "white";
244+
const BACKGROUND_COLOR_LIGHT = "white";
245+
const BACKGROUND_COLOR_DARK = "#0E0E0E";
240246
const BORDER_COLOR = "#d5d5d5";
241247
const TITLE_FONT_SIZE = 13;
242248
const TITLE_COLOR = "#8f8f8f";
@@ -251,7 +257,6 @@ const styles = StyleSheet.create({
251257
margin: 10
252258
},
253259
datepickerContainer: {
254-
backgroundColor: BACKGROUND_COLOR,
255260
borderRadius: BORDER_RADIUS,
256261
marginBottom: 8,
257262
overflow: "hidden"
@@ -282,7 +287,6 @@ const styles = StyleSheet.create({
282287
backgroundColor: "transparent"
283288
},
284289
cancelButton: {
285-
backgroundColor: BACKGROUND_COLOR,
286290
borderRadius: BORDER_RADIUS,
287291
height: 57,
288292
marginBottom: isIphoneX() ? 20 : 0,

typings/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ interface DateTimePickerProps {
129129
*/
130130
locale?: string;
131131

132+
/**
133+
* Toggles the dark mode style of the picker
134+
*
135+
* Default is false
136+
*/
137+
isDarkModeEnabled?: boolean;
138+
132139
/**
133140
* Sets the visibility of the picker
134141
*

0 commit comments

Comments
 (0)