|
| 1 | +# react-native-share-preview |
| 2 | + |
| 3 | +[](https://badge.fury.io/js/react-native-share-preview) |
| 4 | +[](https://github.com/prettier/prettier) |
| 5 | + |
| 6 | +Animated preview of a React Native page for sharing. |
| 7 | + |
| 8 | +<p align="center"> |
| 9 | +<img src="/.github/images/example.gif" height="500" /> |
| 10 | +</p> |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- Smooth animations by React Native Reanimated |
| 15 | +- Customizable loading (Todo) |
| 16 | +- Customizable ShareBar (Todo) |
| 17 | + |
| 18 | +## Setup |
| 19 | + |
| 20 | +This library is available on npm, install it with: `npm i react-native-share-preview` or `yarn add react-native-share-preview`. |
| 21 | + |
| 22 | +Need also to install the related dependencies: |
| 23 | +`npm i react-native-reanimated react-native-view-shot` or `yarn add react-native-reanimated react-native-view-shot`. |
| 24 | + |
| 25 | +Recommend to use with RN > 0.60 so the auto-linking will handle all the dependencies |
| 26 | + |
| 27 | +For RN < 0.60, you need to manually install `react-native-view-shot` and `react-native-reanimated` |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +1. Import react-native-share-preview: |
| 32 | + |
| 33 | +```typescript |
| 34 | +import {ShareView} from 'react-native-share-preview'; |
| 35 | +``` |
| 36 | + |
| 37 | +2. Create a new page and wrap the content you want to share inside a ShareView: |
| 38 | + |
| 39 | +```tsx |
| 40 | +<ShareView |
| 41 | + onBackPress={handleBack} |
| 42 | + onSharePress={handleShareByType} |
| 43 | + onCaptureError={handleCaptureError}> |
| 44 | + <Text>Your Content</Text> |
| 45 | +</ShareView> |
| 46 | +``` |
| 47 | + |
| 48 | +3. Handle the logic for the selected share option |
| 49 | + |
| 50 | +```javascript |
| 51 | +/** |
| 52 | + * Callback called with the user's selected share option |
| 53 | + */ |
| 54 | +const handleShareType = (type: ShareTypes, screenshotUri: string) => { |
| 55 | + return new Promise((resolve, _) => { |
| 56 | + // Call native implementation with your share logic |
| 57 | + // For example you can use react-native-share or your favorite share library |
| 58 | + return Share.open({type, filename: screenshotUri}).catch(err => { |
| 59 | + // Handle share error |
| 60 | + Alert.alert(err.message); |
| 61 | + throw err; |
| 62 | + }); |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | +const handleCaptureError = (message: string) => { |
| 67 | + Alert.alert(message); |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +## A complete example |
| 72 | + |
| 73 | +For a more complex example take a look at the `/example` directory. |
| 74 | + |
| 75 | +## Available props |
| 76 | + |
| 77 | +| Name | Type | Default | Description | |
| 78 | +| -------------- | -------------- | ------------ | ------------------------------------------ | |
| 79 | +| outerBgColor | string | #dddddd | Background color of the share page | |
| 80 | +| innerBgColor | string | #f7f7f7 | Background color of the share content view | |
| 81 | +| shareBar | ReactNode | <ShareBar /> | Custom bottom Share Bar component | |
| 82 | +| shareBarHeight | number | 140 | Height of the bottom Share Bar | |
| 83 | +| captureOptions | CaptureOptions | {} | Capture options for ViewShot component | |
| 84 | +| onBackPress | Function | undefined | Handle share cancel and page go back | |
| 85 | +| onSharePress | Function | undefined | Handle user selected share option action | |
| 86 | +| onCaptureError | Function | undefined | Handle capture view Screenshot error | |
| 87 | + |
| 88 | +## Frequently Asked Questions |
| 89 | + |
| 90 | +### Why on iOS i can't capture the whole screen? |
| 91 | + |
| 92 | +This is problem with the `react-native-view-shot` library, the fix has not been merged yet and can be found in this PR [#209](https://github.com/gre/react-native-view-shot/pull/209/files). |
| 93 | + |
| 94 | +## Acknowledgements |
| 95 | + |
| 96 | +Thanks [@software-mansion](https://github.com/software-mansion/react-native-reanimated) for react-native-reanimated and [@gre](https://github.com/gre/react-native-view-shot) for react-native-view-shot! |
| 97 | + |
| 98 | +Pull requests, feedbacks and suggestions are welcome! |
0 commit comments