UNPKG

jss-react-material

Version:

Google Material Components for React

126 lines (96 loc) 5.83 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || 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; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _class2, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _addons = require('../addons'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var findMediaIndex = function findMediaIndex(sources, width) { var index = sources.reverse().findIndex(function (source) { var matches = source.media.match(/^\(([a-z-]+): ([0-9]+)([a-z]+)\)$/); if (!matches) { return false; } var media = matches[1]; var unit = matches[3]; var sourceWidth = unit === 'rem' ? matches[2] * 16 : matches[2]; if (media === 'min-width' && sourceWidth > width) { return true; } else if (media === 'max-width' && sourceWidth < width) { return true; } return false; }); return index !== -1 ? sources.length - 1 - index : -1; }; var PictureElement = (0, _addons.getClasses)(_class = (_temp = _class2 = function (_React$Component) { _inherits(PictureElement, _React$Component); function PictureElement() { var _ref; _classCallCheck(this, PictureElement); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var _this = _possibleConstructorReturn(this, (_ref = PictureElement.__proto__ || Object.getPrototypeOf(PictureElement)).call.apply(_ref, [this].concat(args))); _this.state = { index: 0 }; return _this; } _createClass(PictureElement, [{ key: 'componentDidMount', value: function componentDidMount() { if (!window.HTMLPictureElement) { var picture = _reactDom2.default.findDOMNode(this).getElementsByTagName('img')[0]; var sources = this.props.sources; var width = picture.width; var index = findMediaIndex(sources, width); if (index !== -1) { this.setState({ index: index }); } } } }, { key: 'render', value: function render() { var _props = this.props, sources = _props.sources, others = _objectWithoutProperties(_props, ['sources']); var index = this.state.index; var fallback = { src: sources[index].src, alt: sources[index].alt }; return _react2.default.createElement( 'picture', { className: this.getClasses('picture', others) }, sources.map(function (props, key) { var src = props.src, type = props.type, media = props.media, sizes = props.sizes; return _react2.default.createElement('source', _extends({ key: key }, { media: media, sizes: sizes, type: type }, { srcSet: src })); }), fallback && _react2.default.createElement('img', _extends({}, fallback, others)) ); } }]); return PictureElement; }(_react2.default.Component), _class2.propTypes = { sources: _propTypes2.default.arrayOf(_propTypes2.default.shape({ src: _addons.MaterialPropTypes.url.isRequired, type: _propTypes2.default.string.isRequired, alt: _propTypes2.default.string, media: _propTypes2.default.string, sizes: _propTypes2.default.string, title: _propTypes2.default.string })).isRequired }, _temp)) || _class; exports.default = PictureElement;