UNPKG

@quarkly/atomize

Version:

Library for creating atomic react components

75 lines (67 loc) 2.33 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _isPlainObject from "lodash/fp/isPlainObject"; import _isArray from "lodash/fp/isArray"; import bootstrap from './bootstrap'; import dict from '../constants/dict'; import normalize from '../utils/normalize-props'; import makePropInfo from '../prop-info'; var defaultStyles = Object.keys(dict); var defaultConfig = { useAliases: true }; export var isTemplate = function isTemplate(arg) { return _isArray(arg); }; export var makeComponent = function makeComponent(styled, tag, _styles, config, other) { var forwardCssProperties = config.forwardCssProperties; var defaultProps = _isPlainObject(other) ? other : undefined; var rulesCreator = bootstrap(config, defaultProps); var rules = _isArray(other) ? other : []; var pureCSS = typeof forwardCssProperties === 'boolean' ? !forwardCssProperties : typeof tag === 'string'; var denieList = ['cssObject']; if (pureCSS) denieList.push('theme'); var Component = normalize(styled(tag).withConfig({ shouldForwardProp: function shouldForwardProp(prop) { return !denieList.includes(prop); } })(rules, function (props) { var cssObject = props.cssObject; return cssObject; }), rulesCreator, pureCSS); if (config.name) { Component.displayName = config.name; } if (config.description) { Component.description = config.description; } if (config.effects) { Component.effects = config.effects; } if (config.overrides) { Component.overrides = config.overrides; } Component.propInfo = makePropInfo(config.propInfo || {}); Component.defaultProps = defaultProps; return Component; }; export var makeAtom = function makeAtom(styled) { return function (tag, config, defaultProps) { if (config === void 0) { config = {}; } var styles = config.styles || defaultStyles; if (isTemplate(config)) { return makeComponent(styled, tag, styles, {}, config); } return makeComponent(styled, tag, styles, _extends(_extends({}, defaultConfig), config), defaultProps); }; }; export var wrap = function wrap(styled) { var atom = makeAtom(styled); return Object.keys(styled).reduce(function (acc, tag) { acc[tag] = atom.bind(null, tag); return acc; }, function (tag) { return atom.bind(null, tag); }); };