UNPKG

2.82 kBJavaScriptView Raw
1function _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
3import * as React from 'react';
4import { Animated, StyleSheet } from 'react-native';
5import { white, black } from '../styles/colors';
6import { withTheme } from '../core/theming';
7import getContrastingColor from '../utils/getContrastingColor';
8const defaultSize = 20;
9
10/**
11 * Badges are small status descriptors for UI elements.
12 * A badge consists of a small circle, typically containing a number or other short set of characters, that appears in proximity to another object.
13 *
14 * <div class="screenshots">
15 * <figure>
16 * <img class="small" src="screenshots/badge-1.png" />
17 * <figcaption>Badge with content</figcaption>
18 * </figure>
19 * <figure>
20 * <img class="small" src="screenshots/badge-2.png" />
21 * <figcaption>Badge without content</figcaption>
22 * </figure>
23 * </div>
24 *
25 * ## Usage
26 * ```js
27 * import * as React from 'react';
28 * import { Badge } from 'react-native-paper';
29 *
30 * const MyComponent = () => (
31 * <Badge>3</Badge>
32 * );
33 *
34 * export default MyComponent;
35 * ```
36 */
37const 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 // Do not run animation on very first rendering
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 /*#__PURE__*/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
89export default withTheme(Badge);
90const styles = StyleSheet.create({
91 container: {
92 alignSelf: 'flex-end',
93 textAlign: 'center',
94 textAlignVertical: 'center',
95 paddingHorizontal: 4,
96 overflow: 'hidden'
97 }
98});
99//# sourceMappingURL=Badge.js.map
\No newline at end of file