UNPKG

rmwc

Version:

A thin React wrapper for Material Design (Web) Components

132 lines (100 loc) 7.71 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShapeContainer = undefined; 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 _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _react = require('react'); var React = _interopRequireWildcard(_react); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _randomId = require('../Base/utils/randomId'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } 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 mdcShapeAngledCornerBackground = function mdcShapeAngledCornerBackground(rootId, backgroundColor) { return '\n #' + rootId + ' .mdc-shape-container__corner:after {\n background-color: ' + backgroundColor + ';\n }\n '; }; var mdcShapeAngledCornerOutline = function mdcShapeAngledCornerOutline(rootId, outlineWidth, outlineColor, outlineStyle) { return '\n #' + rootId + ' .mdc-shape-container__corner:before {\n top: ' + outlineWidth + 'px;\n border-bottom: ' + outlineWidth + 'px ' + (outlineStyle || 'solid') + ' ' + outlineColor + ';\n }\n '; }; var mdcShapeAngledCorner = function mdcShapeAngledCorner(rootId, backgroundColor, topLeftSize, topRightSize, bottomRightSize, bottomLeftSize) { topRightSize = topRightSize !== undefined ? topRightSize : topLeftSize; bottomRightSize = bottomRightSize !== undefined ? bottomRightSize : topLeftSize; bottomLeftSize = bottomLeftSize !== undefined ? bottomLeftSize : topLeftSize; var corners = [['top', 'left', topLeftSize], ['top', 'right', topRightSize], ['bottom', 'right', bottomRightSize], ['bottom', 'left', bottomLeftSize]]; var cornerStyles = corners.map(function (_ref) { var _ref2 = _slicedToArray(_ref, 3), y = _ref2[0], x = _ref2[1], size = _ref2[2]; var diagonalLength = Number(size) * 1.4142135623730951; return '\n #' + rootId + ' .mdc-shape-container__corner--' + y + '-' + x + ' {\n ' + y + ': -' + diagonalLength / 2 + 'px;\n ' + x + ': -' + diagonalLength / 2 + 'px;\n width: ' + diagonalLength + 'px;\n height: ' + diagonalLength + 'px;\n }\n '; }).join('\n'); var backgroundStyle = mdcShapeAngledCornerBackground(rootId, backgroundColor); return '\n ' + cornerStyles + '\n ' + backgroundStyle + '\n '; }; /** A container for shaping other components. */ var ShapeContainer = exports.ShapeContainer = function (_React$Component) { _inherits(ShapeContainer, _React$Component); function ShapeContainer(props) { _classCallCheck(this, ShapeContainer); var _this = _possibleConstructorReturn(this, (ShapeContainer.__proto__ || Object.getPrototypeOf(ShapeContainer)).call(this, props)); _this.generatedId = (0, _randomId.randomId)('shape'); return _this; } _createClass(ShapeContainer, [{ key: 'render', value: function render() { var _props = this.props, children = _props.children, className = _props.className, backgroundColor = _props.backgroundColor, corner = _props.corner, topLeftCorner = _props.topLeftCorner, topRightCorner = _props.topRightCorner, bottomRightCorner = _props.bottomRightCorner, bottomLeftCorner = _props.bottomLeftCorner, outlineWidth = _props.outlineWidth, outlineColor = _props.outlineColor, outlineStyle = _props.outlineStyle, rest = _objectWithoutProperties(_props, ['children', 'className', 'backgroundColor', 'corner', 'topLeftCorner', 'topRightCorner', 'bottomRightCorner', 'bottomLeftCorner', 'outlineWidth', 'outlineColor', 'outlineStyle']); var rootId = 'shape-' + this.generatedId; return React.createElement( 'div', Object.assign({}, rest, { id: 'shape-' + this.generatedId, className: (0, _classnames2.default)(className, 'mdc-shape-container') }), React.createElement( 'style', null, mdcShapeAngledCorner(rootId, backgroundColor || '', topLeftCorner || corner || 0, topRightCorner || corner || 0, bottomRightCorner || corner || 0, bottomLeftCorner || corner || 0), !!outlineWidth && !!outlineColor && mdcShapeAngledCornerOutline(rootId, outlineWidth, outlineColor, outlineStyle || null) ), children, React.createElement('div', { className: 'mdc-shape-container__corner mdc-shape-container__corner--top-left' }), React.createElement('div', { className: 'mdc-shape-container__corner mdc-shape-container__corner--top-right' }), React.createElement('div', { className: 'mdc-shape-container__corner mdc-shape-container__corner--bottom-right' }), React.createElement('div', { className: 'mdc-shape-container__corner mdc-shape-container__corner--bottom-left' }) ); } }]); return ShapeContainer; }(React.Component); Object.defineProperty(ShapeContainer, 'displayName', { enumerable: true, writable: true, value: 'ShapeContainer' }); Object.defineProperty(ShapeContainer, 'defaultProps', { enumerable: true, writable: true, value: { backgroundColor: '#fff' } });