暂无描述

WebViewTypes.ts 20KB

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