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.

SideBar.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // @flow
  2. import React, { Component } from 'react';
  3. import {
  4. Animated,
  5. Dimensions,
  6. TouchableWithoutFeedback,
  7. View
  8. } from 'react-native';
  9. import styles, { SIDEBAR_WIDTH } from './styles';
  10. /**
  11. * The type of the React {@code Component} props of {@link SideBar}.
  12. */
  13. type Props = {
  14. /**
  15. * The local participant's avatar
  16. */
  17. _avatar: string,
  18. /**
  19. * The children of the Component
  20. */
  21. children: React$Node,
  22. /**
  23. * Callback to notify the containing Component that the sidebar is
  24. * closing.
  25. */
  26. onHide: Function,
  27. /**
  28. * Sets the menu displayed or hidden.
  29. */
  30. show: boolean
  31. }
  32. /**
  33. * The type of the React {@code Component} state of {@link SideBar}.
  34. */
  35. type State = {
  36. /**
  37. * Indicates whether the side overlay should be rendered or not.
  38. */
  39. showOverlay: boolean,
  40. /**
  41. * Indicates whether the side bar is visible or not.
  42. */
  43. showSideBar: boolean,
  44. /**
  45. * The native animation object.
  46. */
  47. sliderAnimation: Object
  48. }
  49. /**
  50. * A generic animated side bar to be used for left side menus
  51. */
  52. export default class SideBar extends Component<Props, State> {
  53. _mounted: boolean;
  54. /**
  55. * Initializes a new {@code SideBar} instance.
  56. *
  57. * @inheritdoc
  58. */
  59. constructor(props: Props) {
  60. super(props);
  61. this.state = {
  62. showOverlay: false,
  63. showSideBar: false,
  64. sliderAnimation: new Animated.Value(-SIDEBAR_WIDTH)
  65. };
  66. this._getContainerStyle = this._getContainerStyle.bind(this);
  67. this._onHideMenu = this._onHideMenu.bind(this);
  68. this._setShow = this._setShow.bind(this);
  69. this._setShow(props.show);
  70. }
  71. /**
  72. * Implements the Component's componentDidMount method.
  73. *
  74. * @inheritdoc
  75. */
  76. componentDidMount() {
  77. this._mounted = true;
  78. }
  79. /**
  80. * Implements the Component's componentWillReceiveProps method.
  81. *
  82. * @inheritdoc
  83. */
  84. componentWillReceiveProps(newProps: Props) {
  85. if (newProps.show !== this.props.show) {
  86. this._setShow(newProps.show);
  87. }
  88. }
  89. /**
  90. * Implements React's {@link Component#render()}.
  91. *
  92. * @inheritdoc
  93. */
  94. render() {
  95. return (
  96. <Animated.View
  97. style = { this._getContainerStyle() } >
  98. <View style = { styles.sideMenuContent }>
  99. {
  100. this.props.children
  101. }
  102. </View>
  103. <TouchableWithoutFeedback
  104. onPress = { this._onHideMenu }
  105. style = { styles.sideMenuShadowTouchable } >
  106. <View style = { styles.sideMenuShadow } />
  107. </TouchableWithoutFeedback>
  108. </Animated.View>
  109. );
  110. }
  111. _getContainerStyle: () => Array<Object>
  112. /**
  113. * Assembles a style array for the container.
  114. *
  115. * @private
  116. * @returns {Array<Object>}
  117. */
  118. _getContainerStyle() {
  119. const { sliderAnimation } = this.state;
  120. const { height, width } = Dimensions.get('window');
  121. return [
  122. styles.sideMenuContainer,
  123. {
  124. left: sliderAnimation,
  125. width: this.state.showOverlay
  126. ? Math.max(height, width) + SIDEBAR_WIDTH : SIDEBAR_WIDTH
  127. }
  128. ];
  129. }
  130. _onHideMenu: () => void;
  131. /**
  132. * Hides the menu.
  133. *
  134. * @private
  135. * @returns {void}
  136. */
  137. _onHideMenu() {
  138. this._setShow(false);
  139. const { onHide } = this.props;
  140. if (typeof onHide === 'function') {
  141. onHide();
  142. }
  143. }
  144. _setShow: (boolean) => void;
  145. /**
  146. * Sets the side menu visible or hidden.
  147. *
  148. * @param {boolean} show - The new expected visibility value.
  149. * @private
  150. * @returns {void}
  151. */
  152. _setShow(show) {
  153. if (this.state.showSideBar !== show) {
  154. if (show) {
  155. this.setState({
  156. showOverlay: true
  157. });
  158. }
  159. Animated
  160. .timing(
  161. this.state.sliderAnimation,
  162. { toValue: show ? 0 : -SIDEBAR_WIDTH })
  163. .start(animationState => {
  164. if (animationState.finished && !show) {
  165. this.setState({
  166. showOverlay: false
  167. });
  168. }
  169. });
  170. }
  171. if (this._mounted) {
  172. this.setState({
  173. showSideBar: show
  174. });
  175. }
  176. }
  177. }