Skip to content

Commit

Permalink
chore: added icon with badge example
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Aug 12, 2020
1 parent f9e3834 commit 66f9cce
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 85 deletions.
58 changes: 58 additions & 0 deletions example/src/components/badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { memo, useMemo, useState, useEffect } from 'react';
import { View, StyleSheet, Text } from 'react-native';

interface BadgeProps {
iconSize: number;
}

const BADGE_SIZE = 10;

const BadgeComponent = ({ iconSize }: BadgeProps) => {
const [count, setCount] = useState(1);
const containerStyle = useMemo(
() => ({
...styles.container,
transform: [
{ translateX: (iconSize - BADGE_SIZE / 1.5) / 2 },
{ translateY: (iconSize - BADGE_SIZE / 1.5) / -2 },
],
}),
[iconSize]
);

useEffect(() => {
const intervalId = setInterval(() => setCount(state => state + 1), 2500);
return () => {
clearInterval(intervalId);
};
}, []);

return (
<View style={containerStyle}>
<Text style={styles.count}>{count}</Text>
</View>
);
};

const Badge = memo(BadgeComponent);

const styles = StyleSheet.create({
container: {
position: 'absolute',
alignSelf: 'center',
minWidth: BADGE_SIZE,
minHeight: BADGE_SIZE,
borderRadius: BADGE_SIZE,
backgroundColor: 'red',
},
count: {
color: 'white',
fontWeight: 'bold',
textAlign: 'center',
margin: 1,
lineHeight: BADGE_SIZE,
fontSize: 6,
},
});

export default Badge;
1 change: 1 addition & 0 deletions example/src/components/badge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Badge';
17 changes: 17 additions & 0 deletions example/src/components/iconWithBadge/IconWithBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { memo } from 'react';
import HomeSVG from '../../svg/HomeSVG';
import Badge from '../badge';
import { SVGProps } from '../../svg/types';

const IconWithBadgeComponent = (props: SVGProps) => {
return (
<>
<HomeSVG {...props} />
<Badge iconSize={props.size} />
</>
);
};

const IconWithBadge = memo(IconWithBadgeComponent);

export default IconWithBadge;
1 change: 1 addition & 0 deletions example/src/components/iconWithBadge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './IconWithBadge';
52 changes: 23 additions & 29 deletions example/src/screens/BubbleStyled.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { useMemo } from 'react';
import { StyleProp, ViewStyle } from 'react-native';
import { useSafeArea } from 'react-native-safe-area-context';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import AnimatedTabBar, {
TabsConfig,
BubbleTabBarItemConfig,
} from '@gorhom/animated-tabbar';
import DummyScreen from './Dummy';
import HomeSVG from '../svg/HomeSVG';
import HomeIcon from '../components/iconWithBadge';
import LikeSVG from '../svg/LikeSVG';
import SearchSVG from '../svg/SearchSVG';
import ProfileSVG from '../svg/ProfileSVG';
Expand All @@ -21,7 +20,7 @@ const tabs: TabsConfig<BubbleTabBarItemConfig, MainTabsParams> = {
color: 'white',
},
icon: {
component: HomeSVG,
component: HomeIcon,
activeColor: 'rgba(255,255,255,1)',
inactiveColor: 'rgba(68,68,68,1)',
},
Expand Down Expand Up @@ -84,38 +83,33 @@ const BubbleStyledScreen = () => {
return 20 + bottom + 12 * 2 + 12 * 2 + 12;
}, [bottom]);

const tabBarStyle = useMemo<StyleProp<ViewStyle>>(
() => ({
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
}),
[bottom]
);

const tabBarOptions = useMemo(
() => ({
safeAreaInsets: {
bottom: 0,
},
style: tabBarStyle,
style: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
},
}),
[tabBarStyle]
[bottom]
);

// render
Expand Down
48 changes: 21 additions & 27 deletions example/src/screens/FlashyStyled.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useMemo } from 'react';
import { StyleProp, ViewStyle } from 'react-native';
import { useSafeArea } from 'react-native-safe-area-context';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import AnimatedTabBar, {
Expand Down Expand Up @@ -80,38 +79,33 @@ const FlashyStyledScreen = () => {
return 20 + bottom + 12 * 2 + 12 * 2 + 12;
}, [bottom]);

const tabBarStyle = useMemo<StyleProp<ViewStyle>>(
() => ({
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
}),
[bottom]
);

const tabBarOptions = useMemo(
() => ({
safeAreaInsets: {
bottom: 0,
},
style: tabBarStyle,
style: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
},
}),
[tabBarStyle]
[bottom]
);

// render
Expand Down
53 changes: 24 additions & 29 deletions example/src/screens/MaterialStyled.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useMemo } from 'react';
import { StatusBar, StyleProp, ViewStyle } from 'react-native';
import { StatusBar } from 'react-native';
import { useSafeArea } from 'react-native-safe-area-context';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import AnimatedTabBar, {
TabsConfig,
MaterialTabBarItemConfig,
} from '@gorhom/animated-tabbar';
import DummyScreen from './Dummy';
import HomeSVG from '../svg/HomeSVG';
import HomeIcon from '../components/iconWithBadge';
import LikeSVG from '../svg/LikeSVG';
import SearchSVG from '../svg/SearchSVG';
import ProfileSVG from '../svg/ProfileSVG';
Expand All @@ -18,7 +18,7 @@ const Tab = createBottomTabNavigator<MainTabsParams>();
const tabs: TabsConfig<MaterialTabBarItemConfig, MainTabsParams> = {
Home: {
icon: {
component: HomeSVG,
component: HomeIcon,
color: 'rgba(255,255,255,1)',
},
ripple: {
Expand Down Expand Up @@ -64,38 +64,33 @@ const MaterialStyledScreen = () => {
return 20 + bottom + 12 * 2 + 12 * 2 + 12;
}, [bottom]);

const tabBarStyle = useMemo<StyleProp<ViewStyle>>(
() => ({
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
}),
[bottom]
);

const tabBarOptions = useMemo(
() => ({
safeAreaInsets: {
bottom: 0,
},
style: tabBarStyle,
style: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
borderRadius: 16,
marginLeft: 32,
marginRight: 32,
marginBottom: bottom,
backgroundColor: '#000',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 12,
},
shadowOpacity: 0.58,
shadowRadius: 16.0,

elevation: 24,
},
}),
[tabBarStyle]
[bottom]
);
return (
<>
Expand Down

0 comments on commit 66f9cce

Please sign in to comment.