No Description

WebViewTypes.ts 19KB

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