Skip to content

Commit fe2123b

Browse files
lukewalczaksatya164
authored andcommitted
fix: correct components using dark theme (callstack#203)
1 parent 9bf9060 commit fe2123b

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed

example/src/ToolbarExample.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import {
99
ToolbarContent,
1010
ToolbarAction,
1111
ToolbarBackAction,
12+
withTheme,
1213
} from 'react-native-paper';
14+
import type { Theme } from 'react-native-paper/types';
1315

1416
type Props = {
1517
navigation: any,
18+
theme: Theme,
1619
};
1720

1821
type State = {
@@ -24,7 +27,7 @@ type State = {
2427

2528
const MORE_ICON = Platform.OS === 'ios' ? 'more-horiz' : 'more-vert';
2629

27-
export default class ToolbarExample extends React.Component<Props, State> {
30+
class ToolbarExample extends React.Component<Props, State> {
2831
static title = 'Toolbar';
2932
static navigationOptions = ({ navigation }) => {
3033
return {
@@ -65,8 +68,16 @@ export default class ToolbarExample extends React.Component<Props, State> {
6568
showMoreIcon,
6669
showSubtitle,
6770
} = this.state;
71+
const { theme: { colors: { background } } } = this.props;
6872
return (
69-
<View style={styles.container}>
73+
<View
74+
style={[
75+
styles.container,
76+
{
77+
backgroundColor: background,
78+
},
79+
]}
80+
>
7081
<View style={styles.content}>
7182
<Button
7283
accent
@@ -135,3 +146,5 @@ const styles = StyleSheet.create({
135146
padding: 4,
136147
},
137148
});
149+
150+
export default withTheme(ToolbarExample);

src/components/Button.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,26 @@ class Button extends React.Component<Props, State> {
123123
theme,
124124
} = this.props;
125125
const { colors, roundness, dark: isDarkTheme } = theme;
126+
const isDarkActive = this.props.dark || this.props.theme.dark;
126127
const fontFamily = theme.fonts.medium;
127128
let backgroundColor, textColor, isDark;
128129
if (raised) {
129130
if (disabled) {
130-
backgroundColor = 'rgba(0, 0, 0, .12)';
131+
isDarkActive
132+
? (backgroundColor = color(white)
133+
.alpha(0.12)
134+
.rgbaString())
135+
: (backgroundColor = color(black)
136+
.alpha(0.12)
137+
.rgbaString());
131138
} else {
132139
if (buttonColor) {
133140
backgroundColor = buttonColor;
134141
} else {
135142
if (primary) {
136143
backgroundColor = colors.primary;
137144
} else {
138-
backgroundColor = dark ? black : white;
145+
backgroundColor = isDarkActive ? 'rgba(58, 55, 55, .9)' : white;
139146
}
140147
}
141148
}
@@ -154,19 +161,23 @@ class Button extends React.Component<Props, State> {
154161

155162
if (disabled) {
156163
textColor = isDarkTheme
157-
? 'rgba(255, 255, 255, .26)'
158-
: 'rgba(0, 0, 0, .26)';
164+
? color(white)
165+
.alpha(0.3)
166+
.rgbaString()
167+
: color(black)
168+
.alpha(0.26)
169+
.rgbaString();
159170
} else {
160171
if (raised) {
161-
textColor = isDark ? white : black;
172+
textColor = isDarkActive ? white : black;
162173
} else {
163174
if (buttonColor) {
164175
textColor = buttonColor;
165176
} else {
166177
if (primary) {
167178
textColor = colors.primary;
168179
} else {
169-
textColor = isDark ? white : black;
180+
textColor = isDark || isDarkTheme ? white : black;
170181
}
171182
}
172183
}

src/components/Divider.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/* @flow */
22

33
import * as React from 'react';
4+
import color from 'color';
45
import { StyleSheet, View } from 'react-native';
6+
import withTheme from '../core/withTheme';
7+
import type { Theme } from '../types';
8+
import { black, white } from '../styles/colors';
59

610
type Props = {
711
/**
812
* Whether divider has a left inset
913
*/
1014
inset?: boolean,
1115
style?: any,
16+
theme: Theme,
1217
};
1318

1419
/**
@@ -27,20 +32,36 @@ type Props = {
2732
* ```
2833
*/
2934
const Divider = (props: Props) => {
30-
const { inset, style } = props;
35+
const { inset, style, theme } = props;
36+
const { dark: isDarkTheme } = theme;
3137
return (
32-
<View {...props} style={[styles.divider, inset && styles.inset, style]} />
38+
<View
39+
{...props}
40+
style={[
41+
isDarkTheme ? styles.dividerDarkTheme : styles.dividerDeafultTheme,
42+
inset && styles.inset,
43+
style,
44+
]}
45+
/>
3346
);
3447
};
3548

3649
const styles = StyleSheet.create({
37-
divider: {
38-
backgroundColor: 'rgba(0, 0, 0, .12)',
50+
dividerDeafultTheme: {
51+
backgroundColor: color(black)
52+
.alpha(0.12)
53+
.rgbaString(),
54+
height: StyleSheet.hairlineWidth,
55+
},
56+
dividerDarkTheme: {
57+
backgroundColor: color(white)
58+
.alpha(0.12)
59+
.rgbaString(),
3960
height: StyleSheet.hairlineWidth,
4061
},
4162
inset: {
4263
marginLeft: 72,
4364
},
4465
});
4566

46-
export default Divider;
67+
export default withTheme(Divider);

src/components/Switch.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
2-
32
import * as React from 'react';
4-
import { grey400, grey50 } from '../styles/colors';
3+
4+
import { grey400, grey800, grey50, white, black } from '../styles/colors';
55
import { View, Switch, Platform } from 'react-native';
66
import withTheme from '../core/withTheme';
77
import setColor from 'color';
@@ -63,25 +63,34 @@ class SwitchRow extends React.Component<Props> {
6363
...props
6464
} = this.props;
6565

66+
const { dark: isDarkTheme } = theme;
67+
6668
const checkedColor = color || theme.colors.accent;
6769

6870
const trackTintColor =
6971
Platform.OS === 'ios'
7072
? checkedColor
7173
: disabled
72-
? setColor(grey400)
73-
.alpha(0.38)
74-
.rgb()
75-
.string()
74+
? isDarkTheme
75+
? setColor(white)
76+
.alpha(0.1)
77+
.rgb()
78+
.string()
79+
: setColor(black)
80+
.alpha(0.12)
81+
.rgb()
82+
.string()
7683
: setColor(checkedColor)
77-
.alpha(0.38)
84+
.alpha(0.5)
7885
.rgb()
7986
.string();
8087

8188
const trackThumbTintColor =
8289
Platform.OS === 'ios'
8390
? undefined
84-
: disabled ? grey400 : value ? checkedColor : grey50;
91+
: disabled
92+
? isDarkTheme ? grey800 : grey400
93+
: value ? checkedColor : isDarkTheme ? grey400 : grey50;
8594

8695
return (
8796
<View>

0 commit comments

Comments
 (0)