react-native-webview.git

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. export interface AndroidWebViewProps {
  178. onNavigationStateChange?: (event: WebViewNavigation) => any;
  179. onContentSizeChange?: (event: WebViewEvent) => any;
  180. /**
  181. * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
  182. * Sets the overScrollMode. Possible values are:
  183. *
  184. * - `'always'` (default)
  185. * - `'content'`
  186. * - `'never'`
  187. *
  188. * @platform android
  189. */
  190. overScrollMode?: OverScrollModeType;
  191. /**
  192. * Sets whether Geolocation is enabled. The default is false.
  193. * @platform android
  194. */
  195. geolocationEnabled?: boolean;
  196. /**
  197. * Boolean that sets whether JavaScript running in the context of a file
  198. * scheme URL should be allowed to access content from any origin.
  199. * Including accessing content from other file scheme URLs
  200. * @platform android
  201. */
  202. allowUniversalAccessFromFileURLs?: boolean;
  203. /**
  204. * Sets whether the webview allow access to file system.
  205. * @platform android
  206. */
  207. allowFileAccess?: boolean;
  208. /**
  209. * Used on Android only, controls whether form autocomplete data should be saved
  210. * @platform android
  211. */
  212. saveFormDataDisabled?: boolean;
  213. /*
  214. * Used on Android only, controls whether the given list of URL prefixes should
  215. * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
  216. * default activity intent for those URL instead of loading it within the webview.
  217. * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
  218. * @platform android
  219. */
  220. urlPrefixesForDefaultIntent?: string[];
  221. /**
  222. * Boolean value to enable JavaScript in the `WebView`. Used on Android only
  223. * as JavaScript is enabled by default on iOS. The default value is `true`.
  224. * @platform android
  225. */
  226. javaScriptEnabled?: boolean;
  227. /**
  228. * Boolean value to enable third party cookies in the `WebView`. Used on
  229. * Android Lollipop and above only as third party cookies are enabled by
  230. * default on Android Kitkat and below and on iOS. The default value is `true`.
  231. * @platform android
  232. */
  233. thirdPartyCookiesEnabled?: boolean;
  234. /**
  235. * Boolean value to control whether DOM Storage is enabled. Used only in
  236. * Android.
  237. * @platform android
  238. */
  239. domStorageEnabled?: boolean;
  240. /**
  241. * Sets the user-agent for the `WebView`.
  242. * @platform android
  243. */
  244. userAgent?: string;
  245. /**
  246. * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.
  247. *
  248. * Possible values for `mixedContentMode` are:
  249. *
  250. * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin.
  251. * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure.
  252. * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content.
  253. * @platform android
  254. */
  255. mixedContentMode?: 'never' | 'always' | 'compatibility';
  256. }
  257. export interface WebViewSharedProps extends ViewProps, IOSWebViewProps, AndroidWebViewProps {
  258. /**
  259. * @Deprecated. Use `source` instead.
  260. */
  261. url?: string;
  262. /**
  263. * @Deprecated. Use `source` instead.
  264. */
  265. html?: string;
  266. /**
  267. * Loads static html or a uri (with optional headers) in the WebView.
  268. */
  269. source?: WebViewSource;
  270. /**
  271. * Function that returns a view to show if there's an error.
  272. */
  273. renderError?: (errorDomain: string | undefined, errorCode: number, errorDesc: string) => ReactElement<any>; // view to show if there's an error
  274. /**
  275. * Function that returns a loading indicator.
  276. */
  277. renderLoading?: () => ReactElement<any>;
  278. /**
  279. * Function that is invoked when the `WebView` has finished loading.
  280. */
  281. onLoad?: (event: WebViewNavigationEvent) => any;
  282. /**
  283. * Function that is invoked when the `WebView` load succeeds or fails.
  284. */
  285. onLoadEnd?: (event: WebViewNavigationEvent | WebViewErrorEvent) => any;
  286. /**
  287. * Function that is invoked when the `WebView` starts loading.
  288. */
  289. onLoadStart?: (event: WebViewNavigationEvent) => any;
  290. /**
  291. * Function that is invoked when the `WebView` load fails.
  292. */
  293. onError?: (event: WebViewErrorEvent) => any;
  294. /**
  295. * Controls whether to adjust the content inset for web views that are
  296. * placed behind a navigation bar, tab bar, or toolbar. The default value
  297. * is `true`.
  298. */
  299. automaticallyAdjustContentInsets?: boolean;
  300. /**
  301. * Function that is invoked when the `WebView` loading starts or ends.
  302. */
  303. onNavigationStateChange?: (event: WebViewNavigation) => any;
  304. /**
  305. * A function that is invoked when the webview calls `window.postMessage`.
  306. * Setting this property will inject a `postMessage` global into your
  307. * webview, but will still call pre-existing values of `postMessage`.
  308. *
  309. * `window.postMessage` accepts one argument, `data`, which will be
  310. * available on the event object, `event.nativeEvent.data`. `data`
  311. * must be a string.
  312. */
  313. onMessage?: (event: WebViewMessageEvent) => any;
  314. /**
  315. * Function that is invoked when the `WebView` is loading.
  316. */
  317. onLoadProgress?: (event: NativeSyntheticEvent<WebViewProgressEvent>) => any;
  318. /**
  319. * Boolean value that forces the `WebView` to show the loading view
  320. * on the first load.
  321. */
  322. startInLoadingState?: string;
  323. /**
  324. * Set this to provide JavaScript that will be injected into the web page
  325. * when the view loads.
  326. */
  327. injectedJavaScript?: string;
  328. /**
  329. * Boolean that controls whether the web content is scaled to fit
  330. * the view and enables the user to change the scale. The default value
  331. * is `true`.
  332. *
  333. * On iOS, when `useWebKit=true`, this prop will not work.
  334. */
  335. scalesPageToFit?: boolean;
  336. /**
  337. * Boolean that determines whether HTML5 audio and video requires the user
  338. * to tap them before they start playing. The default value is `true`.
  339. */
  340. mediaPlaybackRequiresUserAction?: boolean;
  341. /**
  342. * List of origin strings to allow being navigated to. The strings allow
  343. * wildcards and get matched against *just* the origin (not the full URL).
  344. * If the user taps to navigate to a new page but the new page is not in
  345. * this whitelist, we will open the URL in Safari.
  346. * The default whitelisted origins are "http://*" and "https://*".
  347. */
  348. originWhitelist?: string[];
  349. /**
  350. * Override the native component used to render the WebView. Enables a custom native
  351. * WebView which uses the same JavaScript as the original WebView.
  352. */
  353. nativeConfig?: WebViewNativeConfig;
  354. style?: StyleProp<ViewStyle>;
  355. children?: ReactNode;
  356. }
  357. export class WebView extends Component<WebViewSharedProps> {
  358. public goForward: () => void;
  359. public goBack: () => void;
  360. public reload: () => void;
  361. public stopLoading: () => void;
  362. }