Przeglądaj źródła

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

master
Lyubo Marinov 7 lat temu
rodzic
commit
c6d553738f

+ 32
- 36
react/features/base/react/components/native/SideBar.js Wyświetl plik

1
 // @flow
1
 // @flow
2
 
2
 
3
 import React, { Component, type Node } from 'react';
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
 import styles, { SIDEBAR_WIDTH } from './styles';
6
 import styles, { SIDEBAR_WIDTH } from './styles';
11
 
7
 
15
 type Props = {
11
 type Props = {
16
 
12
 
17
     /**
13
     /**
18
-     * The children of the Component
14
+     * The children of {@code SideBar}.
19
      */
15
      */
20
     children: Node,
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
      * closing.
20
      * closing.
25
      */
21
      */
26
     onHide: Function,
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
     show: boolean
27
     show: boolean
32
-}
28
+};
33
 
29
 
34
 /**
30
 /**
35
  * The type of the React {@code Component} state of {@link SideBar}.
31
  * The type of the React {@code Component} state of {@link SideBar}.
37
 type State = {
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
     showOverlay: boolean,
38
     showOverlay: boolean,
43
 
39
 
44
     /**
40
     /**
45
      * The native animation object.
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
 export default class SideBar extends Component<Props, State> {
49
 export default class SideBar extends Component<Props, State> {
54
     /**
50
     /**
64
             sliderAnimation: new Animated.Value(0)
60
             sliderAnimation: new Animated.Value(0)
65
         };
61
         };
66
 
62
 
63
+        // Bind event handlers so they are only bound once per instance.
67
         this._onHideMenu = this._onHideMenu.bind(this);
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
      * @inheritdoc
70
      * @inheritdoc
74
      */
71
      */
77
     }
74
     }
78
 
75
 
79
     /**
76
     /**
80
-     * Implements the Component's componentWillReceiveProps method.
77
+     * Implements React's {@link Component#componentWillReceiveProps()}.
81
      *
78
      *
82
      * @inheritdoc
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
      * @returns {Array<Object>}
115
      * @returns {Array<Object>}
121
      */
116
      */
122
     _getContentStyle() {
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
     _onHideMenu: () => void;
124
     _onHideMenu: () => void;
131
 
125
 
132
     /**
126
     /**
133
-     * Hides the menu.
127
+     * Hides the side menu.
134
      *
128
      *
135
      * @private
129
      * @private
136
      * @returns {void}
130
      * @returns {void}
146
     _setShow: (boolean) => void;
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
      * @private
147
      * @private
153
      * @returns {void}
148
      * @returns {void}
154
      */
149
      */
155
     _setShow(show) {
150
     _setShow(show) {
156
-        if (show) {
157
-            this.setState({ showOverlay: true });
158
-        }
151
+        show && this.setState({ showOverlay: true });
159
 
152
 
160
         Animated
153
         Animated
161
             .timing(
154
             .timing(
162
-                this.state.sliderAnimation,
163
-                {
155
+                /* value */ this.state.sliderAnimation,
156
+                /* config */ {
164
                     toValue: show ? SIDEBAR_WIDTH : 0,
157
                     toValue: show ? SIDEBAR_WIDTH : 0,
165
                     useNativeDriver: true
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 Wyświetl plik

1
+// @flow
2
+
1
 import { StyleSheet } from 'react-native';
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
 const AVATAR_OPACITY = 0.4;
7
 const AVATAR_OPACITY = 0.4;
10
 const AVATAR_SIZE = 65;
8
 const AVATAR_SIZE = 65;
21
 
19
 
22
 const HEADER_STYLES = {
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
     headerButton: {
24
     headerButton: {
27
         alignSelf: 'center',
25
         alignSelf: 'center',
57
     },
55
     },
58
 
56
 
59
     /**
57
     /**
60
-     * Base style of Header
58
+     * Base style of Header.
61
      */
59
      */
62
     screenHeader: {
60
     screenHeader: {
63
         alignItems: 'center',
61
         alignItems: 'center',
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
     sideMenuShadow: {
297
     sideMenuShadow: {
300
         ...StyleSheet.absoluteFillObject,
298
         ...StyleSheet.absoluteFillObject,
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
 export default createStyleSheet({
307
 export default createStyleSheet({
310
     ...HEADER_STYLES,
308
     ...HEADER_STYLES,

Ładowanie…
Anuluj
Zapisz