react-svg-textures
Version:
Textures.js ported to React. Fully isomorphic.
107 lines (80 loc) • 4.2 kB
JavaScript
;
exports.__esModule = true;
exports.default = undefined;
var _class, _temp2;
var _react = require('react');
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _textures = require('textures');
var _textures2 = _interopRequireDefault(_textures);
var _lodash = require('lodash.isequal');
var _lodash2 = _interopRequireDefault(_lodash);
var _selection = require('./selection');
var _selection2 = _interopRequireDefault(_selection);
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 _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 Paths = (_temp2 = _class = function (_Component) {
_inherits(Paths, _Component);
function Paths() {
var _temp, _this, _ret;
_classCallCheck(this, Paths);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = {
pattern: null
}, _temp), _possibleConstructorReturn(_this, _ret);
}
Paths.prototype.componentWillMount = function componentWillMount() {
this.generate();
};
Paths.prototype.componentDidUpdate = function componentDidUpdate(oldProps) {
if (!(0, _lodash2.default)(oldProps, this.props)) this.generate();
};
Paths.prototype.generate = function generate() {
var _this2 = this;
var t = _textures2.default.paths();
['size', 'strokeWidth', 'shapeRendering', 'stroke', 'background', 'id', 'd'].forEach(function (key) {
if (_this2.props[key]) t[key](_this2.props[key]);
});
var selection = new _selection2.default();
t(selection);
this.setState({
pattern: selection.toReact(this.props.components)
});
};
Paths.prototype.render = function render() {
return this.state.pattern;
};
return Paths;
}(_react.Component), _class.displayName = 'Paths', _class.defaultProps = {
components: {
defs: 'defs',
g: 'g',
circle: 'circle',
rect: 'rect',
pattern: 'pattern',
path: 'path'
}
}, _temp2);
exports.default = Paths;
Paths.propTypes = process.env.NODE_ENV !== "production" ? {
size: _propTypes2.default.number,
strokeWidth: _propTypes2.default.number,
d: _propTypes2.default.oneOfType([_propTypes2.default.oneOf(['squares', 'nylon', 'waves', 'woven', 'caps', 'crosses', 'hexagons']), _propTypes2.default.func]),
shapeRendering: _propTypes2.default.string,
stroke: _propTypes2.default.string,
background: _propTypes2.default.string,
id: _propTypes2.default.string,
components: _propTypes2.default.shape({
defs: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
g: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
circle: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
rect: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
pattern: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string]),
path: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.string])
})
} : {};
module.exports = exports['default'];