Skip to content

Commit 51dd1b3

Browse files
committed
styles: Pass core theme data down as new-style context.
And demonstrate with one example component and style object, `LineSeparator` and `styles.lineSeparator`. To combine the theme data with details like height and margin that are about the component and not the theme, use the same pattern introduced in a2bfcb4 on ComposeBox: a class initializer.
1 parent 6e80979 commit 51dd1b3

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

src/boot/StylesProvider.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React, { PureComponent } from 'react';
55

66
import type { ChildrenArray, GlobalState, ThemeName } from '../types';
77
import { getSettings } from '../directSelectors';
8-
import { stylesFromTheme } from '../styles/theme';
8+
import { stylesFromTheme, themeColors, ThemeContext } from '../styles/theme';
99

1010
const Dummy = props => props.children;
1111

@@ -32,7 +32,11 @@ class StyleProvider extends PureComponent<Props> {
3232
render() {
3333
const { children, theme } = this.props;
3434

35-
return <Dummy key={theme}>{children}</Dummy>;
35+
return (
36+
<ThemeContext.Provider value={themeColors[theme]}>
37+
<Dummy key={theme}>{children}</Dummy>
38+
</ThemeContext.Provider>
39+
);
3640
}
3741
}
3842

src/common/LineSeparator.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
import React, { PureComponent } from 'react';
33
import { View } from 'react-native';
44

5-
import type { Context } from '../types';
5+
import type { ThemeColors } from '../styles';
6+
import { ThemeContext } from '../styles';
67

78
export default class LineSeparator extends PureComponent<{}> {
8-
context: Context;
9+
static contextType = ThemeContext;
10+
context: ThemeColors;
911

10-
static contextTypes = {
11-
styles: () => null,
12+
styles = {
13+
lineSeparator: {
14+
height: 1,
15+
backgroundColor: this.context.cardColor,
16+
margin: 4,
17+
},
1218
};
1319

1420
render() {
15-
return <View style={this.context.styles.lineSeparator} />;
21+
return <View style={this.styles.lineSeparator} />;
1622
}
1723
}

src/styles/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { statics as navStyles } from './navStyles';
77
import utilityStyles from './utilityStyles';
88

99
export * from './constants';
10+
export type { ThemeColors } from './theme';
11+
export { ThemeContext } from './theme';
1012

1113
export default StyleSheet.create({
1214
...composeBoxStyles,

src/styles/miscStyles.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const statics = {
5858
},
5959
};
6060

61-
export default ({ color, backgroundColor, cardColor, dividerColor }: ThemeColors) => ({
61+
export default ({ color, backgroundColor, dividerColor }: ThemeColors) => ({
6262
color: {
6363
color,
6464
},
@@ -105,9 +105,4 @@ export default ({ color, backgroundColor, cardColor, dividerColor }: ThemeColors
105105
borderBottomWidth: 1,
106106
borderBottomColor: dividerColor,
107107
},
108-
lineSeparator: {
109-
height: 1,
110-
backgroundColor: cardColor,
111-
margin: 4,
112-
},
113108
});

src/styles/theme.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* @flow strict-local */
2+
import React from 'react';
23
import { StyleSheet } from 'react-native';
34

45
import type { ThemeName } from '../types';
@@ -17,7 +18,7 @@ export type AppStyles = $ReadOnly<{|
1718
...$Call<typeof miscStyles, ThemeColors>,
1819
|}>;
1920

20-
const themeColors: { [string]: ThemeColors } = {
21+
export const themeColors: { [string]: ThemeColors } = {
2122
night: {
2223
color: '#d5d9dd',
2324
backgroundColor: '#212D3B',
@@ -37,6 +38,8 @@ const themeColors: { [string]: ThemeColors } = {
3738
};
3839
themeColors.default = themeColors.light;
3940

41+
export const ThemeContext = React.createContext(themeColors.default);
42+
4043
export const stylesFromTheme = (name: ThemeName) => {
4144
const colors = themeColors[name];
4245
return StyleSheet.create({

0 commit comments

Comments
 (0)