No Description

WebViewTypes.ts 19KB

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