Geen omschrijving

WebViewTypes.ts 20KB

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