react-svg-textures
Version:
Textures.js ported to React. Fully isomorphic.
62 lines (45 loc) • 1.87 kB
JavaScript
;
exports.__esModule = true;
exports.default = undefined;
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 _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _lodash = require('lodash.camelcase');
var _lodash2 = _interopRequireDefault(_lodash);
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"); } }
var rand = function rand() {
return (Math.random().toString(36) + '00000000000000000').replace(/[^a-z]+/g, '').slice(0, 5);
};
var Selection = function () {
function Selection(tagName) {
_classCallCheck(this, Selection);
this.children = [];
this.attrs = {};
this.tagName = tagName;
return this;
}
Selection.prototype.append = function append(tagName) {
var child = new Selection(tagName);
this.children.push(child);
return child;
};
Selection.prototype.attr = function attr(key, value) {
this.attrs[(0, _lodash2.default)(key)] = value;
return this;
};
Selection.prototype.toReact = function toReact(components) {
var children = this.children.map(function (child) {
return child.toReact(components);
});
var the = { component: components[this.tagName] };
return this.tagName ? _react2.default.createElement(
the.component,
_extends({}, this.attrs, { key: this.attrs.id || rand() }),
children
) : children[0];
};
return Selection;
}();
exports.default = Selection;
module.exports = exports['default'];