You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Animated, Text } from 'react-native';
  4. import Icon from '../../../icons/components/Icon';
  5. import { type StyleType, combineStyles } from '../../../styles';
  6. import styles from './styles';
  7. /**
  8. * Const for status string 'in progress'.
  9. */
  10. const STATUS_IN_PROGRESS = 'in_progress';
  11. /**
  12. * Const for status string 'off'.
  13. */
  14. const STATUS_OFF = 'off';
  15. type Props = {
  16. /**
  17. * An SVG icon to be rendered as the content of the label.
  18. */
  19. icon?: Function,
  20. /**
  21. * Color for the icon.
  22. */
  23. iconColor?: ?string,
  24. /**
  25. * Status of the label. This prop adds some additional styles based on its
  26. * value. E.g. If status = off, it will render the label symbolising that
  27. * the thing it displays (e.g. Recording) is off.
  28. */
  29. status: ('in_progress' | 'off' | 'on'),
  30. /**
  31. * Style of the label.
  32. */
  33. style?: ?StyleType,
  34. /**
  35. * String or component that will be rendered as the label itself.
  36. */
  37. text?: string,
  38. /**
  39. * Custom styles for the text.
  40. */
  41. textStyle?: ?StyleType
  42. };
  43. type State = {
  44. /**
  45. * An animation object handling the opacity changes of the in progress
  46. * label.
  47. */
  48. pulseAnimation: Object
  49. }
  50. /**
  51. * Renders a circular indicator to be used for status icons, such as recording
  52. * on, audio-only conference, video quality and similar.
  53. */
  54. export default class Label extends Component<Props, State> {
  55. /**
  56. * A reference to the started animation of this label.
  57. */
  58. animationReference: Object;
  59. /**
  60. * Instantiates a new instance of {@code Label}.
  61. *
  62. * @inheritdoc
  63. */
  64. constructor(props: Props) {
  65. super(props);
  66. this.state = {
  67. pulseAnimation: new Animated.Value(0)
  68. };
  69. }
  70. /**
  71. * Implements {@code Component#componentDidMount}.
  72. *
  73. * @inheritdoc
  74. */
  75. componentDidMount() {
  76. this._maybeToggleAnimation({}, this.props);
  77. }
  78. /**
  79. * Implements {@code Component#componentDidUpdate}.
  80. *
  81. * @inheritdoc
  82. */
  83. componentDidUpdate(prevProps: Props) {
  84. this._maybeToggleAnimation(prevProps, this.props);
  85. }
  86. /**
  87. * Implements React {@link Component}'s render.
  88. *
  89. * @inheritdoc
  90. */
  91. render() {
  92. const { icon, text, status, style, iconColor, textStyle } = this.props;
  93. let extraStyle = null;
  94. switch (status) {
  95. case STATUS_IN_PROGRESS:
  96. extraStyle = {
  97. opacity: this.state.pulseAnimation
  98. };
  99. break;
  100. case STATUS_OFF:
  101. extraStyle = styles.labelOff;
  102. break;
  103. }
  104. return (
  105. <Animated.View
  106. style = { [
  107. combineStyles(styles.labelContainer, style),
  108. extraStyle
  109. ] }>
  110. { icon && <Icon
  111. color = { iconColor }
  112. size = '18'
  113. src = { icon } /> }
  114. { text && <Text style = { [ styles.labelText, textStyle ] }>
  115. { text }
  116. </Text>}
  117. </Animated.View>
  118. );
  119. }
  120. /**
  121. * Checks if the animation has to be started or stopped and acts
  122. * accordingly.
  123. *
  124. * @param {Props} oldProps - The previous values of the Props.
  125. * @param {Props} newProps - The new values of the Props.
  126. * @returns {void}
  127. */
  128. _maybeToggleAnimation(oldProps, newProps) {
  129. const { status: oldStatus } = oldProps;
  130. const { status: newStatus } = newProps;
  131. const { pulseAnimation } = this.state;
  132. if (newStatus === STATUS_IN_PROGRESS
  133. && oldStatus !== STATUS_IN_PROGRESS) {
  134. // Animation must be started
  135. this.animationReference = Animated.loop(Animated.sequence([
  136. Animated.timing(pulseAnimation, {
  137. delay: 500,
  138. toValue: 1,
  139. useNativeDriver: true
  140. }),
  141. Animated.timing(pulseAnimation, {
  142. toValue: 0.3,
  143. useNativeDriver: true
  144. })
  145. ]));
  146. this.animationReference.start();
  147. } else if (this.animationReference
  148. && newStatus !== STATUS_IN_PROGRESS
  149. && oldStatus === STATUS_IN_PROGRESS) {
  150. // Animation must be stopped
  151. this.animationReference.stop();
  152. }
  153. }
  154. }