react-native-webview.git

index.d.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import { ComponentType, ReactElement, ReactNode, Component } from 'react';
  2. import { Insets, NativeSyntheticEvent, StyleProp, ViewProps, ViewStyle } from 'react-native';
  3. export interface WebViewNativeEvent {
  4. readonly url: string;
  5. readonly loading: boolean;
  6. readonly title: string;
  7. readonly canGoBack: boolean;
  8. readonly canGoForward: boolean;
  9. }
  10. export interface WebViewProgressEvent extends WebViewNativeEvent {
  11. readonly progress: number;
  12. }
  13. export interface WebViewNavigation extends WebViewNativeEvent {
  14. readonly navigationType:
  15. | 'click'
  16. | 'formsubmit'
  17. | 'backforward'
  18. | 'reload'
  19. | 'formresubmit'
  20. | 'other';
  21. }
  22. export interface WebViewMessage extends WebViewNativeEvent {
  23. readonly data: string;
  24. }
  25. export interface WebViewError extends WebViewNativeEvent {
  26. readonly domain?: string;
  27. readonly code: number;
  28. readonly description: string;
  29. }
  30. export type WebViewEvent = NativeSyntheticEvent<WebViewNativeEvent>;
  31. export type WebViewNavigationEvent = NativeSyntheticEvent<WebViewNavigation>;
  32. export type WebViewMessageEvent = NativeSyntheticEvent<WebViewMessage>;
  33. export type WebViewErrorEvent = NativeSyntheticEvent<WebViewError>;
  34. export type DataDetectorTypes =
  35. | 'phoneNumber'
  36. | 'link'
  37. | 'address'
  38. | 'calendarEvent'
  39. | 'trackingNumber'
  40. | 'flightNumber'
  41. | 'lookupSuggestion'
  42. | 'none'
  43. | 'all';
  44. export type OverScrollModeType = 'always' | 'content' | 'never';
  45. export interface WebViewSourceUri {
  46. /**
  47. * The URI to load in the `WebView`. Can be a local or remote file.
  48. */
  49. uri?: string;
  50. /**
  51. * The HTTP Method to use. Defaults to GET if not specified.
  52. * NOTE: On Android, only GET and POST are supported.
  53. */
  54. method?: string;
  55. /**
  56. * Additional HTTP headers to send with the request.
  57. * NOTE: On Android, this can only be used with GET requests.
  58. */
  59. headers?: {[key: string]: string};
  60. /**
  61. * The HTTP body to send with the request. This must be a valid
  62. * UTF-8 string, and will be sent exactly as specified, with no
  63. * additional encoding (e.g. URL-escaping or base64) applied.
  64. * NOTE: On Android, this can only be used with POST requests.
  65. */
  66. body?: string;
  67. }
  68. export interface WebViewSourceHtml {
  69. /**
  70. * A static HTML page to display in the WebView.
  71. */
  72. html?: string;
  73. /**
  74. * The base URL to be used for any relative links in the HTML.
  75. */
  76. baseUrl?: string;
  77. }
  78. export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
  79. export interface WebViewNativeConfig {
  80. /*
  81. * The native component used to render the WebView.
  82. */
  83. component?: ComponentType<WebViewSharedProps>;
  84. /*
  85. * Set props directly on the native component WebView. Enables custom props which the
  86. * original WebView doesn't pass through.
  87. */
  88. props?: any;
  89. /*
  90. * Set the ViewManager to use for communication with the native side.
  91. * @platform ios
  92. */
  93. viewManager?: any;
  94. }
  95. export interface IOSWebViewProps {
  96. /**
  97. * If true, use WKWebView instead of UIWebView.
  98. * @platform ios
  99. */
  100. useWebKit?: boolean;
  101. /**
  102. * Boolean value that determines whether the web view bounces
  103. * when it reaches the edge of the content. The default value is `true`.
  104. * @platform ios
  105. */
  106. bounces?: boolean;
  107. /**
  108. * A floating-point number that determines how quickly the scroll view
  109. * decelerates after the user lifts their finger. You may also use the
  110. * string shortcuts `"normal"` and `"fast"` which match the underlying iOS
  111. * settings for `UIScrollViewDecelerationRateNormal` and
  112. * `UIScrollViewDecelerationRateFast` respectively:
  113. *
  114. * - normal: 0.998
  115. * - fast: 0.99 (the default for iOS web view)
  116. * @platform ios
  117. */
  118. decelerationRate?: 'fast' | 'normal' | number;
  119. /**
  120. * Boolean value that determines whether scrolling is enabled in the
  121. * `WebView`. The default value is `true`.
  122. * @platform ios
  123. */
  124. scrollEnabled?: boolean;
  125. /**
  126. * The amount by which the web view content is inset from the edges of
  127. * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
  128. * @platform ios
  129. */
  130. contentInset?: Insets;
  131. /**
  132. * Determines the types of data converted to clickable URLs in the web view's content.
  133. * By default only phone numbers are detected.
  134. *
  135. * You can provide one type or an array of many types.
  136. *
  137. * Possible values for `dataDetectorTypes` are:
  138. *
  139. * - `'phoneNumber'`
  140. * - `'link'`
  141. * - `'address'`
  142. * - `'calendarEvent'`
  143. * - `'none'`
  144. * - `'all'`
  145. *
  146. * With the new WebKit implementation, we have three new values:
  147. * - `'trackingNumber'`,
  148. * - `'flightNumber'`,
  149. * - `'lookupSuggestion'`,
  150. *
  151. * @platform ios
  152. */
  153. dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
  154. /**
  155. * Function that allows custom handling of any web view requests. Return
  156. * `true` from the function to continue loading the request and `false`
  157. * to stop loading.
  158. * @platform ios
  159. */
  160. onShouldStartLoadWithRequest?: (event: WebViewNativeEvent) => any;
  161. /**
  162. * Boolean that determines whether HTML5 videos play inline or use the
  163. * native full-screen controller. The default value is `false`.
  164. *
  165. * **NOTE** : In order for video to play inline, not only does this
  166. * property need to be set to `true`, but the video element in the HTML
  167. * document must also include the `webkit-playsinline` attribute.
  168. * @platform ios
  169. */
  170. allowsInlineMediaPlayback?: boolean;
  171. /**
  172. * Hide the accessory view when the keyboard is open. Default is false to be
  173. * backward compatible.
  174. */
  175. hideKeyboardAccessoryView?: boolean;
  176. /**
  177. * If true, this will be able horizontal swipe gestures when using the WKWebView. The default value is `false`.
  178. */
  179. allowsBackForwardNavigationGestures?: boolean
  180. }
  181. export interface AndroidWebViewProps {
  182. onNavigationStateChange?: (event: WebViewNavigation) => any;
  183. onContentSizeChange?: (event: WebViewEvent) => any;
  184. /**
  185. * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
  186. * Sets the overScrollMode. Possible values are:
  187. *
  188. * - `'always'` (default)
  189. * - `'content'`
  190. * - `'never'`
  191. *
  192. * @platform android
  193. */
  194. overScrollMode?: OverScrollModeType;
  195. /**
  196. * Sets whether Geolocation is enabled. The default is false.
  197. * @platform android
  198. */
  199. geolocationEnabled?: boolean;
  200. /**
  201. * Boolean that sets whether JavaScript running in the context of a file
  202. * scheme URL should be allowed to access content from any origin.
  203. * Including accessing content from other file scheme URLs
  204. * @platform android
  205. */
  206. allowUniversalAccessFromFileURLs?: boolean;
  207. /**
  208. * Sets whether the webview allow access to file system.
  209. * @platform android
  210. */
  211. allowFileAccess?: boolean;
  212. /**
  213. * Used on Android only, controls whether form autocomplete data should be saved
  214. * @platform android
  215. */
  216. saveFormDataDisabled?: boolean;
  217. /*
  218. * Used on Android only, controls whether the given list of URL prefixes should
  219. * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
  220. * default activity intent for those URL instead of loading it within the webview.
  221. * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
  222. * @platform android
  223. */
  224. urlPrefixesForDefaultIntent?: string[];
  225. /**
  226. * Boolean value to enable JavaScript in the `WebView`. Used on Android only
  227. * as JavaScript is enabled by default on iOS. The default value is `true`.
  228. * @platform android
  229. */
  230. javaScriptEnabled?: boolean;
  231. /**
  232. * Boolean value to enable third party cookies in the `WebView`. Used on
  233. * Android Lollipop and above only as third party cookies are enabled by
  234. * default on Android Kitkat and below and on iOS. The default value is `true`.
  235. * @platform android
  236. */
  237. thirdPartyCookiesEnabled?: boolean;
  238. /**
  239. * Boolean value to control whether DOM Storage is enabled. Used only in
  240. * Android.
  241. * @platform android
  242. */
  243. domStorageEnabled?: boolean;
  244. /**
  245. * Sets the user-agent for the `WebView`.
  246. * @platform android
  247. */
  248. userAgent?: string;
  249. /**
  250. * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.
  251. *
  252. * Possible values for `mixedContentMode` are:
  253. *
  254. * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin.
  255. * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure.
  256. * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content.
  257. * @platform android
  258. */
  259. mixedContentMode?: 'never' | 'always' | 'compatibility';
  260. }
  261. export interface WebViewSharedProps extends ViewProps, IOSWebViewProps, AndroidWebViewProps {
  262. /**
  263. * @Deprecated. Use `source` instead.
  264. */
  265. url?: string;
  266. /**
  267. * @Deprecated. Use `source` instead.
  268. */
  269. html?: string;
  270. /**
  271. * Loads static html or a uri (with optional headers) in the WebView.
  272. */
  273. source?: WebViewSource;
  274. /**
  275. * Function that returns a view to show if there's an error.
  276. */
  277. renderError?: (errorDomain: string | undefined, errorCode: number, errorDesc: string) => ReactElement<any>; // view to show if there's an error
  278. /**
  279. * Function that returns a loading indicator.
  280. */
  281. renderLoading?: () => ReactElement<any>;
  282. /**
  283. * Function that is invoked when the `WebView` has finished loading.
  284. */
  285. onLoad?: (event: WebViewNavigationEvent) => any;
  286. /**
  287. * Function that is invoked when the `WebView` load succeeds or fails.
  288. */
  289. onLoadEnd?: (event: WebViewNavigationEvent | WebViewErrorEvent) => any;
  290. /**
  291. * Function that is invoked when the `WebView` starts loading.
  292. */
  293. onLoadStart?: (event: WebViewNavigationEvent) => any;
  294. /**
  295. * Function that is invoked when the `WebView` load fails.
  296. */
  297. onError?: (event: WebViewErrorEvent) => any;
  298. /**
  299. * Controls whether to adjust the content inset for web views that are
  300. * placed behind a navigation bar, tab bar, or toolbar. The default value
  301. * is `true`.
  302. */
  303. automaticallyAdjustContentInsets?: boolean;
  304. /**
  305. * Function that is invoked when the `WebView` loading starts or ends.
  306. */
  307. onNavigationStateChange?: (event: WebViewNavigation) => any;
  308. /**
  309. * A function that is invoked when the webview calls `window.postMessage`.
  310. * Setting this property will inject a `postMessage` global into your
  311. * webview, but will still call pre-existing values of `postMessage`.
  312. *
  313. * `window.postMessage` accepts one argument, `data`, which will be
  314. * available on the event object, `event.nativeEvent.data`. `data`
  315. * must be a string.
  316. */
  317. onMessage?: (event: WebViewMessageEvent) => any;
  318. /**
  319. * Function that is invoked when the `WebView` is loading.
  320. */
  321. onLoadProgress?: (event: NativeSyntheticEvent<WebViewProgressEvent>) => any;
  322. /**
  323. * Boolean value that forces the `WebView` to show the loading view
  324. * on the first load.
  325. */
  326. startInLoadingState?: string;
  327. /**
  328. * Set this to provide JavaScript that will be injected into the web page
  329. * when the view loads.
  330. */
  331. injectedJavaScript?: string;
  332. /**
  333. * Boolean that controls whether the web content is scaled to fit
  334. * the view and enables the user to change the scale. The default value
  335. * is `true`.
  336. *
  337. * On iOS, when `useWebKit=true`, this prop will not work.
  338. */
  339. scalesPageToFit?: boolean;
  340. /**
  341. * Boolean that determines whether HTML5 audio and video requires the user
  342. * to tap them before they start playing. The default value is `true`.
  343. */
  344. mediaPlaybackRequiresUserAction?: boolean;
  345. /**
  346. * List of origin strings to allow being navigated to. The strings allow
  347. * wildcards and get matched against *just* the origin (not the full URL).
  348. * If the user taps to navigate to a new page but the new page is not in
  349. * this whitelist, we will open the URL in Safari.
  350. * The default whitelisted origins are "http://*" and "https://*".
  351. */
  352. originWhitelist?: string[];
  353. /**
  354. * Override the native component used to render the WebView. Enables a custom native
  355. * WebView which uses the same JavaScript as the original WebView.
  356. */
  357. nativeConfig?: WebViewNativeConfig;
  358. style?: StyleProp<ViewStyle>;
  359. children?: ReactNode;
  360. }
  361. export class WebView extends Component<WebViewSharedProps> {
  362. public goForward: () => void;
  363. public goBack: () => void;
  364. public reload: () => void;
  365. public stopLoading: () => void;
  366. public injectJavaScript: (js: string) => void
  367. }