UNPKG

932 BJavaScriptView Raw
1// @flow
2
3import invariant from 'invariant';
4import * as React from 'react';
5import { NativeModules, findNodeHandle } from 'react-native';
6
7const { RNViewShot } = NativeModules;
8
9type ReactNativeNodeHandle = number;
10
11type SnapshotOptions = {
12 width?: number,
13 height?: number,
14 format?: 'png' | 'jpg' | 'jpeg' | 'webm',
15 quality?: number,
16 result?: 'file' | 'base64' | 'data-uri',
17};
18
19export 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
27function _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}