react-native-webview.git

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