1 |
|
2 |
|
3 | import invariant from 'invariant';
|
4 | import * as React from 'react';
|
5 | import { NativeModules, findNodeHandle } from 'react-native';
|
6 |
|
7 | const { RNViewShot } = NativeModules;
|
8 |
|
9 | type ReactNativeNodeHandle = number;
|
10 |
|
11 | type SnapshotOptions = {
|
12 | width?: number,
|
13 | height?: number,
|
14 | format?: 'png' | 'jpg' | 'jpeg' | 'webm',
|
15 | quality?: number,
|
16 | result?: 'file' | 'base64' | 'data-uri',
|
17 | };
|
18 |
|
19 | export default async function takeSnapshotAsync(
|
20 | node: ReactNativeNodeHandle | React.Component<*>,
|
21 | options?: SnapshotOptions
|
22 | ): Promise<string> {
|
23 | let handle = typeof node === 'number' ? node : _getNodeHandle(node);
|
24 | return RNViewShot.takeSnapshot(handle, options);
|
25 | }
|
26 |
|
27 | function _getNodeHandle(component: React.Component<*>): ReactNativeNodeHandle {
|
28 | let handle = findNodeHandle(component);
|
29 | invariant(
|
30 | handle != null,
|
31 | `Could not find the React node handle for component to snapshot: %s`,
|
32 | component
|
33 | );
|
34 | return handle;
|
35 | }
|