Skip to content

Commit 173cee4

Browse files
Chris Bobbechrisbobbe
Chris Bobbe
authored andcommitted
Screen: Switch to new React Context API for theme colors.
Per zulip#1946, we're moving to the new React Context API (https://reactjs.org/docs/context.html). For details like height, margin, and flex that are about the component and not the theme, put these in the existing `componentStyles` object. A functional difference between this pattern and the one in, e.g., a2bfcb4 and 51dd1b3, is that, here, we don't group the theme-dependent styles together with the constant ones. If we don't compute the theme-dependent styles in the `render` method, then the component's appearance won't live-update when the theme is changed. A universal choice between the non-functional difference between using an instance field and an outer-level `const` declaration hasn't been made, but there's been some fruitful discussion [1], quoted in the next commit. Here, we just go with the existing `const` declaration. In practice, the lack of live-updating has probably never been observed, as a side-effect of the `key={theme}` hack in src/boot/StylesProvider.js, which causes most of the entire tree to be remounted (not rerendered, remounted) when the theme changes. This hack is explained in docs/architecture/react.md#context, and it will be removed at the end of this series of commits, since the new Context API makes it unnecessary. Before that happens, the latent bugs in a2bfcb4 and 51dd1b3 (mentioned above), and elsewhere, will be fixed. [1]: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/Styles.20in.20component.20files/near/860528
1 parent f9ce323 commit 173cee4

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/common/Screen.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import type { Node as React$Node } from 'react';
55
import { StyleSheet, View, ScrollView } from 'react-native';
66
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
77

8-
import type { Context, Dimensions, LocalizableText, Dispatch } from '../types';
8+
import type { ThemeColors } from '../styles';
9+
import styles, { ThemeContext } from '../styles';
10+
import type { Dimensions, LocalizableText, Dispatch } from '../types';
911
import { connect } from '../react-redux';
1012
import KeyboardAvoider from './KeyboardAvoider';
1113
import OfflineNotice from './OfflineNotice';
@@ -14,9 +16,12 @@ import ZulipStatusBar from './ZulipStatusBar';
1416
import { getSession } from '../selectors';
1517
import ModalNavBar from '../nav/ModalNavBar';
1618
import ModalSearchNavBar from '../nav/ModalSearchNavBar';
17-
import styles from '../styles';
1819

1920
const componentStyles = StyleSheet.create({
21+
screen: {
22+
flex: 1,
23+
flexDirection: 'column',
24+
},
2025
wrapper: {
2126
flex: 1,
2227
justifyContent: 'center',
@@ -72,11 +77,8 @@ type Props = $ReadOnly<{|
7277
* Required unless `search` is true.
7378
*/
7479
class Screen extends PureComponent<Props> {
75-
context: Context;
76-
77-
static contextTypes = {
78-
styles: () => null,
79-
};
80+
static contextType = ThemeContext;
81+
context: ThemeColors;
8082

8183
static defaultProps = {
8284
centerContent: false,
@@ -109,10 +111,15 @@ class Screen extends PureComponent<Props> {
109111
title,
110112
shouldShowLoadingBanner,
111113
} = this.props;
112-
const { styles: contextStyles } = this.context;
113114

114115
return (
115-
<View style={[contextStyles.screen, { paddingBottom: safeAreaInsets.bottom }]}>
116+
<View
117+
style={[
118+
componentStyles.screen,
119+
{ backgroundColor: this.context.backgroundColor },
120+
{ paddingBottom: safeAreaInsets.bottom },
121+
]}
122+
>
116123
<ZulipStatusBar />
117124
{search ? (
118125
<ModalSearchNavBar autoFocus={autoFocus} searchBarOnChange={searchBarOnChange} />

0 commit comments

Comments
 (0)