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.

TintedView.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { View } from 'react-native';
  4. import { ColorPalette } from '../../../styles';
  5. /**
  6. * Base style for the {@code TintedView} component.
  7. */
  8. const BASE_STYLE = {
  9. alignItems: 'center',
  10. bottom: 0,
  11. justifyContent: 'center',
  12. left: 0,
  13. position: 'absolute',
  14. right: 0,
  15. top: 0
  16. };
  17. /**
  18. * {@code TintedView}'s React {@code Component} prop types.
  19. */
  20. type Props = {
  21. /**
  22. * The children components of this component.
  23. */
  24. children?: React$Node,
  25. /**
  26. * Color used as the background of the view. Defaults to
  27. */
  28. color: string,
  29. /**
  30. * Opacity for the
  31. */
  32. opacity: number,
  33. /**
  34. * Style to override the base style.
  35. */
  36. style: Object
  37. };
  38. /**
  39. * {@code TintedView}'s React {@code Component} state.
  40. */
  41. type State = {
  42. /**
  43. * The style of {@code TintedView} which is a combination of its default
  44. * style, the consumer-specified style.
  45. */
  46. style: Object
  47. };
  48. /**
  49. * Implements a component aimed at covering another view and tinting it with
  50. * the given color and opacity.
  51. */
  52. export default class TintedView extends Component<Props, State> {
  53. /**
  54. * Default values for the component's props.
  55. */
  56. static defaultProps = {
  57. color: ColorPalette.appBackground,
  58. opacity: 0.8,
  59. style: {}
  60. };
  61. /**
  62. * Initializes a new {@code TintedView} instance.
  63. *
  64. * @param {Object} props - The read-only React {@code Component} props with
  65. * which the new instance is to be initialized.
  66. */
  67. constructor(props: Object) {
  68. super(props);
  69. this.componentWillReceiveProps(props);
  70. }
  71. /**
  72. * Notifies this mounted React {@code Component} that it will receive new
  73. * props. Forks (in Facebook/React speak) the prop {@code style} because its
  74. * value is to be combined with the default style.
  75. *
  76. * @inheritdoc
  77. * @param {Object} nextProps - The read-only React {@code Component} props
  78. * that this instance will receive.
  79. * @returns {void}
  80. */
  81. componentWillReceiveProps(nextProps: Object) {
  82. // style
  83. const prevColor = this.props && this.props.color;
  84. const prevOpacity = this.props && this.props.opacity;
  85. const prevStyle = this.props && this.props.style;
  86. const nextColor = nextProps && nextProps.color;
  87. const nextOpacity = nextProps && nextProps.opacity;
  88. const nextStyle = nextProps && nextProps.style;
  89. const assignState = !this.state;
  90. if (assignState
  91. || prevColor !== nextColor
  92. || prevOpacity !== nextOpacity
  93. || prevStyle !== nextStyle) {
  94. const nextState = {
  95. style: {
  96. ...BASE_STYLE,
  97. ...nextStyle,
  98. backgroundColor: nextColor,
  99. opacity: nextOpacity
  100. }
  101. };
  102. if (assignState) {
  103. // eslint-disable-next-line react/no-direct-mutation-state
  104. this.state = nextState;
  105. } else {
  106. this.setState(nextState);
  107. }
  108. }
  109. }
  110. /**
  111. * Implements React's {@link Component#render()}.
  112. *
  113. * @inheritdoc
  114. * @returns {ReactElement}
  115. */
  116. render() {
  117. // XXX Don't tint the children, tint the background only.
  118. return (
  119. <View style = { BASE_STYLE }>
  120. <View style = { this.state.style } />
  121. <View style = { BASE_STYLE }>
  122. { this.props.children }
  123. </View>
  124. </View>
  125. );
  126. }
  127. }