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