Bez popisu

WebViewTypes.ts 19KB

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