terra-image
Version:
The terra-image component provides styling for visual imagery.
203 lines (202 loc) • 10.9 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "FitTypes", {
enumerable: true,
get: function get() {
return _imageHelper.FitTypes;
}
});
Object.defineProperty(exports, "ImageVariant", {
enumerable: true,
get: function get() {
return _imageHelper.ImageVariant;
}
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _objectFitImages = _interopRequireDefault(require("object-fit-images"));
var _classnames = _interopRequireDefault(require("classnames"));
var _bind = _interopRequireDefault(require("classnames/bind"));
var _terraThemeContext = _interopRequireDefault(require("terra-theme-context"));
var _ImageModule = _interopRequireDefault(require("./Image.module.scss"));
var _imageHelper = require("./_imageHelper");
var _excluded = ["variant", "isFluid", "placeholder", "fit"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } } return target; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } // Added polyfill for IE.
var cx = _bind.default.bind(_ImageModule.default);
var propTypes = {
/**
* The source string for the image which will be loaded and displayed.
*/
src: _propTypes.default.string.isRequired,
/**
* Sets the style of the image from the following values; `default`, `rounded`, `circle`, `thumbnail`.
*/
variant: _propTypes.default.oneOf(['default', 'rounded', 'circle', 'thumbnail']),
/**
* Sets the fluid behavior of the image, which is `nonfluid` by default.
*/
isFluid: _propTypes.default.bool,
/**
* 
* The text content that specifies an alternative text for an image.
* `alt` prop helps to provide meaningfull context for images and should be used for creating informative images. For decorative images `alt` prop can be ignored.
* whenever `alt` prop is empty OR not defined image will be marked as decorative by default and ignored by Assistive Tools.
*/
alt: _propTypes.default.string,
/**
* A React element which will be displayed during loading and in case of src load failure.
*/
placeholder: _propTypes.default.element,
/**
* Sets the height of the image.
*/
height: _propTypes.default.string,
/**
* Sets the width of the image.
*/
width: _propTypes.default.string,
/**
* Function to be executed when the image load is successful.
*/
onLoad: _propTypes.default.func,
/**
* Function to be executed when the image load errors.
*/
onError: _propTypes.default.func,
/**
* Sets the `object-fit` style of the image from the following values; `cover`, `contain`, `fill`, `scale-down`, `none`.
*/
fit: _propTypes.default.oneOf(['cover', 'scale-down', 'fill', 'contain', 'none'])
};
var defaultProps = {
variant: 'default',
isFluid: false,
fit: 'fill'
};
var Image = /*#__PURE__*/function (_React$Component) {
function Image(props) {
var _this;
_classCallCheck(this, Image);
_this = _callSuper(this, Image, [props]);
_this.ImageRef = /*#__PURE__*/_react.default.createRef();
_this.state = {
isLoading: true,
isError: false,
prevPropsSrc: props.src
};
_this.handleOnLoad = _this.handleOnLoad.bind(_this);
_this.handleOnError = _this.handleOnError.bind(_this);
return _this;
}
_inherits(Image, _React$Component);
return _createClass(Image, [{
key: "componentDidUpdate",
value: function componentDidUpdate() {
(0, _objectFitImages.default)(this.ImageRef.current);
}
}, {
key: "handleOnLoad",
value: function handleOnLoad() {
this.setState({
isLoading: false
});
var onLoad = this.props.onLoad;
if (onLoad !== undefined) {
onLoad();
}
}
}, {
key: "handleOnError",
value: function handleOnError() {
this.setState({
isLoading: false,
isError: true
});
var onError = this.props.onError;
if (onError !== undefined) {
onError();
}
}
}, {
key: "createImage",
value: function createImage(customProps, imageClasses) {
var src = customProps.src,
alt = customProps.alt,
height = customProps.height,
width = customProps.width;
var imageProps = alt !== undefined && (alt === null || alt === void 0 ? void 0 : alt.trim()) !== '' ? (0, _imageHelper.getInformativeImage)(customProps) : (0, _imageHelper.getDecorativeImage)(customProps);
return /*#__PURE__*/_react.default.createElement("img", _extends({}, imageProps, {
src: src,
alt: alt,
height: height,
width: width,
onLoad: this.handleOnLoad,
onError: this.handleOnError,
className: imageClasses,
ref: this.ImageRef
}));
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
variant = _this$props.variant,
isFluid = _this$props.isFluid,
placeholder = _this$props.placeholder,
fit = _this$props.fit,
customProps = _objectWithoutProperties(_this$props, _excluded);
var theme = this.context;
var imageClasses = (0, _classnames.default)(cx('image', fit, variant, theme.className, {
fluid: isFluid
}, {
hidden: placeholder && this.state.isLoading
}), customProps.className);
delete customProps.className;
if (!this.state.isLoading) {
(0, _objectFitImages.default)(this.ImageRef.current);
}
if (placeholder) {
if (this.state.isLoading) {
return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, this.createImage(customProps, imageClasses), placeholder);
}
return this.state.isError ? placeholder : this.createImage(customProps, imageClasses);
}
return this.createImage(customProps, imageClasses);
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.src !== prevState.prevPropsSrc) {
return {
isLoading: true,
isError: false,
prevPropsSrc: nextProps.src
};
}
return null;
}
}]);
}(_react.default.Component);
Image.propTypes = propTypes;
Image.defaultProps = defaultProps;
Image.contextType = _terraThemeContext.default;
var _default = exports.default = Image;