|
@@ -1,11 +1,7 @@
|
1
|
1
|
// @flow
|
2
|
2
|
|
3
|
3
|
import React, { Component, type Node } from 'react';
|
4
|
|
-import {
|
5
|
|
- Animated,
|
6
|
|
- TouchableWithoutFeedback,
|
7
|
|
- View
|
8
|
|
-} from 'react-native';
|
|
4
|
+import { Animated, TouchableWithoutFeedback, View } from 'react-native';
|
9
|
5
|
|
10
|
6
|
import styles, { SIDEBAR_WIDTH } from './styles';
|
11
|
7
|
|
|
@@ -15,21 +11,21 @@ import styles, { SIDEBAR_WIDTH } from './styles';
|
15
|
11
|
type Props = {
|
16
|
12
|
|
17
|
13
|
/**
|
18
|
|
- * The children of the Component
|
|
14
|
+ * The children of {@code SideBar}.
|
19
|
15
|
*/
|
20
|
16
|
children: Node,
|
21
|
17
|
|
22
|
18
|
/**
|
23
|
|
- * Callback to notify the containing Component that the sidebar is
|
|
19
|
+ * Callback to notify the containing {@code Component} that the sidebar is
|
24
|
20
|
* closing.
|
25
|
21
|
*/
|
26
|
22
|
onHide: Function,
|
27
|
23
|
|
28
|
24
|
/**
|
29
|
|
- * Sets the menu displayed or hidden.
|
|
25
|
+ * Whether the menu (of the {@code SideBar}?) is displayed/rendered/shown.
|
30
|
26
|
*/
|
31
|
27
|
show: boolean
|
32
|
|
-}
|
|
28
|
+};
|
33
|
29
|
|
34
|
30
|
/**
|
35
|
31
|
* The type of the React {@code Component} state of {@link SideBar}.
|
|
@@ -37,18 +33,18 @@ type Props = {
|
37
|
33
|
type State = {
|
38
|
34
|
|
39
|
35
|
/**
|
40
|
|
- * Indicates whether the side overlay should be rendered or not.
|
|
36
|
+ * Whether the side overlay should be displayed/rendered/shown.
|
41
|
37
|
*/
|
42
|
38
|
showOverlay: boolean,
|
43
|
39
|
|
44
|
40
|
/**
|
45
|
41
|
* The native animation object.
|
46
|
42
|
*/
|
47
|
|
- sliderAnimation: Object
|
48
|
|
-}
|
|
43
|
+ sliderAnimation: Animated.Value
|
|
44
|
+};
|
49
|
45
|
|
50
|
46
|
/**
|
51
|
|
- * A generic animated side bar to be used for left side menus
|
|
47
|
+ * A generic animated side bar to be used for left-side, hamburger-style menus.
|
52
|
48
|
*/
|
53
|
49
|
export default class SideBar extends Component<Props, State> {
|
54
|
50
|
/**
|
|
@@ -64,11 +60,12 @@ export default class SideBar extends Component<Props, State> {
|
64
|
60
|
sliderAnimation: new Animated.Value(0)
|
65
|
61
|
};
|
66
|
62
|
|
|
63
|
+ // Bind event handlers so they are only bound once per instance.
|
67
|
64
|
this._onHideMenu = this._onHideMenu.bind(this);
|
68
|
65
|
}
|
69
|
66
|
|
70
|
67
|
/**
|
71
|
|
- * Implements the Component's componentDidMount method.
|
|
68
|
+ * Implements React's {@link Component#componentDidMount()}.
|
72
|
69
|
*
|
73
|
70
|
* @inheritdoc
|
74
|
71
|
*/
|
|
@@ -77,14 +74,12 @@ export default class SideBar extends Component<Props, State> {
|
77
|
74
|
}
|
78
|
75
|
|
79
|
76
|
/**
|
80
|
|
- * Implements the Component's componentWillReceiveProps method.
|
|
77
|
+ * Implements React's {@link Component#componentWillReceiveProps()}.
|
81
|
78
|
*
|
82
|
79
|
* @inheritdoc
|
83
|
80
|
*/
|
84
|
|
- componentWillReceiveProps(newProps: Props) {
|
85
|
|
- if (newProps.show !== this.props.show) {
|
86
|
|
- this._setShow(newProps.show);
|
87
|
|
- }
|
|
81
|
+ componentWillReceiveProps({ show }: Props) {
|
|
82
|
+ (show === this.props.show) || this._setShow(show);
|
88
|
83
|
}
|
89
|
84
|
|
90
|
85
|
/**
|
|
@@ -120,17 +115,16 @@ export default class SideBar extends Component<Props, State> {
|
120
|
115
|
* @returns {Array<Object>}
|
121
|
116
|
*/
|
122
|
117
|
_getContentStyle() {
|
123
|
|
- const { sliderAnimation } = this.state;
|
124
|
|
- const transformStyle
|
125
|
|
- = { transform: [ { translateX: sliderAnimation } ] };
|
126
|
|
-
|
127
|
|
- return [ styles.sideMenuContent, transformStyle ];
|
|
118
|
+ return [
|
|
119
|
+ styles.sideMenuContent,
|
|
120
|
+ { transform: [ { translateX: this.state.sliderAnimation } ] }
|
|
121
|
+ ];
|
128
|
122
|
}
|
129
|
123
|
|
130
|
124
|
_onHideMenu: () => void;
|
131
|
125
|
|
132
|
126
|
/**
|
133
|
|
- * Hides the menu.
|
|
127
|
+ * Hides the side menu.
|
134
|
128
|
*
|
135
|
129
|
* @private
|
136
|
130
|
* @returns {void}
|
|
@@ -146,28 +140,30 @@ export default class SideBar extends Component<Props, State> {
|
146
|
140
|
_setShow: (boolean) => void;
|
147
|
141
|
|
148
|
142
|
/**
|
149
|
|
- * Sets the side menu visible or hidden.
|
|
143
|
+ * Shows/hides the side menu.
|
150
|
144
|
*
|
151
|
|
- * @param {boolean} show - The new expected visibility value.
|
|
145
|
+ * @param {boolean} show - If the side menu is to be made visible,
|
|
146
|
+ * {@code true}; otherwise, {@code false}.
|
152
|
147
|
* @private
|
153
|
148
|
* @returns {void}
|
154
|
149
|
*/
|
155
|
150
|
_setShow(show) {
|
156
|
|
- if (show) {
|
157
|
|
- this.setState({ showOverlay: true });
|
158
|
|
- }
|
|
151
|
+ show && this.setState({ showOverlay: true });
|
159
|
152
|
|
160
|
153
|
Animated
|
161
|
154
|
.timing(
|
162
|
|
- this.state.sliderAnimation,
|
163
|
|
- {
|
|
155
|
+ /* value */ this.state.sliderAnimation,
|
|
156
|
+ /* config */ {
|
164
|
157
|
toValue: show ? SIDEBAR_WIDTH : 0,
|
165
|
158
|
useNativeDriver: true
|
166
|
159
|
})
|
167
|
|
- .start(animationState => {
|
168
|
|
- if (animationState.finished && !show) {
|
169
|
|
- this.setState({ showOverlay: false });
|
170
|
|
- }
|
|
160
|
+ .start(({ finished }) => {
|
|
161
|
+ finished && !show && this.setState({ showOverlay: false });
|
|
162
|
+
|
|
163
|
+ // XXX Technically, the arrow function can further be simplified
|
|
164
|
+ // by removing the {} and returning the boolean expression
|
|
165
|
+ // above. Practically and unfortunately though, Flow freaks out
|
|
166
|
+ // and states that Animated.timing doesn't exist!?
|
171
|
167
|
});
|
172
|
168
|
}
|
173
|
169
|
}
|