UNPKG

react-svg-textures

Version:

Textures.js ported to React. Fully isomorphic.

49 lines (38 loc) 1.53 kB
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; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } import React from 'react'; import camel from 'lodash.camelcase'; 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[camel(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 ? React.createElement( the.component, _extends({}, this.attrs, { key: this.attrs.id || rand() }), children ) : children[0]; }; return Selection; }(); export { Selection as default };