From b86e2e6715b1fab516916911166336b5aa69b969 Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Fri, 11 Oct 2019 14:04:17 +0200 Subject: [PATCH] feat: add headerTitleAlign option to center and left align title --- .../NestedNavigator.test.tsx.snap | 6 +-- .../StackNavigator.test.tsx.snap | 9 ++-- src/types.tsx | 1 + src/views/Header/HeaderSegment.tsx | 42 +++++++++++++------ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/navigators/__tests__/__snapshots__/NestedNavigator.test.tsx.snap b/src/navigators/__tests__/__snapshots__/NestedNavigator.test.tsx.snap index 35dc584b8..1a80e8cf6 100644 --- a/src/navigators/__tests__/__snapshots__/NestedNavigator.test.tsx.snap +++ b/src/navigators/__tests__/__snapshots__/NestedNavigator.test.tsx.snap @@ -444,8 +444,7 @@ Array [ pointerEvents="box-none" style={ Array [ - null, - Object {}, + false, Object { "opacity": undefined, }, @@ -585,8 +584,7 @@ Array [ pointerEvents="box-none" style={ Array [ - null, - Object {}, + false, Object { "opacity": undefined, }, diff --git a/src/navigators/__tests__/__snapshots__/StackNavigator.test.tsx.snap b/src/navigators/__tests__/__snapshots__/StackNavigator.test.tsx.snap index aa33c2c5e..ea7cede06 100644 --- a/src/navigators/__tests__/__snapshots__/StackNavigator.test.tsx.snap +++ b/src/navigators/__tests__/__snapshots__/StackNavigator.test.tsx.snap @@ -279,8 +279,7 @@ Array [ pointerEvents="box-none" style={ Array [ - null, - Object {}, + false, Object { "opacity": undefined, }, @@ -321,6 +320,9 @@ Array [ "right": 0, "top": 0, }, + Object { + "right": 0, + }, Object { "opacity": undefined, }, @@ -616,8 +618,7 @@ Array [ pointerEvents="box-none" style={ Array [ - null, - Object {}, + false, Object { "opacity": undefined, }, diff --git a/src/types.tsx b/src/types.tsx index e90e5f605..3fefd1694 100644 --- a/src/types.tsx +++ b/src/types.tsx @@ -71,6 +71,7 @@ export type HeaderOptions = { headerTitle?: | React.ReactNode | ((props: HeaderTitleProps) => React.ReactNode); + headerTitleAlign?: 'left' | 'center'; headerTitleStyle?: StyleProp; headerTitleContainerStyle?: StyleProp; headerTintColor?: string; diff --git a/src/views/Header/HeaderSegment.tsx b/src/views/Header/HeaderSegment.tsx index 1cb559e56..a1ee88114 100644 --- a/src/views/Header/HeaderSegment.tsx +++ b/src/views/Header/HeaderSegment.tsx @@ -38,6 +38,13 @@ type State = { leftLabelLayout?: Layout; }; +const DEFAULT_INSETS = { + top: 0, + left: 0, + right: 0, + bottom: 0, +}; + const warnIfHeaderStylesDefined = (styles: { [key: string]: any }) => { Object.keys(styles).forEach(styleProp => { const value = styles[styleProp]; @@ -143,6 +150,10 @@ export default class HeaderSegment extends React.Component { leftLabel: previousTitle, onGoBack, headerTitle, + headerTitleAlign = Platform.select({ + ios: 'center', + default: 'left', + }), headerLeft: left = onGoBack ? (props: HeaderBackButtonProps) => : undefined, @@ -185,7 +196,9 @@ export default class HeaderSegment extends React.Component { previousTitle ? leftLabelLayout : undefined ); - const statusBarHeight = this.context ? this.context.top : 0; + const insets = this.context || DEFAULT_INSETS; + + const statusBarHeight = insets.top; const { height = getDefaultHeaderHeight(layout, this.context), @@ -324,7 +337,12 @@ export default class HeaderSegment extends React.Component { {leftButton ? ( {leftButton} @@ -332,11 +350,10 @@ export default class HeaderSegment extends React.Component { { {right !== undefined ? ( {typeof right === 'function' ? right({ @@ -393,8 +415,4 @@ const styles = StyleSheet.create({ justifyContent: 'center', alignItems: 'flex-end', }, - title: Platform.select({ - ios: {}, - default: { position: 'absolute' }, - }), });