Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit a620665

Browse files
ShayanJavaditimomeh
authored andcommitted
Fixed badge animation (#71)
* animation fix animation fix * animation fix animation fix * updated readme updated readme * changed fixed position value to dynamic changed fixed position value to dynamic * updated readme with new styles updated readme with new styles
1 parent a8b7b08 commit a620665

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Don't skip this part. You will be happy to know about all the good stuff you can
131131
| **`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` ||
132132
| **`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` | - |
133133
| **`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 |
134-
| **`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 } }` |
134+
| **`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, left: "50%", marginLeft: 15 }, text: { color: "#fff", fontWeight: "500", fontSize: 12 } }` |
135135
| **`isBadgeVisible`** | Determines if the badge is visible or not | `boolean` | - |
136136

137137
## Behind the Navigation Bar

lib/Badge.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import React, { Component } from 'react'
77
import {
88
View,
99
Text,
10-
StyleSheet
10+
StyleSheet,
11+
Animated
1112
} from 'react-native';
1213

1314
type BProps = {
@@ -16,6 +17,7 @@ type BProps = {
1617
children: Array<ReactElement<*>>,
1718
style: any,
1819
isVisible: boolean,
20+
translateY: number,
1921
}
2022

2123
const defaultProps = {
@@ -42,7 +44,9 @@ export default class Badge extends Component {
4244
justifyContent: "center",
4345
backgroundColor: "#F50057",
4446
zIndex: 9999,
45-
bottom: 10,
47+
top: 3,
48+
left: "50%",
49+
marginLeft: 15
4650
},
4751
style.container
4852
);
@@ -64,18 +68,21 @@ export default class Badge extends Component {
6468
}
6569

6670
render() {
67-
const { children, text, isVisible } = this.props;
71+
const { children, text, isVisible, translateY } = this.props;
6872
const styles = this.createStyles();
6973
if (!isVisible) {
7074
return null;
7175
}
7276

7377
return (
74-
<View>
75-
<View style={styles.container}>
76-
<Text style={styles.text}>{text}</Text>
77-
</View>
78-
</View>
78+
<Animated.View
79+
style={[
80+
styles.container,
81+
{ transform: [{ translateY: translateY }] },
82+
]}
83+
>
84+
<Text style={styles.text}>{text}</Text>
85+
</Animated.View>
7986
)
8087
}
8188
}

lib/Tab.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,20 @@ export default class Tab extends Component {
129129

130130
_renderBadge = () => {
131131
const { badgeText, badgeSize, badgeStyle, isBadgeVisible } = this.props;
132+
const mode = this._getModeString();
132133

133134
if (badgeText === undefined && !isBadgeVisible) {
134135
return null;
135136
}
136137

137138
return (
138-
<Badge
139-
text={badgeText}
140-
size={badgeSize}
141-
style={badgeStyle}
142-
isVisible={isBadgeVisible}
143-
/>
139+
<Badge
140+
text={badgeText}
141+
size={badgeSize}
142+
style={badgeStyle}
143+
isVisible={isBadgeVisible}
144+
translateY={this.state[mode].iconY}
145+
/>
144146
)
145147
}
146148

0 commit comments

Comments
 (0)