UNPKG

react-native-fast-image

Version:

🚩 FastImage, performant React Native image component.

113 lines (102 loc) • 3.29 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var _extends = _interopDefault(require('@babel/runtime/helpers/extends')); var React = require('react'); var React__default = _interopDefault(React); var reactNative = require('react-native'); const FastImageViewNativeModule = reactNative.NativeModules.FastImageView; const resizeMode = { contain: 'contain', cover: 'cover', stretch: 'stretch', center: 'center' }; const priority = { low: 'low', normal: 'normal', high: 'high' }; const cacheControl = { // Ignore headers, use uri as cache key, fetch only if not in cache. immutable: 'immutable', // Respect http headers, no aggressive caching. web: 'web', // Only load from cache. cacheOnly: 'cacheOnly' }; function FastImageBase({ source, tintColor, onLoadStart, onProgress, onLoad, onError, onLoadEnd, style, fallback, children, // eslint-disable-next-line no-shadow resizeMode = 'cover', forwardedRef, ...props }) { if (fallback) { const cleanedSource = { ...source }; delete cleanedSource.cache; const resolvedSource = reactNative.Image.resolveAssetSource(cleanedSource); return /*#__PURE__*/React__default.createElement(reactNative.View, { style: [styles.imageContainer, style], ref: forwardedRef }, /*#__PURE__*/React__default.createElement(reactNative.Image, _extends({}, props, { style: reactNative.StyleSheet.absoluteFill, source: resolvedSource, onLoadStart: onLoadStart, onProgress: onProgress, onLoad: onLoad, onError: onError, onLoadEnd: onLoadEnd, resizeMode: resizeMode })), children); } const resolvedSource = reactNative.Image.resolveAssetSource(source); return /*#__PURE__*/React__default.createElement(reactNative.View, { style: [styles.imageContainer, style], ref: forwardedRef }, /*#__PURE__*/React__default.createElement(FastImageView, _extends({}, props, { tintColor: tintColor, style: reactNative.StyleSheet.absoluteFill, source: resolvedSource, onFastImageLoadStart: onLoadStart, onFastImageProgress: onProgress, onFastImageLoad: onLoad, onFastImageError: onError, onFastImageLoadEnd: onLoadEnd, resizeMode: resizeMode })), children); } const FastImageMemo = /*#__PURE__*/React.memo(FastImageBase); const FastImageComponent = /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React__default.createElement(FastImageMemo, _extends({ forwardedRef: ref }, props))); FastImageComponent.displayName = 'FastImage'; const FastImage = FastImageComponent; FastImage.resizeMode = resizeMode; FastImage.cacheControl = cacheControl; FastImage.priority = priority; FastImage.preload = sources => FastImageViewNativeModule.preload(sources); const styles = reactNative.StyleSheet.create({ imageContainer: { overflow: 'hidden' } }); // Types of requireNativeComponent are not correct. const FastImageView = reactNative.requireNativeComponent('FastImageView', FastImage, { nativeOnly: { onFastImageLoadStart: true, onFastImageProgress: true, onFastImageLoad: true, onFastImageError: true, onFastImageLoadEnd: true } }); module.exports = FastImage;