No Description

index.d.ts 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. * If the value of this property is true, the scroll view stops on multiples
  127. * of the scroll view’s bounds when the user scrolls.
  128. * The default value is false.
  129. * @platform ios
  130. */
  131. pagingEnabled?: boolean,
  132. /**
  133. * The amount by which the web view content is inset from the edges of
  134. * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
  135. * @platform ios
  136. */
  137. contentInset?: Insets;
  138. /**
  139. * Determines the types of data converted to clickable URLs in the web view's content.
  140. * By default only phone numbers are detected.
  141. *
  142. * You can provide one type or an array of many types.
  143. *
  144. * Possible values for `dataDetectorTypes` are:
  145. *
  146. * - `'phoneNumber'`
  147. * - `'link'`
  148. * - `'address'`
  149. * - `'calendarEvent'`
  150. * - `'none'`
  151. * - `'all'`
  152. *
  153. * With the new WebKit implementation, we have three new values:
  154. * - `'trackingNumber'`,
  155. * - `'flightNumber'`,
  156. * - `'lookupSuggestion'`,
  157. *
  158. * @platform ios
  159. */
  160. dataDetectorTypes?: DataDetectorTypes | DataDetectorTypes[];
  161. /**
  162. * Function that allows custom handling of any web view requests. Return
  163. * `true` from the function to continue loading the request and `false`
  164. * to stop loading.
  165. * @platform ios
  166. */
  167. onShouldStartLoadWithRequest?: (event: WebViewNativeEvent) => any;
  168. /**
  169. * Boolean that determines whether HTML5 videos play inline or use the
  170. * native full-screen controller. The default value is `false`.
  171. *
  172. * **NOTE** : In order for video to play inline, not only does this
  173. * property need to be set to `true`, but the video element in the HTML
  174. * document must also include the `webkit-playsinline` attribute.
  175. * @platform ios
  176. */
  177. allowsInlineMediaPlayback?: boolean;
  178. /**
  179. * Hide the accessory view when the keyboard is open. Default is false to be
  180. * backward compatible.
  181. */
  182. hideKeyboardAccessoryView?: boolean;
  183. /**
  184. * If true, this will be able horizontal swipe gestures when using the WKWebView. The default value is `false`.
  185. */
  186. allowsBackForwardNavigationGestures?: boolean
  187. }
  188. export interface AndroidWebViewProps {
  189. onNavigationStateChange?: (event: WebViewNavigation) => any;
  190. onContentSizeChange?: (event: WebViewEvent) => any;
  191. /**
  192. * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
  193. * Sets the overScrollMode. Possible values are:
  194. *
  195. * - `'always'` (default)
  196. * - `'content'`
  197. * - `'never'`
  198. *
  199. * @platform android
  200. */
  201. overScrollMode?: OverScrollModeType;
  202. /**
  203. * Sets whether Geolocation is enabled. The default is false.
  204. * @platform android
  205. */
  206. geolocationEnabled?: boolean;
  207. /**
  208. * Boolean that sets whether JavaScript running in the context of a file
  209. * scheme URL should be allowed to access content from any origin.
  210. * Including accessing content from other file scheme URLs
  211. * @platform android
  212. */
  213. allowUniversalAccessFromFileURLs?: boolean;
  214. /**
  215. * Sets whether the webview allow access to file system.
  216. * @platform android
  217. */
  218. allowFileAccess?: boolean;
  219. /**
  220. * Used on Android only, controls whether form autocomplete data should be saved
  221. * @platform android
  222. */
  223. saveFormDataDisabled?: boolean;
  224. /*
  225. * Used on Android only, controls whether the given list of URL prefixes should
  226. * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
  227. * default activity intent for those URL instead of loading it within the webview.
  228. * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
  229. * @platform android
  230. */
  231. urlPrefixesForDefaultIntent?: string[];
  232. /**
  233. * Boolean value to enable JavaScript in the `WebView`. Used on Android only
  234. * as JavaScript is enabled by default on iOS. The default value is `true`.
  235. * @platform android
  236. */
  237. javaScriptEnabled?: boolean;
  238. /**
  239. * Boolean value to enable third party cookies in the `WebView`. Used on
  240. * Android Lollipop and above only as third party cookies are enabled by
  241. * default on Android Kitkat and below and on iOS. The default value is `true`.
  242. * @platform android
  243. */
  244. thirdPartyCookiesEnabled?: boolean;
  245. /**
  246. * Boolean value to control whether DOM Storage is enabled. Used only in
  247. * Android.
  248. * @platform android
  249. */
  250. domStorageEnabled?: boolean;
  251. /**
  252. * Sets the user-agent for the `WebView`.
  253. * @platform android
  254. */
  255. userAgent?: string;
  256. /**
  257. * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.
  258. *
  259. * Possible values for `mixedContentMode` are:
  260. *
  261. * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin.
  262. * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure.
  263. * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content.
  264. * @platform android
  265. */
  266. mixedContentMode?: 'never' | 'always' | 'compatibility';
  267. }
  268. export interface WebViewSharedProps extends ViewProps, IOSWebViewProps, AndroidWebViewProps {
  269. /**
  270. * @Deprecated. Use `source` instead.
  271. */
  272. url?: string;
  273. /**
  274. * @Deprecated. Use `source` instead.
  275. */
  276. html?: string;
  277. /**
  278. * Loads static html or a uri (with optional headers) in the WebView.
  279. */
  280. source?: WebViewSource;
  281. /**
  282. * Function that returns a view to show if there's an error.
  283. */
  284. renderError?: (errorDomain: string | undefined, errorCode: number, errorDesc: string) => ReactElement<any>; // view to show if there's an error
  285. /**
  286. * Function that returns a loading indicator.
  287. */
  288. renderLoading?: () => ReactElement<any>;
  289. /**
  290. * Function that is invoked when the `WebView` has finished loading.
  291. */
  292. onLoad?: (event: WebViewNavigationEvent) => any;
  293. /**
  294. * Function that is invoked when the `WebView` load succeeds or fails.
  295. */
  296. onLoadEnd?: (event: WebViewNavigationEvent | WebViewErrorEvent) => any;
  297. /**
  298. * Function that is invoked when the `WebView` starts loading.
  299. */
  300. onLoadStart?: (event: WebViewNavigationEvent) => any;
  301. /**
  302. * Function that is invoked when the `WebView` load fails.
  303. */
  304. onError?: (event: WebViewErrorEvent) => any;
  305. /**
  306. * Controls whether to adjust the content inset for web views that are
  307. * placed behind a navigation bar, tab bar, or toolbar. The default value
  308. * is `true`.
  309. */
  310. automaticallyAdjustContentInsets?: boolean;
  311. /**
  312. * Function that is invoked when the `WebView` loading starts or ends.
  313. */
  314. onNavigationStateChange?: (event: WebViewNavigation) => any;
  315. /**
  316. * A function that is invoked when the webview calls `window.postMessage`.
  317. * Setting this property will inject a `postMessage` global into your
  318. * webview, but will still call pre-existing values of `postMessage`.
  319. *
  320. * `window.postMessage` accepts one argument, `data`, which will be
  321. * available on the event object, `event.nativeEvent.data`. `data`
  322. * must be a string.
  323. */
  324. onMessage?: (event: WebViewMessageEvent) => any;
  325. /**
  326. * Function that is invoked when the `WebView` is loading.
  327. */
  328. onLoadProgress?: (event: NativeSyntheticEvent<WebViewProgressEvent>) => any;
  329. /**
  330. * Boolean value that forces the `WebView` to show the loading view
  331. * on the first load.
  332. */
  333. startInLoadingState?: string;
  334. /**
  335. * Set this to provide JavaScript that will be injected into the web page
  336. * when the view loads.
  337. */
  338. injectedJavaScript?: string;
  339. /**
  340. * Boolean that controls whether the web content is scaled to fit
  341. * the view and enables the user to change the scale. The default value
  342. * is `true`.
  343. *
  344. * On iOS, when `useWebKit=true`, this prop will not work.
  345. */
  346. scalesPageToFit?: boolean;
  347. /**
  348. * Boolean that determines whether HTML5 audio and video requires the user
  349. * to tap them before they start playing. The default value is `true`.
  350. */
  351. mediaPlaybackRequiresUserAction?: boolean;
  352. /**
  353. * List of origin strings to allow being navigated to. The strings allow
  354. * wildcards and get matched against *just* the origin (not the full URL).
  355. * If the user taps to navigate to a new page but the new page is not in
  356. * this whitelist, we will open the URL in Safari.
  357. * The default whitelisted origins are "http://*" and "https://*".
  358. */
  359. originWhitelist?: string[];
  360. /**
  361. * Override the native component used to render the WebView. Enables a custom native
  362. * WebView which uses the same JavaScript as the original WebView.
  363. */
  364. nativeConfig?: WebViewNativeConfig;
  365. style?: StyleProp<ViewStyle>;
  366. children?: ReactNode;
  367. }
  368. export class WebView extends Component<WebViewSharedProps> {
  369. public goForward: () => void;
  370. public goBack: () => void;
  371. public reload: () => void;
  372. public stopLoading: () => void;
  373. public postMessage: (msg: string) => void
  374. public injectJavaScript: (js: string) => void
  375. }