UNPKG

@dash-ui/compound

Version:

A utility for creating compound styles with dash-ui

111 lines (87 loc) 3.02 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Compound = factory()); })(this, (function () { 'use strict'; function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); } /** * A factory function that creates a compound styles utility * * @param styles */ function compound(styles) { /** * A function for creating compound/multi-variant styles * * @param styleMap * @param options */ return function compoundStyles(styleMap, options) { if (options === void 0) { options = emptyObj; } var cache = {}; var mapKeys = Object.keys(styleMap); function atomicCss(compoundMap) { var key = JSON.stringify(compoundMap); var cached = cache[key]; if (cached) return cached; var output = // @ts-expect-error typeof styleMap.default === "function" ? [// @ts-expect-error styleMap.default.css()] : []; for (var i = 0; i < mapKeys.length; i++) { var _key2; var _key = mapKeys[i]; if (_key === "default") continue; var value = compoundMap[_key]; if (value === void 0 || value === null) continue; output.push((_key2 = styleMap[_key]) === null || _key2 === void 0 ? void 0 : _key2.css(value)); } return cache[key] = output; } function css(compoundMap) { var _ref; return (_ref = "").concat.apply(_ref, atomicCss(compoundMap)); } function compoundStyle(compoundMap, compoundOptions) { var _compoundOptions$atom; if (compoundMap === void 0) { compoundMap = {}; } if (compoundOptions === void 0) { compoundOptions = emptyObj; } if ((_compoundOptions$atom = compoundOptions.atomic) !== null && _compoundOptions$atom !== void 0 ? _compoundOptions$atom : options.atomic) { var _css = atomicCss(compoundMap); var classes = ""; for (var i = 0; i < _css.length; i++) { classes += styles.cls(_css[i]) + (i === _css.length - 1 ? "" : " "); } return classes; } return styles.cls(css(compoundMap)); } return _extends(compoundStyle, { css: css, atomicCss: atomicCss, styles: styleMap }); }; } var emptyObj = {}; return compound; })); //# sourceMappingURL=compound.dev.js.map