UNPKG

@times-components/card

Version:

The card component fades in and lays out content that typically consists of an image and a collection of textual elements (although this content could be anything). Card manages the layout of these elements, whilst providing a consistent loading state for

142 lines (108 loc) • 10.1 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _utils = require("@times-components/utils"); var _styledComponents = _interopRequireDefault(require("styled-components")); var _image = _interopRequireDefault(require("@times-components/image")); var _cardPropTypes = require("./card-prop-types"); var _cardLoading = _interopRequireDefault(require("./card-loading")); var _styles = _interopRequireDefault(require("./styles")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 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, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } 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 } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } 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() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var CardContent = /*#__PURE__*/function (_Component) { _inherits(CardContent, _Component); var _super = _createSuper(CardContent); function CardContent() { _classCallCheck(this, CardContent); return _super.apply(this, arguments); } _createClass(CardContent, [{ key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { var _this$props = this.props, imageUri = _this$props.imageUri, lowResQuality = _this$props.lowResQuality, lowResSize = _this$props.lowResSize, highResSize = _this$props.highResSize, isLoading = _this$props.isLoading, index = _this$props.index; return imageUri !== nextProps.imageUri || lowResQuality !== nextProps.lowResQuality || lowResSize !== nextProps.lowResSize || highResSize !== nextProps.highResSize || isLoading !== nextProps.isLoading || index !== nextProps.index; } }, { key: "render", value: function render() { var _this$props2 = this.props, children = _this$props2.children, contentContainerClass = _this$props2.contentContainerClass, fadeImageIn = _this$props2.fadeImageIn, highResSize = _this$props2.highResSize, imageAccessibilityLabel = _this$props2.imageAccessibilityLabel, imageContainerClass = _this$props2.imageContainerClass, imageRatio = _this$props2.imageRatio, imageStyle = _this$props2.imageStyle, imageUri = _this$props2.imageUri, isLoading = _this$props2.isLoading, isReversed = _this$props2.isReversed, lowResQuality = _this$props2.lowResQuality, lowResSize = _this$props2.lowResSize, relatedArticle = _this$props2.relatedArticle, showImage = _this$props2.showImage, isLcpItem = _this$props2.isLcpItem; var TcCardContainer = (0, _styledComponents["default"])(_utils.TcView).withConfig({ displayName: "card-content__TcCardContainer", componentId: "sc-1w0wvq8-0" })(["", ";"], function (props) { return props.styles && props.styles; }); var renderImage = function renderImage() { if (!showImage) return null; var imageContainerStyle = relatedArticle ? _styles["default"].imageContainer : _styles["default"].imageContainerTablet; return /*#__PURE__*/_react["default"].createElement(_utils.TcView, { className: imageContainerClass, style: (0, _utils.checkStylesForUnits)(_objectSpread(_objectSpread(_objectSpread({}, imageContainerStyle), imageStyle), isReversed ? _styles["default"].reversedImageContainer : "")) }, /*#__PURE__*/_react["default"].createElement(_image["default"], { accessibilityLabel: imageAccessibilityLabel, aspectRatio: imageRatio, fadeImageIn: fadeImageIn, highResSize: highResSize, lowResQuality: lowResQuality, lowResSize: lowResSize, uri: imageUri, isLcpItem: isLcpItem })); }; var cardContainerStyle = relatedArticle ? _objectSpread(_objectSpread({}, _styles["default"].cardContainer), {}, { display: "block" }) : _styles["default"].cardContainer; return /*#__PURE__*/_react["default"].createElement(_utils.TcView, { style: (0, _utils.checkStylesForUnits)(_objectSpread(_objectSpread({}, cardContainerStyle), isReversed ? _styles["default"].reversedCardContainer : "")) }, !isReversed ? renderImage() : null, /*#__PURE__*/_react["default"].createElement(TcCardContainer, { className: contentContainerClass, styles: (0, _utils.checkStylesForUnits)(_objectSpread(_objectSpread(_objectSpread({}, _styles["default"].contentContainer), isReversed ? _styles["default"].reversedContentContainer : ""), isLoading ? _styles["default"].loadingContentContainer : "")) }, isLoading ? /*#__PURE__*/_react["default"].createElement(_cardLoading["default"], null) : children), isReversed ? renderImage() : null); } }]); return CardContent; }(_react.Component); CardContent.propTypes = _cardPropTypes.cardPropTypes; CardContent.defaultProps = _cardPropTypes.cardDefaultProps; var _default = CardContent; exports["default"] = _default;