UNPKG

react-kube

Version:

Kube CSS in React Components

69 lines (55 loc) 2.92 kB
"use strict"; 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; }; })(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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) subClass.__proto__ = superClass; } var _react = require("react"); var _react2 = _interopRequireDefault(_react); var TextFit = (function (_React$Component) { function TextFit() { _classCallCheck(this, TextFit); if (_React$Component != null) { _React$Component.apply(this, arguments); } } _inherits(TextFit, _React$Component); _createClass(TextFit, [{ key: "componentDidMount", value: function componentDidMount() { window.addEventListener("resize", this.resizeText.bind(this)); this.resizeText(); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener("resize", this.resizeText.bind(this)); } }, { key: "resizeText", value: function resizeText() { var element = _react2["default"].findDOMNode(this); var width = element.offsetWidth; element.style.fontSize = Math.max(Math.min(width / (this.props.compressor * 10), parseFloat(this.props.maxFontSize)), parseFloat(this.props.minFontSize)) + "px"; } }, { key: "render", value: function render() { return this.props.children; } }]); return TextFit; })(_react2["default"].Component); TextFit.propTypes = { children: _react2["default"].PropTypes.node.isRequired, className: _react2["default"].PropTypes.string, compressor: _react2["default"].PropTypes.number, maxFontSize: _react2["default"].PropTypes.number, minFontSize: _react2["default"].PropTypes.number, style: _react2["default"].PropTypes.object }; TextFit.defaultProps = { compressor: 1, minFontSize: Number.NEGATIVE_INFINITY, maxFontSize: Number.POSITIVE_INFINITY }; module.exports = TextFit;