Sin descripción

WebViewTypes.ts 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /* eslint-disable react/no-multi-comp */
  2. import { ReactElement, Component } from 'react';
  3. import {
  4. NativeSyntheticEvent,
  5. ViewProps,
  6. NativeMethodsMixin,
  7. Constructor,
  8. UIManagerStatic,
  9. } from 'react-native';
  10. export interface WebViewCommands {
  11. goForward: Function;
  12. goBack: Function;
  13. reload: Function;
  14. stopLoading: Function;
  15. postMessage: Function;
  16. injectJavaScript: Function;
  17. loadUrl: Function;
  18. }
  19. export interface CustomUIManager extends UIManagerStatic {
  20. getViewManagerConfig?: (
  21. name: string,
  22. ) => {
  23. Commands: WebViewCommands;
  24. };
  25. dispatchViewManagerCommand: (
  26. viewHandle: number,
  27. command: Function,
  28. params: object | null,
  29. ) => void;
  30. RNCUIWebView: {
  31. Commands: WebViewCommands;
  32. };
  33. RNCWKWebView: {
  34. Commands: WebViewCommands;
  35. };
  36. RNCWebView: {
  37. Commands: WebViewCommands;
  38. };
  39. }
  40. type WebViewState = 'IDLE' | 'LOADING' | 'ERROR';
  41. interface BaseState {
  42. viewState: WebViewState;
  43. }
  44. interface NormalState extends BaseState {
  45. viewState: 'IDLE' | 'LOADING';
  46. lastErrorEvent: WebViewError | null;
  47. }
  48. interface ErrorState extends BaseState {
  49. viewState: 'ERROR';
  50. lastErrorEvent: WebViewError;
  51. }
  52. export type State = NormalState | ErrorState;
  53. // eslint-disable-next-line react/prefer-stateless-function
  54. declare class NativeWebViewIOSComponent extends Component<
  55. IOSNativeWebViewProps
  56. > {}
  57. declare const NativeWebViewIOSBase: Constructor<NativeMethodsMixin> &
  58. typeof NativeWebViewIOSComponent;
  59. export class NativeWebViewIOS extends NativeWebViewIOSBase {}
  60. // eslint-disable-next-line react/prefer-stateless-function
  61. declare class NativeWebViewAndroidComponent extends Component<
  62. AndroidNativeWebViewProps
  63. > {}
  64. declare const NativeWebViewAndroidBase: Constructor<NativeMethodsMixin> &
  65. typeof NativeWebViewAndroidComponent;
  66. export class NativeWebViewAndroid extends NativeWebViewAndroidBase {}
  67. export interface ContentInsetProp {
  68. top?: number;
  69. left?: number;
  70. bottom?: number;
  71. right?: number;
  72. }
  73. export interface WebViewNativeEvent {
  74. url: string;
  75. loading: boolean;
  76. title: string;
  77. canGoBack: boolean;
  78. canGoForward: boolean;
  79. lockIdentifier: number;
  80. }
  81. export interface WebViewProgressEvent extends WebViewNativeEvent {
  82. progress: number;
  83. }
  84. export interface WebViewNavigation extends WebViewNativeEvent {
  85. navigationType:
  86. | 'click'
  87. | 'formsubmit'
  88. | 'backforward'
  89. | 'reload'
  90. | 'formresubmit'
  91. | 'other';
  92. }
  93. export type DecelerationRateConstant = 'normal' | 'fast';
  94. export interface WebViewMessage extends WebViewNativeEvent {
  95. data: string;
  96. }
  97. export interface WebViewError extends WebViewNativeEvent {
  98. /**
  99. * `domain` is only used on iOS
  100. */
  101. domain?: string;
  102. code: number;
  103. description: string;
  104. }
  105. export type WebViewEvent = NativeSyntheticEvent<WebViewNativeEvent>;
  106. export type WebViewNavigationEvent = NativeSyntheticEvent<WebViewNavigation>;
  107. export type WebViewMessageEvent = NativeSyntheticEvent<WebViewMessage>;
  108. export type WebViewErrorEvent = NativeSyntheticEvent<WebViewError>;
  109. export type DataDetectorTypes
  110. = | 'phoneNumber'
  111. | 'link'
  112. | 'address'
  113. | 'calendarEvent'
  114. | 'trackingNumber'
  115. | 'flightNumber'
  116. | 'lookupSuggestion'
  117. | 'none'
  118. | 'all';
  119. export type OverScrollModeType = 'always' | 'content' | 'never';
  120. export interface WebViewSourceUri {
  121. /**
  122. * The URI to load in the `WebView`. Can be a local or remote file.
  123. */
  124. uri: string;
  125. /**
  126. * The HTTP Method to use. Defaults to GET if not specified.
  127. * NOTE: On Android, only GET and POST are supported.
  128. */
  129. method?: string;
  130. /**
  131. * Additional HTTP headers to send with the request.
  132. * NOTE: On Android, this can only be used with GET requests.
  133. */
  134. headers?: Object;
  135. /**
  136. * The HTTP body to send with the request. This must be a valid
  137. * UTF-8 string, and will be sent exactly as specified, with no
  138. * additional encoding (e.g. URL-escaping or base64) applied.
  139. * NOTE: On Android, this can only be used with POST requests.
  140. */
  141. body?: string;
  142. }
  143. export interface WebViewSourceHtml {
  144. /**
  145. * A static HTML page to display in the WebView.
  146. */
  147. html: string;
  148. /**
  149. * The base URL to be used for any relative links in the HTML.
  150. */
  151. baseUrl?: string;
  152. }
  153. export type WebViewSource = WebViewSourceUri | WebViewSourceHtml;
  154. export interface ViewManager {
  155. startLoadWithResult: Function;
  156. }
  157. export interface WebViewNativeConfig {
  158. /**
  159. * The native component used to render the WebView.
  160. */
  161. component?: typeof NativeWebViewIOS | typeof NativeWebViewAndroid;
  162. /**
  163. * Set props directly on the native component WebView. Enables custom props which the
  164. * original WebView doesn't pass through.
  165. */
  166. props?: Object;
  167. /**
  168. * Set the ViewManager to use for communication with the native side.
  169. * @platform ios
  170. */
  171. viewManager?: ViewManager;
  172. }
  173. export type OnShouldStartLoadWithRequest = (
  174. event: WebViewNavigation,
  175. ) => boolean;
  176. export interface CommonNativeWebViewProps extends ViewProps {
  177. cacheEnabled?: boolean;
  178. injectedJavaScript?: string;
  179. mediaPlaybackRequiresUserAction?: boolean;
  180. messagingEnabled: boolean;
  181. onLoadingError: (event: WebViewErrorEvent) => void;
  182. onLoadingFinish: (event: WebViewNavigationEvent) => void;
  183. onLoadingProgress: (event: WebViewProgressEvent) => void;
  184. onLoadingStart: (event: WebViewNavigationEvent) => void;
  185. onMessage: (event: WebViewMessageEvent) => void;
  186. onShouldStartLoadWithRequest: (event: WebViewNavigationEvent) => void;
  187. scalesPageToFit?: boolean;
  188. showsHorizontalScrollIndicator?: boolean;
  189. showsVerticalScrollIndicator?: boolean;
  190. // TODO: find a better way to type this.
  191. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  192. source: any;
  193. userAgent?: string;
  194. }
  195. export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
  196. allowFileAccess?: boolean;
  197. allowUniversalAccessFromFileURLs?: boolean;
  198. androidHardwareAccelerationDisabled?: boolean;
  199. domStorageEnabled?: boolean;
  200. geolocationEnabled?: boolean;
  201. javaScriptEnabled?: boolean;
  202. mixedContentMode?: 'never' | 'always' | 'compatibility';
  203. onContentSizeChange?: (event: WebViewEvent) => void;
  204. overScrollMode?: OverScrollModeType;
  205. saveFormDataDisabled?: boolean;
  206. textZoom?: number;
  207. thirdPartyCookiesEnabled?: boolean;
  208. urlPrefixesForDefaultIntent?: ReadonlyArray<string>;
  209. }
  210. export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
  211. allowsBackForwardNavigationGestures?: boolean;
  212. allowsInlineMediaPlayback?: boolean;
  213. allowsLinkPreview?: boolean;
  214. automaticallyAdjustContentInsets?: boolean;
  215. bounces?: boolean;
  216. contentInset?: ContentInsetProp;
  217. dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
  218. decelerationRate?: number;
  219. directionalLockEnabled?: boolean;
  220. hideKeyboardAccessoryView?: boolean;
  221. incognito?: boolean;
  222. pagingEnabled?: boolean;
  223. scrollEnabled?: boolean;
  224. useSharedProcessPool?: boolean;
  225. }
  226. export interface IOSWebViewProps extends WebViewSharedProps {
  227. /**
  228. * If true, use WKWebView instead of UIWebView.
  229. * @platform ios
  230. */
  231. useWebKit?: boolean;
  232. /**
  233. * Does not store any data within the lifetime of the WebView.
  234. */
  235. incognito?: boolean;
  236. /**
  237. * Boolean value that determines whether the web view bounces
  238. * when it reaches the edge of the content. The default value is `true`.
  239. * @platform ios
  240. */
  241. bounces?: boolean;
  242. /**
  243. * A floating-point number that determines how quickly the scroll view
  244. * decelerates after the user lifts their finger. You may also use the
  245. * string shortcuts `"normal"` and `"fast"` which match the underlying iOS
  246. * settings for `UIScrollViewDecelerationRateNormal` and
  247. * `UIScrollViewDecelerationRateFast` respectively:
  248. *
  249. * - normal: 0.998
  250. * - fast: 0.99 (the default for iOS web view)
  251. * @platform ios
  252. */
  253. decelerationRate?: DecelerationRateConstant | number;
  254. /**
  255. * Boolean value that determines whether scrolling is enabled in the
  256. * `WebView`. The default value is `true`.
  257. * @platform ios
  258. */
  259. scrollEnabled?: boolean;
  260. /**
  261. * If the value of this property is true, the scroll view stops on multiples
  262. * of the scroll view’s bounds when the user scrolls.
  263. * The default value is false.
  264. * @platform ios
  265. */
  266. pagingEnabled?: boolean;
  267. /**
  268. * Controls whether to adjust the content inset for web views that are
  269. * placed behind a navigation bar, tab bar, or toolbar. The default value
  270. * is `true`.
  271. * @platform ios
  272. */
  273. automaticallyAdjustContentInsets?: boolean;
  274. /**
  275. * The amount by which the web view content is inset from the edges of
  276. * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
  277. * @platform ios
  278. */
  279. contentInset?: ContentInsetProp;
  280. /**
  281. * Determines the types of data converted to clickable URLs in the web view's content.
  282. * By default only phone numbers are detected.
  283. *
  284. * You can provide one type or an array of many types.
  285. *
  286. * Possible values for `dataDetectorTypes` are:
  287. *
  288. * - `'phoneNumber'`
  289. * - `'link'`
  290. * - `'address'`
  291. * - `'calendarEvent'`
  292. * - `'none'`
  293. * - `'all'`
  294. *
  295. * With the new WebKit implementation, we have three new values:
  296. * - `'trackingNumber'`,
  297. * - `'flightNumber'`,
  298. * - `'lookupSuggestion'`,
  299. *
  300. * @platform ios
  301. */
  302. dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
  303. /**
  304. * Boolean that determines whether HTML5 videos play inline or use the
  305. * native full-screen controller. The default value is `false`.
  306. *
  307. * **NOTE** : In order for video to play inline, not only does this
  308. * property need to be set to `true`, but the video element in the HTML
  309. * document must also include the `webkit-playsinline` attribute.
  310. * @platform ios
  311. */
  312. allowsInlineMediaPlayback?: boolean;
  313. /**
  314. * Hide the accessory view when the keyboard is open. Default is false to be
  315. * backward compatible.
  316. */
  317. hideKeyboardAccessoryView?: boolean;
  318. /**
  319. * A Boolean value indicating whether horizontal swipe gestures will trigger
  320. * back-forward list navigations.
  321. */
  322. allowsBackForwardNavigationGestures?: boolean;
  323. /**
  324. * A Boolean value indicating whether WebKit WebView should be created using a shared
  325. * process pool, enabling WebViews to share cookies and localStorage between each other.
  326. * Default is true but can be set to false for backwards compatibility.
  327. * @platform ios
  328. */
  329. useSharedProcessPool?: boolean;
  330. /**
  331. * The custom user agent string.
  332. */
  333. userAgent?: string;
  334. /**
  335. * A Boolean value that determines whether pressing on a link
  336. * displays a preview of the destination for the link.
  337. *
  338. * This property is available on devices that support 3D Touch.
  339. * In iOS 10 and later, the default value is `true`; before that, the default value is `false`.
  340. * @platform ios
  341. */
  342. allowsLinkPreview?: boolean;
  343. /**
  344. * Set true if shared cookies from HTTPCookieStorage should used for every load request in the
  345. * `RNCWKWebView`. The default value is `false`.
  346. * @platform ios
  347. */
  348. sharedCookiesEnabled?: boolean;
  349. /**
  350. * A Boolean value that determines whether scrolling is disabled in a particular direction.
  351. * The default value is `true`.
  352. * @platform ios
  353. */
  354. directionalLockEnabled?: boolean;
  355. }
  356. export interface AndroidWebViewProps extends WebViewSharedProps {
  357. onNavigationStateChange?: (event: WebViewNavigation) => void;
  358. onContentSizeChange?: (event: WebViewEvent) => void;
  359. /**
  360. * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
  361. * Sets the overScrollMode. Possible values are:
  362. *
  363. * - `'always'` (default)
  364. * - `'content'`
  365. * - `'never'`
  366. *
  367. * @platform android
  368. */
  369. overScrollMode?: OverScrollModeType;
  370. /**
  371. * Sets whether Geolocation is enabled. The default is false.
  372. * @platform android
  373. */
  374. geolocationEnabled?: boolean;
  375. /**
  376. * Boolean that sets whether JavaScript running in the context of a file
  377. * scheme URL should be allowed to access content from any origin.
  378. * Including accessing content from other file scheme URLs
  379. * @platform android
  380. */
  381. allowUniversalAccessFromFileURLs?: boolean;
  382. /**
  383. * Sets whether the webview allow access to file system.
  384. * @platform android
  385. */
  386. allowFileAccess?: boolean;
  387. /**
  388. * Used on Android only, controls whether form autocomplete data should be saved
  389. * @platform android
  390. */
  391. saveFormDataDisabled?: boolean;
  392. /**
  393. * Used on Android only, controls whether the given list of URL prefixes should
  394. * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
  395. * default activity intent for those URL instead of loading it within the webview.
  396. * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
  397. * @platform android
  398. */
  399. urlPrefixesForDefaultIntent?: ReadonlyArray<string>;
  400. /**
  401. * Boolean value to enable JavaScript in the `WebView`. Used on Android only
  402. * as JavaScript is enabled by default on iOS. The default value is `true`.
  403. * @platform android
  404. */
  405. javaScriptEnabled?: boolean;
  406. /**
  407. * Boolean value to disable Hardware Acceleration in the `WebView`. Used on Android only
  408. * as Hardware Acceleration is a feature only for Android. The default value is `false`.
  409. * @platform android
  410. */
  411. androidHardwareAccelerationDisabled?: boolean;
  412. /**
  413. * Boolean value to enable third party cookies in the `WebView`. Used on
  414. * Android Lollipop and above only as third party cookies are enabled by
  415. * default on Android Kitkat and below and on iOS. The default value is `true`.
  416. * @platform android
  417. */
  418. thirdPartyCookiesEnabled?: boolean;
  419. /**
  420. * Boolean value to control whether DOM Storage is enabled. Used only in
  421. * Android.
  422. * @platform android
  423. */
  424. domStorageEnabled?: boolean;
  425. /**
  426. * Sets the user-agent for the `WebView`.
  427. * @platform android
  428. */
  429. userAgent?: string;
  430. /**
  431. * Sets number that controls text zoom of the page in percent.
  432. * @platform android
  433. */
  434. textZoom?: number;
  435. /**
  436. * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.
  437. *
  438. * Possible values for `mixedContentMode` are:
  439. *
  440. * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin.
  441. * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure.
  442. * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content.
  443. * @platform android
  444. */
  445. mixedContentMode?: 'never' | 'always' | 'compatibility';
  446. }
  447. export interface WebViewSharedProps extends ViewProps {
  448. /**
  449. * Loads static html or a uri (with optional headers) in the WebView.
  450. */
  451. source?: WebViewSource;
  452. /**
  453. * Function that returns a view to show if there's an error.
  454. */
  455. renderError?: (
  456. errorDomain: string | undefined,
  457. errorCode: number,
  458. errorDesc: string,
  459. ) => ReactElement; // view to show if there's an error
  460. /**
  461. * Function that returns a loading indicator.
  462. */
  463. renderLoading?: () => ReactElement;
  464. /**
  465. * Function that is invoked when the `WebView` has finished loading.
  466. */
  467. onLoad?: (event: WebViewNavigationEvent) => void;
  468. /**
  469. * Function that is invoked when the `WebView` load succeeds or fails.
  470. */
  471. onLoadEnd?: (event: WebViewNavigationEvent | WebViewErrorEvent) => void;
  472. /**
  473. * Function that is invoked when the `WebView` starts loading.
  474. */
  475. onLoadStart?: (event: WebViewNavigationEvent) => void;
  476. /**
  477. * Function that is invoked when the `WebView` load fails.
  478. */
  479. onError?: (event: WebViewErrorEvent) => void;
  480. /**
  481. * Function that is invoked when the `WebView` loading starts or ends.
  482. */
  483. onNavigationStateChange?: (event: WebViewNavigation) => void;
  484. /**
  485. * Function that is invoked when the webview calls `window.ReactNativeWebView.postMessage`.
  486. * Setting this property will inject this global into your webview.
  487. *
  488. * `window.ReactNativeWebView.postMessage` accepts one argument, `data`, which will be
  489. * available on the event object, `event.nativeEvent.data`. `data` must be a string.
  490. */
  491. onMessage?: (event: WebViewMessageEvent) => void;
  492. /**
  493. * Function that is invoked when the `WebView` is loading.
  494. */
  495. onLoadProgress?: (event: WebViewProgressEvent) => void;
  496. /**
  497. * Boolean value that forces the `WebView` to show the loading view
  498. * on the first load.
  499. */
  500. startInLoadingState?: boolean;
  501. /**
  502. * Set this to provide JavaScript that will be injected into the web page
  503. * when the view loads.
  504. */
  505. injectedJavaScript?: string;
  506. /**
  507. * Boolean value that determines whether a horizontal scroll indicator is
  508. * shown in the `WebView`. The default value is `true`.
  509. */
  510. showsHorizontalScrollIndicator?: boolean;
  511. /**
  512. * Boolean value that determines whether a vertical scroll indicator is
  513. * shown in the `WebView`. The default value is `true`.
  514. */
  515. showsVerticalScrollIndicator?: boolean;
  516. /**
  517. * Boolean that controls whether the web content is scaled to fit
  518. * the view and enables the user to change the scale. The default value
  519. * is `true`.
  520. *
  521. * On iOS, when `useWebKit=true`, this prop will not work.
  522. */
  523. scalesPageToFit?: boolean;
  524. /**
  525. * Boolean that determines whether HTML5 audio and video requires the user
  526. * to tap them before they start playing. The default value is `true`.
  527. */
  528. mediaPlaybackRequiresUserAction?: boolean;
  529. /**
  530. * List of origin strings to allow being navigated to. The strings allow
  531. * wildcards and get matched against *just* the origin (not the full URL).
  532. * If the user taps to navigate to a new page but the new page is not in
  533. * this whitelist, we will open the URL in Safari.
  534. * The default whitelisted origins are "http://*" and "https://*".
  535. */
  536. originWhitelist?: ReadonlyArray<string>;
  537. /**
  538. * Function that allows custom handling of any web view requests. Return
  539. * `true` from the function to continue loading the request and `false`
  540. * to stop loading. The `navigationType` is always `other` on android.
  541. */
  542. onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest;
  543. /**
  544. * Override the native component used to render the WebView. Enables a custom native
  545. * WebView which uses the same JavaScript as the original WebView.
  546. */
  547. nativeConfig?: WebViewNativeConfig;
  548. /**
  549. * Should caching be enabled. Default is true.
  550. */
  551. cacheEnabled?: boolean;
  552. }