Brak opisu

WebViewTypes.ts 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. thirdPartyCookiesEnabled?: boolean;
  207. urlPrefixesForDefaultIntent?: ReadonlyArray<string>;
  208. }
  209. export interface IOSNativeWebViewProps extends CommonNativeWebViewProps {
  210. allowsBackForwardNavigationGestures?: boolean;
  211. allowsInlineMediaPlayback?: boolean;
  212. allowsLinkPreview?: boolean;
  213. automaticallyAdjustContentInsets?: boolean;
  214. bounces?: boolean;
  215. contentInset?: ContentInsetProp;
  216. dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
  217. decelerationRate?: number;
  218. directionalLockEnabled?: boolean;
  219. hideKeyboardAccessoryView?: boolean;
  220. incognito?: boolean;
  221. pagingEnabled?: boolean;
  222. scrollEnabled?: boolean;
  223. useSharedProcessPool?: boolean;
  224. }
  225. export interface IOSWebViewProps extends WebViewSharedProps {
  226. /**
  227. * If true, use WKWebView instead of UIWebView.
  228. * @platform ios
  229. */
  230. useWebKit?: boolean;
  231. /**
  232. * Does not store any data within the lifetime of the WebView.
  233. */
  234. incognito?: boolean;
  235. /**
  236. * Boolean value that determines whether the web view bounces
  237. * when it reaches the edge of the content. The default value is `true`.
  238. * @platform ios
  239. */
  240. bounces?: boolean;
  241. /**
  242. * A floating-point number that determines how quickly the scroll view
  243. * decelerates after the user lifts their finger. You may also use the
  244. * string shortcuts `"normal"` and `"fast"` which match the underlying iOS
  245. * settings for `UIScrollViewDecelerationRateNormal` and
  246. * `UIScrollViewDecelerationRateFast` respectively:
  247. *
  248. * - normal: 0.998
  249. * - fast: 0.99 (the default for iOS web view)
  250. * @platform ios
  251. */
  252. decelerationRate?: DecelerationRateConstant | number;
  253. /**
  254. * Boolean value that determines whether scrolling is enabled in the
  255. * `WebView`. The default value is `true`.
  256. * @platform ios
  257. */
  258. scrollEnabled?: boolean;
  259. /**
  260. * If the value of this property is true, the scroll view stops on multiples
  261. * of the scroll view’s bounds when the user scrolls.
  262. * The default value is false.
  263. * @platform ios
  264. */
  265. pagingEnabled?: boolean;
  266. /**
  267. * Controls whether to adjust the content inset for web views that are
  268. * placed behind a navigation bar, tab bar, or toolbar. The default value
  269. * is `true`.
  270. * @platform ios
  271. */
  272. automaticallyAdjustContentInsets?: boolean;
  273. /**
  274. * The amount by which the web view content is inset from the edges of
  275. * the scroll view. Defaults to {top: 0, left: 0, bottom: 0, right: 0}.
  276. * @platform ios
  277. */
  278. contentInset?: ContentInsetProp;
  279. /**
  280. * Determines the types of data converted to clickable URLs in the web view's content.
  281. * By default only phone numbers are detected.
  282. *
  283. * You can provide one type or an array of many types.
  284. *
  285. * Possible values for `dataDetectorTypes` are:
  286. *
  287. * - `'phoneNumber'`
  288. * - `'link'`
  289. * - `'address'`
  290. * - `'calendarEvent'`
  291. * - `'none'`
  292. * - `'all'`
  293. *
  294. * With the new WebKit implementation, we have three new values:
  295. * - `'trackingNumber'`,
  296. * - `'flightNumber'`,
  297. * - `'lookupSuggestion'`,
  298. *
  299. * @platform ios
  300. */
  301. dataDetectorTypes?: DataDetectorTypes | ReadonlyArray<DataDetectorTypes>;
  302. /**
  303. * Boolean that determines whether HTML5 videos play inline or use the
  304. * native full-screen controller. The default value is `false`.
  305. *
  306. * **NOTE** : In order for video to play inline, not only does this
  307. * property need to be set to `true`, but the video element in the HTML
  308. * document must also include the `webkit-playsinline` attribute.
  309. * @platform ios
  310. */
  311. allowsInlineMediaPlayback?: boolean;
  312. /**
  313. * Hide the accessory view when the keyboard is open. Default is false to be
  314. * backward compatible.
  315. */
  316. hideKeyboardAccessoryView?: boolean;
  317. /**
  318. * A Boolean value indicating whether horizontal swipe gestures will trigger
  319. * back-forward list navigations.
  320. */
  321. allowsBackForwardNavigationGestures?: boolean;
  322. /**
  323. * A Boolean value indicating whether WebKit WebView should be created using a shared
  324. * process pool, enabling WebViews to share cookies and localStorage between each other.
  325. * Default is true but can be set to false for backwards compatibility.
  326. * @platform ios
  327. */
  328. useSharedProcessPool?: boolean;
  329. /**
  330. * The custom user agent string.
  331. */
  332. userAgent?: string;
  333. /**
  334. * A Boolean value that determines whether pressing on a link
  335. * displays a preview of the destination for the link.
  336. *
  337. * This property is available on devices that support 3D Touch.
  338. * In iOS 10 and later, the default value is `true`; before that, the default value is `false`.
  339. * @platform ios
  340. */
  341. allowsLinkPreview?: boolean;
  342. /**
  343. * A Boolean value that determines whether scrolling is disabled in a particular direction.
  344. * The default value is `true`.
  345. * @platform ios
  346. */
  347. directionalLockEnabled?: boolean;
  348. }
  349. export interface AndroidWebViewProps extends WebViewSharedProps {
  350. onNavigationStateChange?: (event: WebViewNavigation) => void;
  351. onContentSizeChange?: (event: WebViewEvent) => void;
  352. /**
  353. * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
  354. * Sets the overScrollMode. Possible values are:
  355. *
  356. * - `'always'` (default)
  357. * - `'content'`
  358. * - `'never'`
  359. *
  360. * @platform android
  361. */
  362. overScrollMode?: OverScrollModeType;
  363. /**
  364. * Sets whether Geolocation is enabled. The default is false.
  365. * @platform android
  366. */
  367. geolocationEnabled?: boolean;
  368. /**
  369. * Boolean that sets whether JavaScript running in the context of a file
  370. * scheme URL should be allowed to access content from any origin.
  371. * Including accessing content from other file scheme URLs
  372. * @platform android
  373. */
  374. allowUniversalAccessFromFileURLs?: boolean;
  375. /**
  376. * Sets whether the webview allow access to file system.
  377. * @platform android
  378. */
  379. allowFileAccess?: boolean;
  380. /**
  381. * Used on Android only, controls whether form autocomplete data should be saved
  382. * @platform android
  383. */
  384. saveFormDataDisabled?: boolean;
  385. /**
  386. * Used on Android only, controls whether the given list of URL prefixes should
  387. * make {@link com.facebook.react.views.webview.ReactWebViewClient} to launch a
  388. * default activity intent for those URL instead of loading it within the webview.
  389. * Use this to list URLs that WebView cannot handle, e.g. a PDF url.
  390. * @platform android
  391. */
  392. urlPrefixesForDefaultIntent?: ReadonlyArray<string>;
  393. /**
  394. * Boolean value to enable JavaScript in the `WebView`. Used on Android only
  395. * as JavaScript is enabled by default on iOS. The default value is `true`.
  396. * @platform android
  397. */
  398. javaScriptEnabled?: boolean;
  399. /**
  400. * Boolean value to disable Hardware Acceleration in the `WebView`. Used on Android only
  401. * as Hardware Acceleration is a feature only for Android. The default value is `false`.
  402. * @platform android
  403. */
  404. androidHardwareAccelerationDisabled?: boolean;
  405. /**
  406. * Boolean value to enable third party cookies in the `WebView`. Used on
  407. * Android Lollipop and above only as third party cookies are enabled by
  408. * default on Android Kitkat and below and on iOS. The default value is `true`.
  409. * @platform android
  410. */
  411. thirdPartyCookiesEnabled?: boolean;
  412. /**
  413. * Boolean value to control whether DOM Storage is enabled. Used only in
  414. * Android.
  415. * @platform android
  416. */
  417. domStorageEnabled?: boolean;
  418. /**
  419. * Sets the user-agent for the `WebView`.
  420. * @platform android
  421. */
  422. userAgent?: string;
  423. /**
  424. * Specifies the mixed content mode. i.e WebView will allow a secure origin to load content from any other origin.
  425. *
  426. * Possible values for `mixedContentMode` are:
  427. *
  428. * - `'never'` (default) - WebView will not allow a secure origin to load content from an insecure origin.
  429. * - `'always'` - WebView will allow a secure origin to load content from any other origin, even if that origin is insecure.
  430. * - `'compatibility'` - WebView will attempt to be compatible with the approach of a modern web browser with regard to mixed content.
  431. * @platform android
  432. */
  433. mixedContentMode?: 'never' | 'always' | 'compatibility';
  434. }
  435. export interface WebViewSharedProps extends ViewProps {
  436. /**
  437. * Loads static html or a uri (with optional headers) in the WebView.
  438. */
  439. source?: WebViewSource;
  440. /**
  441. * Function that returns a view to show if there's an error.
  442. */
  443. renderError?: (
  444. errorDomain: string | undefined,
  445. errorCode: number,
  446. errorDesc: string,
  447. ) => ReactElement; // view to show if there's an error
  448. /**
  449. * Function that returns a loading indicator.
  450. */
  451. renderLoading?: () => ReactElement;
  452. /**
  453. * Function that is invoked when the `WebView` has finished loading.
  454. */
  455. onLoad?: (event: WebViewNavigationEvent) => void;
  456. /**
  457. * Function that is invoked when the `WebView` load succeeds or fails.
  458. */
  459. onLoadEnd?: (event: WebViewNavigationEvent | WebViewErrorEvent) => void;
  460. /**
  461. * Function that is invoked when the `WebView` starts loading.
  462. */
  463. onLoadStart?: (event: WebViewNavigationEvent) => void;
  464. /**
  465. * Function that is invoked when the `WebView` load fails.
  466. */
  467. onError?: (event: WebViewErrorEvent) => void;
  468. /**
  469. * Function that is invoked when the `WebView` loading starts or ends.
  470. */
  471. onNavigationStateChange?: (event: WebViewNavigation) => void;
  472. /**
  473. * Function that is invoked when the webview calls `window.ReactNativeWebView.postMessage`.
  474. * Setting this property will inject this global into your webview.
  475. *
  476. * `window.ReactNativeWebView.postMessage` accepts one argument, `data`, which will be
  477. * available on the event object, `event.nativeEvent.data`. `data` must be a string.
  478. */
  479. onMessage?: (event: WebViewMessageEvent) => void;
  480. /**
  481. * Function that is invoked when the `WebView` is loading.
  482. */
  483. onLoadProgress?: (event: WebViewProgressEvent) => void;
  484. /**
  485. * Boolean value that forces the `WebView` to show the loading view
  486. * on the first load.
  487. */
  488. startInLoadingState?: boolean;
  489. /**
  490. * Set this to provide JavaScript that will be injected into the web page
  491. * when the view loads.
  492. */
  493. injectedJavaScript?: string;
  494. /**
  495. * Boolean value that determines whether a horizontal scroll indicator is
  496. * shown in the `WebView`. The default value is `true`.
  497. */
  498. showsHorizontalScrollIndicator?: boolean;
  499. /**
  500. * Boolean value that determines whether a vertical scroll indicator is
  501. * shown in the `WebView`. The default value is `true`.
  502. */
  503. showsVerticalScrollIndicator?: boolean;
  504. /**
  505. * Boolean that controls whether the web content is scaled to fit
  506. * the view and enables the user to change the scale. The default value
  507. * is `true`.
  508. *
  509. * On iOS, when `useWebKit=true`, this prop will not work.
  510. */
  511. scalesPageToFit?: boolean;
  512. /**
  513. * Boolean that determines whether HTML5 audio and video requires the user
  514. * to tap them before they start playing. The default value is `true`.
  515. */
  516. mediaPlaybackRequiresUserAction?: boolean;
  517. /**
  518. * List of origin strings to allow being navigated to. The strings allow
  519. * wildcards and get matched against *just* the origin (not the full URL).
  520. * If the user taps to navigate to a new page but the new page is not in
  521. * this whitelist, we will open the URL in Safari.
  522. * The default whitelisted origins are "http://*" and "https://*".
  523. */
  524. originWhitelist?: ReadonlyArray<string>;
  525. /**
  526. * Function that allows custom handling of any web view requests. Return
  527. * `true` from the function to continue loading the request and `false`
  528. * to stop loading. The `navigationType` is always `other` on android.
  529. */
  530. onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest;
  531. /**
  532. * Override the native component used to render the WebView. Enables a custom native
  533. * WebView which uses the same JavaScript as the original WebView.
  534. */
  535. nativeConfig?: WebViewNativeConfig;
  536. /**
  537. * Should caching be enabled. Default is true.
  538. */
  539. cacheEnabled?: boolean;
  540. }