Skip to content

Commit b697cf1

Browse files
Chris Bobbegnprice
Chris Bobbe
authored andcommitted
ChatScreen: Switch to new React Context API for theme colors.
Per #1946, we're moving to the new React Context API (https://reactjs.org/docs/context.html). As in the parent commit, for Screen, separate the constant parts of the style from those that change with the theme; the latter get computed in the render method. Differing from the parent commit, put the constant parts of the style in an instance field, rather than an outer-level `const` declaration. We haven't settled the question universally; in discussion [1], Greg points out, """ There's a tension between two things that are good to have: * The render method, particularly the blob of JSX that's typically found all at the end, is where we describe the visual look of the component. It'd be good for the description of its visual look to be all in one place, or at least reasonably close together, and the styles are part of that. * The styles are often kind of long with a lot of fairly boring stuff, and it's good to be able to see all the other stuff going on in `render` -- often including logic about what to show at all, or what data to use -- without the more boring of the styles details pushing it off the screen. """ Putting it an instance property means it's closer at hand than at a `const` above the class definition, so, slightly favor that pattern by using it in this file, where there's no precedent (or there was a precedent, and it was recently removed in 8621b61, before this series of commits). [1]: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/Styles.20in.20component.20files/near/860528
1 parent ad429e3 commit b697cf1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/chat/ChatScreen.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import type { NavigationScreenProp } from 'react-navigation';
55
import { ActionSheetProvider } from '@expo/react-native-action-sheet';
66

77
import { connect } from '../react-redux';
8-
import type { Context, Dispatch, Fetching, Narrow, EditMessage } from '../types';
8+
import type { ThemeColors } from '../styles';
9+
import styles, { ThemeContext } from '../styles';
10+
import type { Dispatch, Fetching, Narrow, EditMessage } from '../types';
911
import { KeyboardAvoider, OfflineNotice, ZulipStatusBar } from '../common';
1012
import ChatNavBar from '../nav/ChatNavBar';
1113

1214
import MessageList from '../webview/MessageList';
1315
import NoMessages from '../message/NoMessages';
1416
import ComposeBox from '../compose/ComposeBox';
1517
import UnreadNotice from './UnreadNotice';
16-
import styles from '../styles';
1718
import { canSendToNarrow } from '../utils/narrow';
1819
import { getLoading } from '../directSelectors';
1920
import { getFetchingForNarrow } from './fetchingSelectors';
@@ -37,14 +38,18 @@ type State = {|
3738
|};
3839

3940
class ChatScreen extends PureComponent<Props, State> {
40-
context: Context;
41+
static contextType = ThemeContext;
42+
context: ThemeColors;
4143

4244
state = {
4345
editMessage: null,
4446
};
4547

46-
static contextTypes = {
47-
styles: () => null,
48+
styles = {
49+
screen: {
50+
flex: 1,
51+
flexDirection: 'column',
52+
},
4853
};
4954

5055
startEditMessage = (editMessage: EditMessage) => {
@@ -56,7 +61,6 @@ class ChatScreen extends PureComponent<Props, State> {
5661
};
5762

5863
render() {
59-
const { styles: contextStyles } = this.context;
6064
const { fetching, haveNoMessages, loading, navigation } = this.props;
6165
const { narrow } = navigation.state.params;
6266
const { editMessage } = this.state;
@@ -68,7 +72,7 @@ class ChatScreen extends PureComponent<Props, State> {
6872

6973
return (
7074
<ActionSheetProvider>
71-
<View style={contextStyles.screen}>
75+
<View style={[this.styles.screen, { backgroundColor: this.context.backgroundColor }]}>
7276
<KeyboardAvoider style={styles.flexed} behavior="padding">
7377
<ZulipStatusBar narrow={narrow} />
7478
<ChatNavBar narrow={narrow} editMessage={editMessage} />

0 commit comments

Comments
 (0)