Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed badge animation #71

Merged
merged 18 commits into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Don't skip this part. You will be happy to know about all the good stuff you can
| **`onPress`** | Function to be called when the Tab was pressed. **When you use this, the pressed tab won't be active automatically. You need to set it to active by updating `BottomNavigation.activeTab`.** This function will be called with the parameter `(newTabIndex) => {}` | `function` | – |
| **`badgeText`** | Text for the tab's badge. **The badge will be hidden if no badgeText is passed. isBadgeVisible can be used to override this**. | `string` | - |
| **`badgeSize`** | Size of the badge. Will be used to calculate the height, width, and border radius (height: size, width: size, borderRadius: size/2) | `number` | 20 |
| **`badgeStyle`** | Style for the badge. `badgeStyle.container` will be used to determine the badge's container style, and `badgeStyle.text` will be used to determine the badge's text style | `object` | `{ container: { position: "absolute", width: 20, height: 20, borderRadius: 20 / 2, alignItems: "center", justifyContent: "center", backgroundColor: "#F50057", zIndex: 9999, bottom: 10 }, text: { color: "#fff", fontWeight: "500", fontSize: 12 } }` |
| **`badgeStyle`** | Style for the badge. `badgeStyle.container` will be used to determine the badge's container style, and `badgeStyle.text` will be used to determine the badge's text style | `object` | `{ container: { position: "absolute", width: 20, height: 20, borderRadius: 20 / 2, alignItems: "center", justifyContent: "center", backgroundColor: "#F50057", zIndex: 9999, top: 3, right: 27 }, text: { color: "#fff", fontWeight: "500", fontSize: 12 } }` |
| **`isBadgeVisible`** | Determines if the badge is visible or not | `boolean` | - |

## Behind the Navigation Bar
Expand Down
22 changes: 14 additions & 8 deletions lib/Badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import React, { Component } from 'react'
import {
View,
Text,
StyleSheet
StyleSheet,
Animated
} from 'react-native';

type BProps = {
Expand All @@ -16,6 +17,7 @@ type BProps = {
children: Array<ReactElement<*>>,
style: any,
isVisible: boolean,
translateY: number,
}

const defaultProps = {
Expand All @@ -42,7 +44,8 @@ export default class Badge extends Component {
justifyContent: "center",
backgroundColor: "#F50057",
zIndex: 9999,
bottom: 10,
top: 3,
right: 27,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an active Tab, this position causes the Badge to be far away from the Icon. This happens because it's a fixed value, but the position depends on the screen dimension, shifting, and Tab amount.

image

I would suggest something like left: '50%', marginLeft: 15. This way, it's positioned in the center of the Tab and then moved to the left. Looks like this:

image

Feel free to use a bigger/smaller value for marginLeft – depending on how you think it should be positioned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

},
style.container
);
Expand All @@ -64,18 +67,21 @@ export default class Badge extends Component {
}

render() {
const { children, text, isVisible } = this.props;
const { children, text, isVisible, translateY } = this.props;
const styles = this.createStyles();
if (!isVisible) {
return null;
}

return (
<View>
<View style={styles.container}>
<Text style={styles.text}>{text}</Text>
</View>
</View>
<Animated.View
style={[
styles.container,
{ transform: [{ translateY: translateY }] },
]}
>
<Text style={styles.text}>{text}</Text>
</Animated.View>
)
}
}
14 changes: 8 additions & 6 deletions lib/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,20 @@ export default class Tab extends Component {

_renderBadge = () => {
const { badgeText, badgeSize, badgeStyle, isBadgeVisible } = this.props;
const mode = this._getModeString();

if (badgeText === undefined && !isBadgeVisible) {
return null;
}

return (
<Badge
text={badgeText}
size={badgeSize}
style={badgeStyle}
isVisible={isBadgeVisible}
/>
<Badge
text={badgeText}
size={badgeSize}
style={badgeStyle}
isVisible={isBadgeVisible}
translateY={this.state[mode].iconY}
/>
)
}

Expand Down