Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit b86e2e6

Browse files
committed
feat: add headerTitleAlign option to center and left align title
1 parent 2d5f6c3 commit b86e2e6

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

src/navigators/__tests__/__snapshots__/NestedNavigator.test.tsx.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ Array [
444444
pointerEvents="box-none"
445445
style={
446446
Array [
447-
null,
448-
Object {},
447+
false,
449448
Object {
450449
"opacity": undefined,
451450
},
@@ -585,8 +584,7 @@ Array [
585584
pointerEvents="box-none"
586585
style={
587586
Array [
588-
null,
589-
Object {},
587+
false,
590588
Object {
591589
"opacity": undefined,
592590
},

src/navigators/__tests__/__snapshots__/StackNavigator.test.tsx.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,7 @@ Array [
279279
pointerEvents="box-none"
280280
style={
281281
Array [
282-
null,
283-
Object {},
282+
false,
284283
Object {
285284
"opacity": undefined,
286285
},
@@ -321,6 +320,9 @@ Array [
321320
"right": 0,
322321
"top": 0,
323322
},
323+
Object {
324+
"right": 0,
325+
},
324326
Object {
325327
"opacity": undefined,
326328
},
@@ -616,8 +618,7 @@ Array [
616618
pointerEvents="box-none"
617619
style={
618620
Array [
619-
null,
620-
Object {},
621+
false,
621622
Object {
622623
"opacity": undefined,
623624
},

src/types.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export type HeaderOptions = {
7171
headerTitle?:
7272
| React.ReactNode
7373
| ((props: HeaderTitleProps) => React.ReactNode);
74+
headerTitleAlign?: 'left' | 'center';
7475
headerTitleStyle?: StyleProp<TextStyle>;
7576
headerTitleContainerStyle?: StyleProp<ViewStyle>;
7677
headerTintColor?: string;

src/views/Header/HeaderSegment.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ type State = {
3838
leftLabelLayout?: Layout;
3939
};
4040

41+
const DEFAULT_INSETS = {
42+
top: 0,
43+
left: 0,
44+
right: 0,
45+
bottom: 0,
46+
};
47+
4148
const warnIfHeaderStylesDefined = (styles: { [key: string]: any }) => {
4249
Object.keys(styles).forEach(styleProp => {
4350
const value = styles[styleProp];
@@ -143,6 +150,10 @@ export default class HeaderSegment extends React.Component<Props, State> {
143150
leftLabel: previousTitle,
144151
onGoBack,
145152
headerTitle,
153+
headerTitleAlign = Platform.select({
154+
ios: 'center',
155+
default: 'left',
156+
}),
146157
headerLeft: left = onGoBack
147158
? (props: HeaderBackButtonProps) => <HeaderBackButton {...props} />
148159
: undefined,
@@ -185,7 +196,9 @@ export default class HeaderSegment extends React.Component<Props, State> {
185196
previousTitle ? leftLabelLayout : undefined
186197
);
187198

188-
const statusBarHeight = this.context ? this.context.top : 0;
199+
const insets = this.context || DEFAULT_INSETS;
200+
201+
const statusBarHeight = insets.top;
189202

190203
const {
191204
height = getDefaultHeaderHeight(layout, this.context),
@@ -324,19 +337,23 @@ export default class HeaderSegment extends React.Component<Props, State> {
324337
{leftButton ? (
325338
<Animated.View
326339
pointerEvents="box-none"
327-
style={[styles.left, leftButtonStyle, leftContainerStyle]}
340+
style={[
341+
styles.left,
342+
{ left: insets.left },
343+
leftButtonStyle,
344+
leftContainerStyle,
345+
]}
328346
>
329347
{leftButton}
330348
</Animated.View>
331349
) : null}
332350
<Animated.View
333351
pointerEvents="box-none"
334352
style={[
335-
Platform.select({
336-
ios: null,
337-
default: { left: leftButton ? 72 : 16 },
338-
}),
339-
styles.title,
353+
headerTitleAlign === 'left' && {
354+
position: 'absolute',
355+
left: leftButton ? 72 : 16,
356+
},
340357
titleStyle,
341358
titleContainerStyle,
342359
]}
@@ -353,7 +370,12 @@ export default class HeaderSegment extends React.Component<Props, State> {
353370
{right !== undefined ? (
354371
<Animated.View
355372
pointerEvents="box-none"
356-
style={[styles.right, rightButtonStyle, rightContainerStyle]}
373+
style={[
374+
styles.right,
375+
{ right: insets.right },
376+
rightButtonStyle,
377+
rightContainerStyle,
378+
]}
357379
>
358380
{typeof right === 'function'
359381
? right({
@@ -393,8 +415,4 @@ const styles = StyleSheet.create({
393415
justifyContent: 'center',
394416
alignItems: 'flex-end',
395417
},
396-
title: Platform.select({
397-
ios: {},
398-
default: { position: 'absolute' },
399-
}),
400418
});

0 commit comments

Comments
 (0)