-
Notifications
You must be signed in to change notification settings - Fork 0
/
Badge.tsx
86 lines (82 loc) · 2.06 KB
/
Badge.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React from 'react';
import { Platform, StyleProp, Text, View, ViewStyle } from 'react-native';
import { useThemeContext } from '../contexts/ThemeContext';
import createStyleSheet from '../styles/createStyleSheet';
import { truncatedBadgeCount } from '../utils/format';
type BadgeProps = {
count?: number;
maxCount?: number | undefined;
badgeColor?: string | undefined;
textColor?: string | undefined;
style?: StyleProp<ViewStyle> | undefined;
size?: 'small' | 'default' | undefined;
};
export default function Badge({
count,
maxCount,
badgeColor,
textColor,
style,
size = 'default',
}: BadgeProps): JSX.Element {
const { colors, fonts } = useThemeContext();
const isSmall = size === 'small';
return (
<View
style={[
count !== undefined
? isSmall
? styles.badgeSmall
: styles.badgeDefault
: styles.badgeDotSmall,
{
backgroundColor: badgeColor ?? colors.badge.background,
},
count !== undefined
? count >= 10 &&
(isSmall ? styles.badgeSmallPadding : styles.badgeDefaultPadding)
: styles.badgeSmallPadding,
style,
]}
>
{count !== undefined ? (
<Text
style={[{ color: textColor ?? colors.badge.content }, fonts.caption]}
>
{truncatedBadgeCount(count, maxCount)}
</Text>
) : null}
</View>
);
}
const styles = createStyleSheet({
badgeDefault: {
paddingTop: Platform.select({ ios: 2, android: 2 }),
minWidth: 19,
minHeight: 19,
borderRadius: 99,
alignItems: 'center',
justifyContent: 'center',
},
badgeDefaultPadding: {
paddingHorizontal: 6,
},
badgeSmall: {
paddingTop: Platform.select({ ios: 3, android: 2 }),
minWidth: 16,
minHeight: 16,
borderRadius: 99,
alignItems: 'center',
justifyContent: 'center',
},
badgeDotSmall: {
minWidth: 10,
minHeight: 10,
borderRadius: 99,
alignItems: 'center',
justifyContent: 'center',
},
badgeSmallPadding: {
paddingHorizontal: 4,
},
});