Modal.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //@flow
  2. import React, { Fragment, useState, useCallback, useRef } from 'react';
  3. import { StyleSheet, View, SafeAreaView, Text, Image, Modal } from 'react-native';
  4. import { captureScreen, captureRef } from 'react-native-view-shot';
  5. import Btn from './Btn';
  6. import Desc from './Desc';
  7. const styles = StyleSheet.create({
  8. root: {
  9. padding: 50,
  10. },
  11. preview: {
  12. marginTop: 20,
  13. width: 200,
  14. height: 200,
  15. borderWidth: 1,
  16. borderColor: '#aaa',
  17. },
  18. modal: {
  19. alignSelf: 'flex-end',
  20. padding: 20,
  21. backgroundColor: '#eee',
  22. },
  23. buttons: {
  24. flexDirection: 'row',
  25. },
  26. });
  27. const ModalExample = () => {
  28. const [opened, setOpened] = useState(false);
  29. const [source, setSource] = useState(null);
  30. const modalRef = useRef();
  31. const onOpenModal = useCallback(() => setOpened(true), []);
  32. const onCapture = useCallback(uri => {
  33. setSource({ uri });
  34. setOpened(false);
  35. }, []);
  36. const onPressCapture = useCallback(() => {
  37. captureScreen().then(onCapture);
  38. }, [onCapture]);
  39. const onPressCaptureModalContent = useCallback(() => {
  40. captureRef(modalRef).then(onCapture);
  41. }, [onCapture]);
  42. return (
  43. <Fragment>
  44. <SafeAreaView>
  45. <View style={styles.root}>
  46. <Desc
  47. desc="We can notice that, in current implementation, react-native-view-shot does not
  48. screenshot Modal as part of a captureScreen."
  49. />
  50. <Btn onPress={onOpenModal} label="Open Modal" />
  51. <Image source={source} style={styles.preview} resizeMode="contain" />
  52. </View>
  53. </SafeAreaView>
  54. <Modal transparent animated animationType="slide" visible={opened}>
  55. <SafeAreaView>
  56. <View ref={modalRef} style={styles.modal}>
  57. <Text>This is a modal</Text>
  58. <View style={styles.buttons}>
  59. <Btn onPress={onPressCapture} label="Capture Screen" />
  60. <Btn onPress={onPressCaptureModalContent} label="Capture This" />
  61. </View>
  62. </View>
  63. </SafeAreaView>
  64. </Modal>
  65. </Fragment>
  66. );
  67. };
  68. ModalExample.navigationOptions = {
  69. title: 'Modal',
  70. };
  71. export default ModalExample;