浏览代码

[RN] Refactor SideBar layout and animation (coding style)

master
Lyubo Marinov 7 年前
父节点
当前提交
c6d553738f

+ 32
- 36
react/features/base/react/components/native/SideBar.js 查看文件

@@ -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
 }

+ 9
- 11
react/features/base/react/components/native/styles.js 查看文件

@@ -1,10 +1,8 @@
1
+// @flow
2
+
1 3
 import { StyleSheet } from 'react-native';
2 4
 
3
-import {
4
-    BoxModel,
5
-    ColorPalette,
6
-    createStyleSheet
7
-} from '../../../styles';
5
+import { BoxModel, ColorPalette, createStyleSheet } from '../../../styles';
8 6
 
9 7
 const AVATAR_OPACITY = 0.4;
10 8
 const AVATAR_SIZE = 65;
@@ -21,7 +19,7 @@ export const UNDERLAY_COLOR = 'rgba(255, 255, 255, 0.2)';
21 19
 
22 20
 const HEADER_STYLES = {
23 21
     /**
24
-     * Platform specific header button (e.g. back, menu...etc).
22
+     * Platform specific header button (e.g. back, menu, etc).
25 23
      */
26 24
     headerButton: {
27 25
         alignSelf: 'center',
@@ -57,7 +55,7 @@ const HEADER_STYLES = {
57 55
     },
58 56
 
59 57
     /**
60
-     * Base style of Header
58
+     * Base style of Header.
61 59
      */
62 60
     screenHeader: {
63 61
         alignItems: 'center',
@@ -293,8 +291,8 @@ const SIDEBAR_STYLES = {
293 291
     },
294 292
 
295 293
     /**
296
-     * The opaque area that covers the rest of the screen, when
297
-     * the side bar is open.
294
+     * The opaque area that covers the rest of the screen, when the side bar is
295
+     * open.
298 296
      */
299 297
     sideMenuShadow: {
300 298
         ...StyleSheet.absoluteFillObject,
@@ -303,8 +301,8 @@ const SIDEBAR_STYLES = {
303 301
 };
304 302
 
305 303
 /**
306
- * The styles of the React {@code Components} of the generic components
307
- * in the app.
304
+ * The styles of the generic React {@code Component}s implemented by the feature
305
+ * base/react.
308 306
  */
309 307
 export default createStyleSheet({
310 308
     ...HEADER_STYLES,

正在加载...
取消
保存