Skip to content

Commit 4e524e8

Browse files
committed
nav [nfc]: Factor out NavBarBackButton into one place.
This already would have been good, but after adding the accessibilityLabel, this bit of code is definitely complex enough we want to avoid duplicating it.
1 parent 7a17dd6 commit 4e524e8

File tree

4 files changed

+40
-37
lines changed

4 files changed

+40
-37
lines changed

src/nav/ChatNavBar.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ import React, { PureComponent } from 'react';
44
import { View } from 'react-native';
55
import Color from 'color';
66

7-
import * as NavigationService from './NavigationService';
87
import type { Dispatch, Narrow, EditMessage } from '../types';
98
import { LoadingBanner } from '../common';
109
import { connect } from '../react-redux';
1110
import { BRAND_COLOR, NAVBAR_SIZE } from '../styles';
1211
import Title from '../title/Title';
13-
import NavButton from './NavButton';
12+
import NavBarBackButton from './NavBarBackButton';
1413
import { DEFAULT_TITLE_BACKGROUND_COLOR, getTitleBackgroundColor } from '../title/titleSelectors';
1514
import { foregroundColorFromBackground } from '../utils/color';
16-
import { navigateBack } from '../actions';
1715
import { ExtraButton, InfoButton } from '../title-buttons/titleButtonFromNarrow';
1816

1917
type SelectorProps = {|
@@ -60,14 +58,7 @@ class ChatNavBar extends PureComponent<Props> {
6058
{ backgroundColor },
6159
]}
6260
>
63-
<NavButton
64-
name="arrow-left"
65-
accessibilityLabel="Navigate up"
66-
color={color}
67-
onPress={() => {
68-
NavigationService.dispatch(navigateBack());
69-
}}
70-
/>
61+
<NavBarBackButton color={color} />
7162
<Title color={color} narrow={narrow} editMessage={editMessage} />
7263
<ExtraButton color={color} narrow={narrow} />
7364
<InfoButton color={color} narrow={narrow} />

src/nav/ModalNavBar.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
import React, { PureComponent } from 'react';
44
import { View } from 'react-native';
55

6-
import * as NavigationService from './NavigationService';
76
import type { LocalizableText } from '../types';
87
import type { ThemeData } from '../styles';
98
import styles, { ThemeContext, NAVBAR_SIZE } from '../styles';
10-
119
import Label from '../common/Label';
12-
import NavButton from './NavButton';
13-
import { navigateBack } from '../actions';
10+
import NavBarBackButton from './NavBarBackButton';
1411

1512
type Props = $ReadOnly<{|
1613
canGoBack: boolean,
@@ -41,15 +38,7 @@ class ModalNavBar extends PureComponent<Props> {
4138
},
4239
]}
4340
>
44-
{canGoBack && (
45-
<NavButton
46-
name="arrow-left"
47-
accessibilityLabel="Navigate up"
48-
onPress={() => {
49-
NavigationService.dispatch(navigateBack());
50-
}}
51-
/>
52-
)}
41+
{canGoBack && <NavBarBackButton />}
5342
<View style={styles.flexedLeftAlign}>
5443
<Label style={textStyle} text={title} numberOfLines={1} ellipsizeMode="tail" />
5544
</View>

src/nav/ModalSearchNavBar.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import React, { PureComponent } from 'react';
33
import { View } from 'react-native';
44

5-
import * as NavigationService from './NavigationService';
65
import type { ThemeData } from '../styles';
76
import { ThemeContext, NAVBAR_SIZE } from '../styles';
87
import SearchInput from '../common/SearchInput';
9-
import NavButton from './NavButton';
10-
import { navigateBack } from '../actions';
8+
import NavBarBackButton from './NavBarBackButton';
119

1210
type Props = $ReadOnly<{|
1311
autoFocus: boolean,
@@ -36,16 +34,7 @@ class ModalSearchNavBar extends PureComponent<Props> {
3634
backgroundColor: this.context.backgroundColor,
3735
}}
3836
>
39-
{canGoBack && (
40-
<NavButton
41-
name="arrow-left"
42-
accessibilityLabel="Navigate up"
43-
onPress={() => {
44-
NavigationService.dispatch(navigateBack());
45-
}}
46-
/>
47-
)}
48-
37+
{canGoBack && <NavBarBackButton />}
4938
<SearchInput autoFocus={autoFocus} onChangeText={searchBarOnChange} />
5039
</View>
5140
);

src/nav/NavBarBackButton.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* @flow strict-local */
2+
import React from 'react';
3+
4+
import { navigateBack } from '../actions';
5+
import NavButton from './NavButton';
6+
import * as NavigationService from './NavigationService';
7+
8+
/**
9+
* The button for the start of the app bar, to return to previous screen.
10+
*
11+
* This serves as what Android calls the "Up button", and iOS calls the
12+
* "back button". It navigates back to the previous screen within the app.
13+
*
14+
* For background, see Android, iOS, and Material guidance on navigation:
15+
* https://developer.android.com/guide/navigation/navigation-principles
16+
* https://developer.apple.com/design/human-interface-guidelines/ios/app-architecture/navigation/
17+
* https://developer.apple.com/design/human-interface-guidelines/ios/bars/navigation-bars/
18+
* https://material.io/design/navigation/understanding-navigation.html
19+
*/
20+
// TODO: on iOS, show the right icon for a back button
21+
// TODO: on iOS, give the right label for a back button
22+
export default function NavBarBackButton(props: {| +color?: string |}) {
23+
const { color } = props;
24+
return (
25+
<NavButton
26+
name="arrow-left"
27+
accessibilityLabel="Navigate up"
28+
color={color}
29+
onPress={() => {
30+
NavigationService.dispatch(navigateBack());
31+
}}
32+
/>
33+
);
34+
}

0 commit comments

Comments
 (0)