react-native-webview.git

index.d.ts 13KB

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