wix-style-react
Version:
115 lines (91 loc) • 4.59 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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 _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; } }
import React from 'react';
import PropTypes from 'prop-types';
import { st, classes } from './Proportion.st.css';
import { PREDEFINED_RATIOS } from './ratios';
var Proportion = /*#__PURE__*/function (_React$PureComponent) {
_inherits(Proportion, _React$PureComponent);
var _super = _createSuper(Proportion);
function Proportion() {
_classCallCheck(this, Proportion);
return _super.apply(this, arguments);
}
_createClass(Proportion, [{
key: "render",
value: function render() {
var _this$props = this.props,
dataHook = _this$props.dataHook,
className = _this$props.className,
aspectRatio = _this$props.aspectRatio;
var aspectRatioHolder = this._getAspectRatioHolder();
var disabled = aspectRatio === PREDEFINED_RATIOS.none ? true : false;
var content = this._getContent(disabled);
return /*#__PURE__*/React.createElement("div", {
className: st(classes.root, className),
"data-hook": dataHook
}, !disabled && aspectRatioHolder, content);
}
}, {
key: "_getContent",
value: function _getContent(disabled) {
var children = this.props.children;
return disabled ? children : /*#__PURE__*/React.createElement("div", {
className: classes.contentWrapper
}, children);
}
/**
* This is based on Noam Rosenthal's (noamr@wix.com) solution
* which can be found here: https://codeburst.io/keeping-aspect-ratio-with-html-and-no-padding-tricks-40705656808b
*
* The solution uses the fact that SVGs can maintain aspect ratio's natively.
* In addition we use an img element for this solution to work correctly in IE
* */
}, {
key: "_getAspectRatioHolder",
value: function _getAspectRatioHolder() {
var _this$_getRatio = this._getRatio(),
width = _this$_getRatio.width,
height = _this$_getRatio.height;
var svg = "<svg viewBox=\"0 0 ".concat(width, " ").concat(height, "\" xmlns=\"http://www.w3.org/2000/svg\" />");
return /*#__PURE__*/React.createElement("img", {
"data-hook": 'proportion-aspect',
className: classes.ratioHolder,
src: "data:image/svg+xml,".concat(encodeURIComponent(svg))
});
}
}, {
key: "_getRatio",
value: function _getRatio() {
var aspectRatio = this.props.aspectRatio;
return {
width: aspectRatio ? Math.round(aspectRatio * 100) : 100,
height: 100
};
}
}]);
return Proportion;
}(React.PureComponent);
_defineProperty(Proportion, "PREDEFINED_RATIOS", PREDEFINED_RATIOS);
_defineProperty(Proportion, "displayName", 'Proportion');
_defineProperty(Proportion, "propTypes", {
/** Applied as data-hook HTML attribute that can be used in the tests */
dataHook: PropTypes.string,
/** Children to render. */
children: PropTypes.node.isRequired,
/** A css class to be applied to the component's root element */
className: PropTypes.string,
/** Condition for wrapping content with Proportion or return original */
/** Predefined Proportion.square (1), Proportion.portrait (3/4), Proportion.cinema (16/9), Proportion.landscape (4/3), or a custom number (width / height) or 'none' for original size */
aspectRatio: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
});
_defineProperty(Proportion, "defaultProps", {
aspectRatio: 1
});
export default Proportion;