StackWithTranslucentHeader.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @flow
  3. */
  4. import type {
  5. NavigationScreenProp,
  6. NavigationEventSubscription,
  7. } from 'react-navigation';
  8. import { isIphoneX } from 'react-native-iphone-x-helper';
  9. import * as React from 'react';
  10. import { BlurView, Constants } from 'expo';
  11. import {
  12. Dimensions,
  13. Platform,
  14. ScrollView,
  15. StatusBar,
  16. StyleSheet,
  17. View,
  18. } from 'react-native';
  19. import { Header, createStackNavigator } from 'react-navigation';
  20. import invariant from 'invariant';
  21. import SampleText from './SampleText';
  22. import { Button } from './commonComponents/ButtonWithMargin';
  23. import { HeaderButtons } from './commonComponents/HeaderButtons';
  24. type MyNavScreenProps = {
  25. navigation: NavigationScreenProp<*>,
  26. banner: React.Node,
  27. };
  28. class MyNavScreen extends React.Component<MyNavScreenProps> {
  29. render() {
  30. const { navigation, banner } = this.props;
  31. const { push, replace, popToTop, pop } = navigation;
  32. invariant(
  33. push && replace && popToTop && pop,
  34. 'missing action creators for StackNavigator'
  35. );
  36. return (
  37. <ScrollView style={{ flex: 1 }} {...this.getHeaderInset()}>
  38. <SampleText>{banner}</SampleText>
  39. <Button
  40. onPress={() => push('Profile', { name: 'Jane' })}
  41. title="Push a profile screen"
  42. />
  43. <Button
  44. onPress={() => navigation.navigate('Photos', { name: 'Jane' })}
  45. title="Navigate to a photos screen"
  46. />
  47. <Button
  48. onPress={() => replace('Profile', { name: 'Lucy' })}
  49. title="Replace with profile"
  50. />
  51. <Button onPress={() => popToTop()} title="Pop to top" />
  52. <Button onPress={() => pop()} title="Pop" />
  53. <Button onPress={() => navigation.goBack(null)} title="Go back" />
  54. <StatusBar barStyle="default" />
  55. </ScrollView>
  56. );
  57. }
  58. // Inset to compensate for navigation bar being transparent.
  59. // And improved abstraction for this will be built in to react-navigation
  60. // at some point.
  61. getHeaderInset() {
  62. const NOTCH_HEIGHT = isIphoneX() ? 25 : 0;
  63. // $FlowIgnore: we will remove the HEIGHT static soon enough
  64. const BASE_HEADER_HEIGHT = Header.HEIGHT;
  65. const HEADER_HEIGHT =
  66. Platform.OS === 'ios'
  67. ? BASE_HEADER_HEIGHT + NOTCH_HEIGHT
  68. : BASE_HEADER_HEIGHT + Constants.statusBarHeight;
  69. return Platform.select({
  70. ios: {
  71. contentInset: { top: HEADER_HEIGHT },
  72. contentOffset: { y: -HEADER_HEIGHT },
  73. },
  74. android: {
  75. contentContainerStyle: {
  76. paddingTop: HEADER_HEIGHT,
  77. },
  78. },
  79. });
  80. }
  81. }
  82. type MyHomeScreenProps = {
  83. navigation: NavigationScreenProp<*>,
  84. };
  85. class MyHomeScreen extends React.Component<MyHomeScreenProps> {
  86. static navigationOptions = {
  87. title: 'Welcome',
  88. };
  89. _s0: NavigationEventSubscription;
  90. _s1: NavigationEventSubscription;
  91. _s2: NavigationEventSubscription;
  92. _s3: NavigationEventSubscription;
  93. componentDidMount() {
  94. this._s0 = this.props.navigation.addListener('willFocus', this._onWF);
  95. this._s1 = this.props.navigation.addListener('didFocus', this._onDF);
  96. this._s2 = this.props.navigation.addListener('willBlur', this._onWB);
  97. this._s3 = this.props.navigation.addListener('didBlur', this._onDB);
  98. }
  99. componentWillUnmount() {
  100. this._s0.remove();
  101. this._s1.remove();
  102. this._s2.remove();
  103. this._s3.remove();
  104. }
  105. _onWF = a => {
  106. console.log('_willFocus HomeScreen', a);
  107. };
  108. _onDF = a => {
  109. console.log('_didFocus HomeScreen', a);
  110. };
  111. _onWB = a => {
  112. console.log('_willBlur HomeScreen', a);
  113. };
  114. _onDB = a => {
  115. console.log('_didBlur HomeScreen', a);
  116. };
  117. render() {
  118. const { navigation } = this.props;
  119. return <MyNavScreen banner="Home Screen" navigation={navigation} />;
  120. }
  121. }
  122. type MyPhotosScreenProps = {
  123. navigation: NavigationScreenProp<*>,
  124. };
  125. class MyPhotosScreen extends React.Component<MyPhotosScreenProps> {
  126. static navigationOptions = {
  127. title: 'Photos',
  128. };
  129. _s0: NavigationEventSubscription;
  130. _s1: NavigationEventSubscription;
  131. _s2: NavigationEventSubscription;
  132. _s3: NavigationEventSubscription;
  133. componentDidMount() {
  134. this._s0 = this.props.navigation.addListener('willFocus', this._onWF);
  135. this._s1 = this.props.navigation.addListener('didFocus', this._onDF);
  136. this._s2 = this.props.navigation.addListener('willBlur', this._onWB);
  137. this._s3 = this.props.navigation.addListener('didBlur', this._onDB);
  138. }
  139. componentWillUnmount() {
  140. this._s0.remove();
  141. this._s1.remove();
  142. this._s2.remove();
  143. this._s3.remove();
  144. }
  145. _onWF = a => {
  146. console.log('_willFocus PhotosScreen', a);
  147. };
  148. _onDF = a => {
  149. console.log('_didFocus PhotosScreen', a);
  150. };
  151. _onWB = a => {
  152. console.log('_willBlur PhotosScreen', a);
  153. };
  154. _onDB = a => {
  155. console.log('_didBlur PhotosScreen', a);
  156. };
  157. render() {
  158. const { navigation } = this.props;
  159. return (
  160. <MyNavScreen
  161. banner={`${navigation.state.params.name}'s Photos`}
  162. navigation={navigation}
  163. />
  164. );
  165. }
  166. }
  167. const MyProfileScreen = ({ navigation }) => (
  168. <MyNavScreen
  169. banner={`${navigation.state.params.mode === 'edit' ? 'Now Editing ' : ''}${
  170. navigation.state.params.name
  171. }'s Profile`}
  172. navigation={navigation}
  173. />
  174. );
  175. MyProfileScreen.navigationOptions = props => {
  176. const { navigation } = props;
  177. const { state, setParams } = navigation;
  178. const { params } = state;
  179. return {
  180. headerBackImage: params.headerBackImage,
  181. headerTitle: `${params.name}'s Profile!`,
  182. // Render a button on the right side of the header.
  183. // When pressed switches the screen to edit mode.
  184. headerRight: (
  185. <HeaderButtons>
  186. <HeaderButtons.Item
  187. title={params.mode === 'edit' ? 'Done' : 'Edit'}
  188. onPress={() =>
  189. setParams({ mode: params.mode === 'edit' ? '' : 'edit' })
  190. }
  191. />
  192. </HeaderButtons>
  193. ),
  194. };
  195. };
  196. const StackWithTranslucentHeader = createStackNavigator(
  197. {
  198. Home: {
  199. screen: MyHomeScreen,
  200. },
  201. Profile: {
  202. path: 'people/:name',
  203. screen: MyProfileScreen,
  204. },
  205. Photos: {
  206. path: 'photos/:name',
  207. screen: MyPhotosScreen,
  208. },
  209. },
  210. {
  211. headerTransitionPreset: 'uikit',
  212. navigationOptions: {
  213. headerTransparent: true,
  214. headerStyle: {
  215. borderBottomWidth: StyleSheet.hairlineWidth,
  216. borderBottomColor: '#A7A7AA',
  217. },
  218. headerBackground: Platform.select({
  219. ios: <BlurView style={{ flex: 1 }} intensity={98} />,
  220. android: (
  221. <View style={{ flex: 1, backgroundColor: 'rgba(255,255,255,0.7)' }} />
  222. ),
  223. }),
  224. },
  225. }
  226. );
  227. export default StackWithTranslucentHeader;