react-native-navigation的迁移库

docs-overlay.mdx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ---
  2. id: docs-overlay
  3. title: Overlay
  4. sidebar_label: Overlay
  5. ---
  6. import Tabs from '@theme/Tabs';
  7. import TabItem from '@theme/TabItem';
  8. import useBaseUrl from '@docusaurus/useBaseUrl';
  9. Overlays are used to layout content on top of all other layout hierarchies, while fitting the screen bounds.
  10. The contents displayed in an Overlay can either be non-obtrusive, like Toasts and Snackbars, or an Alert that blocks all interactions with any content behind it. The view contained within it should be aligned either with absolute position, flex, or with margins, all according to your needs.
  11. ## Handling touch events outside the view
  12. When showing views like Alert or Toast in an Overlay, you would want to configure if you want to allow users to interact with content behind the alert. This is done via the [interceptTouchOutside]() option.
  13. ## Overlay examples
  14. <Tabs
  15. defaultValue="alert"
  16. values={[
  17. { label: 'Alert', value: 'alert', },
  18. { label: 'Toast', value: 'toast', }
  19. ]
  20. }>
  21. <TabItem value='alert'>
  22. The example below demonstrates how to create a simple alert dialog using an Overlay. Touch events outside the alert will be blocked and won't pass through to the content behind the alert since we're specifying `interceptTouchOutside: true` in the static options of the Alert.
  23. [screenshot](/img/alert_android.png)
  24. ```jsx
  25. const React = require('react');
  26. const { Text, Button, View } = require('react-native');
  27. const { Navigation } = require('react-native-navigation');
  28. function Alert({ componentId, title, message }) {
  29. const dismiss = () => Navigation.dismissOverlay(componentId);
  30. return (
  31. <View style={styles.root}>
  32. <View style={styles.alert}>
  33. <Text style={styles.title}>{title}</Text>
  34. <Text style={styles.message}>{message}</Text>
  35. <Button title='OK' onPress={dismiss} />
  36. </View>
  37. </View>
  38. );
  39. }
  40. const styles = {
  41. root: {
  42. flex: 1,
  43. justifyContent: 'center',
  44. alignItems: 'center',
  45. backgroundColor: '#00000050'
  46. },
  47. alert: {
  48. alignItems: 'center',
  49. backgroundColor: 'whitesmoke',
  50. width: 250,
  51. elevation: 4,
  52. padding: 16
  53. },
  54. title: {
  55. fontSize: 18
  56. },
  57. message: {
  58. marginVertical: 8
  59. }
  60. };
  61. Alert.options = (props) => {
  62. return ({
  63. overlay: {
  64. interceptTouchOutside: true
  65. }
  66. });
  67. }
  68. module.exports = Alert;
  69. ```
  70. </TabItem>
  71. <TabItem value='toast'>
  72. The example below demonstrates how to show a Toast using an Overlay. A user can interact with the content behind the toast since we've declared `interceptTouchOutside: false` in the static options of the Alert.
  73. [screenshot](/img/toast_android.png)
  74. ```jsx
  75. const React = require('react');
  76. const { View, Text, StyleSheet, TouchableOpacity } = require('react-native');
  77. const Colors = require('../commons/Colors');
  78. const Navigation = require('../services/Navigation');
  79. const Toast = function ({componentId}) {
  80. return (
  81. <View style={styles.root}>
  82. <View style={styles.toast}>
  83. <Text style={styles.text}>This a very important message!</Text>
  84. <TouchableOpacity style={styles.button} onPress={() => Navigation.dismissOverlay(componentId)}>
  85. <Text style={styles.buttonText}>OK</Text>
  86. </TouchableOpacity>
  87. </View>
  88. </View>
  89. )
  90. }
  91. const styles = StyleSheet.create({
  92. root: {
  93. flex: 1,
  94. flexDirection: 'column-reverse',
  95. },
  96. toast: {
  97. elevation: 2,
  98. flexDirection: 'row',
  99. height: 40,
  100. margin: 16,
  101. borderRadius: 20,
  102. backgroundColor: Colors.accent,
  103. alignItems: 'center',
  104. justifyContent: 'space-between'
  105. },
  106. text: {
  107. color: 'white',
  108. fontSize: 16,
  109. marginLeft: 16
  110. },
  111. button: {
  112. marginRight: 16
  113. },
  114. buttonText: {
  115. color: 'white',
  116. fontSize: 16,
  117. fontWeight: 'bold'
  118. }
  119. });
  120. Toast.options = {
  121. layout: {
  122. componentBackgroundColor: 'transparent'
  123. },
  124. overlay: {
  125. interceptTouchOutside: false
  126. }
  127. }
  128. module.exports = Toast;
  129. ```
  130. </TabItem>
  131. </Tabs>