UNPKG

2.94 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, useWindowDimensions } 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 = _ref => {
38 let {
39 children,
40 size = defaultSize,
41 style,
42 theme,
43 visible = true,
44 ...rest
45 } = _ref;
46 const {
47 current: opacity
48 } = React.useRef(new Animated.Value(visible ? 1 : 0));
49 const {
50 fontScale
51 } = useWindowDimensions();
52 const isFirstRendering = React.useRef(true);
53 const {
54 animation: {
55 scale
56 }
57 } = theme;
58 React.useEffect(() => {
59 // Do not run animation on very first rendering
60 if (isFirstRendering.current) {
61 isFirstRendering.current = false;
62 return;
63 }
64
65 Animated.timing(opacity, {
66 toValue: visible ? 1 : 0,
67 duration: 150 * scale,
68 useNativeDriver: true
69 }).start();
70 }, [visible, opacity, scale]);
71 const {
72 backgroundColor = theme.colors.notification,
73 ...restStyle
74 } = StyleSheet.flatten(style) || {};
75 const textColor = getContrastingColor(backgroundColor, white, black);
76 const borderRadius = size / 2;
77 return /*#__PURE__*/React.createElement(Animated.Text, _extends({
78 numberOfLines: 1,
79 style: [{
80 opacity,
81 backgroundColor,
82 color: textColor,
83 fontSize: size * 0.5,
84 ...theme.fonts.regular,
85 lineHeight: size / fontScale,
86 height: size,
87 minWidth: size,
88 borderRadius
89 }, styles.container, restStyle]
90 }, rest), children);
91};
92
93export default withTheme(Badge);
94const styles = StyleSheet.create({
95 container: {
96 alignSelf: 'flex-end',
97 textAlign: 'center',
98 textAlignVertical: 'center',
99 paddingHorizontal: 4,
100 overflow: 'hidden'
101 }
102});
103//# sourceMappingURL=Badge.js.map
\No newline at end of file