1 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
2 |
|
3 | import * as React from 'react';
|
4 | import { Animated, StyleSheet } from 'react-native';
|
5 | import { white, black } from '../styles/colors';
|
6 | import { withTheme } from '../core/theming';
|
7 | import getContrastingColor from '../utils/getContrastingColor';
|
8 | const defaultSize = 20;
|
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 | const Badge = ({
|
38 | children,
|
39 | size = defaultSize,
|
40 | style,
|
41 | theme,
|
42 | visible = true,
|
43 | ...rest
|
44 | }) => {
|
45 | const {
|
46 | current: opacity
|
47 | } = React.useRef(new Animated.Value(visible ? 1 : 0));
|
48 | const isFirstRendering = React.useRef(true);
|
49 | const {
|
50 | animation: {
|
51 | scale
|
52 | }
|
53 | } = theme;
|
54 | React.useEffect(() => {
|
55 |
|
56 | if (isFirstRendering.current) {
|
57 | isFirstRendering.current = false;
|
58 | return;
|
59 | }
|
60 |
|
61 | Animated.timing(opacity, {
|
62 | toValue: visible ? 1 : 0,
|
63 | duration: 150 * scale,
|
64 | useNativeDriver: true
|
65 | }).start();
|
66 | }, [visible, opacity, scale]);
|
67 | const {
|
68 | backgroundColor = theme.colors.notification,
|
69 | ...restStyle
|
70 | } = StyleSheet.flatten(style) || {};
|
71 | const textColor = getContrastingColor(backgroundColor, white, black);
|
72 | const borderRadius = size / 2;
|
73 | return React.createElement(Animated.Text, _extends({
|
74 | numberOfLines: 1,
|
75 | style: [{
|
76 | opacity,
|
77 | backgroundColor,
|
78 | color: textColor,
|
79 | fontSize: size * 0.5,
|
80 | ...theme.fonts.regular,
|
81 | lineHeight: size,
|
82 | height: size,
|
83 | minWidth: size,
|
84 | borderRadius
|
85 | }, styles.container, restStyle]
|
86 | }, rest), children);
|
87 | };
|
88 |
|
89 | export default withTheme(Badge);
|
90 | const styles = StyleSheet.create({
|
91 | container: {
|
92 | alignSelf: 'flex-end',
|
93 | textAlign: 'center',
|
94 | textAlignVertical: 'center',
|
95 | paddingHorizontal: 4,
|
96 | overflow: 'hidden'
|
97 | }
|
98 | });
|
99 |
|
\ | No newline at end of file |