function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
import * as React from "react";
import { Animated, StyleSheet, useWindowDimensions } from "react-native";
import { withTheme } from "../core/theming";
import { colors } from "../styles/tokens";
import color from "color";
const defaultSize = 20;
/**
 * Badges are small status descriptors for UI elements.
 * A badge consists of a small circle, typically containing a number or other short set of characters, that appears in proximity to another object.
 *
 * <div class="screenshots">
 *   <figure>
 *     <img class="small" src="screenshots/badge-1.png" />
 *     <figcaption>Badge with content</figcaption>
 *   </figure>
 *   <figure>
 *     <img class="small" src="screenshots/badge-2.png" />
 *     <figcaption>Badge without content</figcaption>
 *   </figure>
 * </div>
 *
 * ## Usage
 * ```js
 * import * as React from 'react';
 * import { Badge } from 'react-native-paper';
 *
 * const MyComponent = () => (
 *   <Badge>3</Badge>
 * );
 *
 * export default MyComponent;
 * ```
 */
const Badge = _ref => {
  let {
    children,
    size = defaultSize,
    style,
    theme,
    visible = true,
    ...rest
  } = _ref;
  const {
    current: opacity
  } = React.useRef(new Animated.Value(visible ? 1 : 0));
  const {
    fontScale
  } = useWindowDimensions();
  const isFirstRendering = React.useRef(true);
  const {
    animation: {
      scale
    }
  } = theme;
  React.useEffect(() => {
    // Do not run animation on very first rendering
    if (isFirstRendering.current) {
      isFirstRendering.current = false;
      return;
    }
    Animated.timing(opacity, {
      toValue: visible ? 1 : 0,
      duration: 150 * scale,
      useNativeDriver: true
    }).start();
  }, [visible, opacity, scale]);
  const {
    backgroundColor = theme.colors.error.default,
    ...restStyle
  } = StyleSheet.flatten(style) || {};
  const textColor = color(backgroundColor).isLight() ? colors.white : colors.black;
  const borderRadius = size / 2;
  const paddingHorizontal = 4;
  return /*#__PURE__*/React.createElement(Animated.Text, _extends({
    numberOfLines: 1,
    style: [{
      opacity,
      backgroundColor,
      color: textColor,
      fontSize: size * 0.5,
      ...theme.fonts.regular,
      lineHeight: size / fontScale,
      height: size,
      minWidth: size,
      borderRadius,
      paddingHorizontal
    }, styles.container, restStyle]
  }, rest), children);
};
export default withTheme(Badge);
const styles = StyleSheet.create({
  container: {
    alignSelf: "flex-end",
    textAlign: "center",
    textAlignVertical: "center",
    overflow: "hidden"
  }
});
//# sourceMappingURL=Badge.js.map