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

Commit

Permalink
feat: add headerTitleAlign option to center and left align title
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Oct 11, 2019
1 parent 2d5f6c3 commit b86e2e6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ Array [
pointerEvents="box-none"
style={
Array [
null,
Object {},
false,
Object {
"opacity": undefined,
},
Expand Down Expand Up @@ -585,8 +584,7 @@ Array [
pointerEvents="box-none"
style={
Array [
null,
Object {},
false,
Object {
"opacity": undefined,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ Array [
pointerEvents="box-none"
style={
Array [
null,
Object {},
false,
Object {
"opacity": undefined,
},
Expand Down Expand Up @@ -321,6 +320,9 @@ Array [
"right": 0,
"top": 0,
},
Object {
"right": 0,
},
Object {
"opacity": undefined,
},
Expand Down Expand Up @@ -616,8 +618,7 @@ Array [
pointerEvents="box-none"
style={
Array [
null,
Object {},
false,
Object {
"opacity": undefined,
},
Expand Down
1 change: 1 addition & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type HeaderOptions = {
headerTitle?:
| React.ReactNode
| ((props: HeaderTitleProps) => React.ReactNode);
headerTitleAlign?: 'left' | 'center';
headerTitleStyle?: StyleProp<TextStyle>;
headerTitleContainerStyle?: StyleProp<ViewStyle>;
headerTintColor?: string;
Expand Down
42 changes: 30 additions & 12 deletions src/views/Header/HeaderSegment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -143,6 +150,10 @@ export default class HeaderSegment extends React.Component<Props, State> {
leftLabel: previousTitle,
onGoBack,
headerTitle,
headerTitleAlign = Platform.select({
ios: 'center',
default: 'left',
}),
headerLeft: left = onGoBack
? (props: HeaderBackButtonProps) => <HeaderBackButton {...props} />
: undefined,
Expand Down Expand Up @@ -185,7 +196,9 @@ export default class HeaderSegment extends React.Component<Props, State> {
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),
Expand Down Expand Up @@ -324,19 +337,23 @@ export default class HeaderSegment extends React.Component<Props, State> {
{leftButton ? (
<Animated.View
pointerEvents="box-none"
style={[styles.left, leftButtonStyle, leftContainerStyle]}
style={[
styles.left,
{ left: insets.left },
leftButtonStyle,
leftContainerStyle,
]}
>
{leftButton}
</Animated.View>
) : null}
<Animated.View
pointerEvents="box-none"
style={[
Platform.select({
ios: null,
default: { left: leftButton ? 72 : 16 },
}),
styles.title,
headerTitleAlign === 'left' && {
position: 'absolute',
left: leftButton ? 72 : 16,
},
titleStyle,
titleContainerStyle,
]}
Expand All @@ -353,7 +370,12 @@ export default class HeaderSegment extends React.Component<Props, State> {
{right !== undefined ? (
<Animated.View
pointerEvents="box-none"
style={[styles.right, rightButtonStyle, rightContainerStyle]}
style={[
styles.right,
{ right: insets.right },
rightButtonStyle,
rightContainerStyle,
]}
>
{typeof right === 'function'
? right({
Expand Down Expand Up @@ -393,8 +415,4 @@ const styles = StyleSheet.create({
justifyContent: 'center',
alignItems: 'flex-end',
},
title: Platform.select({
ios: {},
default: { position: 'absolute' },
}),
});

0 comments on commit b86e2e6

Please sign in to comment.