No Description

index.d.ts 13KB

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