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

Commit

Permalink
fix: make sure left button isn't bigger than screen width / 2
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 18, 2019
1 parent d2397d5 commit ebc4865
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions packages/stack/src/views/Header/HeaderBackButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ class HeaderBackButton extends React.Component<Props, State> {
});
};

private renderBackImage() {
private shouldTruncateLabel = () => {
const { titleLayout, screenLayout, label } = this.props;
const { initialLabelWidth } = this.state;

return (
!label ||
(initialLabelWidth &&
titleLayout &&
screenLayout &&
(screenLayout.width - titleLayout.width) / 2 < initialLabelWidth + 26)
);
};

private renderBackImage = () => {
const { backImage, labelVisible, tintColor } = this.props;

if (backImage) {
Expand All @@ -68,29 +81,12 @@ class HeaderBackButton extends React.Component<Props, State> {
/>
);
}
}

private getLabelText = () => {
const { titleLayout, screenLayout, label, truncatedLabel } = this.props;

let { initialLabelWidth } = this.state;

if (!label) {
return truncatedLabel;
} else if (
initialLabelWidth &&
titleLayout &&
screenLayout &&
(screenLayout.width - titleLayout.width) / 2 < initialLabelWidth + 26
) {
return truncatedLabel;
} else {
return label;
}
};

private renderLabel() {
const {
label,
truncatedLabel,
allowFontScaling,
labelVisible,
backImage,
Expand All @@ -99,33 +95,32 @@ class HeaderBackButton extends React.Component<Props, State> {
screenLayout,
} = this.props;

let leftLabelText = this.getLabelText();
const leftLabelText = this.shouldTruncateLabel() ? truncatedLabel : label;

if (!labelVisible || leftLabelText === undefined) {
return null;
}

const label = (
const labelElement = (
<Animated.Text
accessible={false}
onLayout={this.handleLabelLayout}
style={[
styles.label,
screenLayout ? { marginRight: screenLayout.width / 2 } : null,
tintColor ? { color: tintColor } : null,
labelStyle,
]}
numberOfLines={1}
allowFontScaling={!!allowFontScaling}
>
{this.getLabelText()}
{leftLabelText}
</Animated.Text>
);

if (backImage) {
if (backImage || Platform.OS !== 'ios') {
// When a custom backimage is specified, we can't mask the label
// Otherwise there might be weird effect due to our mask not being the same as the image
return label;
return labelElement;
}

return (
Expand All @@ -140,7 +135,17 @@ class HeaderBackButton extends React.Component<Props, State> {
</View>
}
>
{label}
<View
style={
screenLayout
? // We make the button extend till the middle of the screen
// Otherwise it appears to cut off when translating
[styles.labelWrapper, { minWidth: screenLayout.width / 2 - 27 }]
: null
}
>
{labelElement}
</View>
</MaskedViewIOS>
);
}
Expand Down Expand Up @@ -202,6 +207,12 @@ const styles = StyleSheet.create({
// Adjusting the letterSpacing makes them coincide better
letterSpacing: 0.35,
},
labelWrapper: {
// These styles will make sure that the label doesn't fill the available space
// Otherwise it messes with the measurement of the label
flexDirection: 'row',
alignItems: 'flex-start',
},
icon: Platform.select({
ios: {
height: 21,
Expand Down

0 comments on commit ebc4865

Please sign in to comment.