react-css-components
Version:
Define styled React components using CSS based module format
79 lines (69 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = transformVariants;
var _Syntax = require('./Syntax');
var Syntax = _interopRequireWildcard(_Syntax);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /**
* @copyright 2016-present, React CSS Components team
*
*/
/**
* Variant names we want to see compiled as CSS pseudo classes.
*/
var _SUPPORTED_PSEUDO_CLASSES = {
focus: true,
hover: true,
active: true,
checked: true,
default: true,
disabled: true,
empty: true,
enabled: true,
firstChild: true,
fullscreen: true,
indeterminate: true,
invalid: true,
lastChild: true,
left: true,
link: true,
onlyChild: true,
optional: true,
required: true,
right: true,
root: true,
scope: true,
target: true,
valid: true,
visited: true
};
function transformVariants(root) {
var toAppend = [];
root.each(function (rule) {
if (Syntax.isComponent(rule)) {
toAppend = toAppend.concat(flattenVariants(rule));
}
});
root.append.apply(root, _toConsumableArray(toAppend));
return root;
}
function flattenVariants(rule) {
var toRemove = [];
var toAppend = [];
rule.each(function (variant) {
var _toAppend;
if (!Syntax.isVariant(variant)) {
return;
}
toRemove.push(variant);
variant = variant.clone();
variant.selector = rule.selector + variant.selector;
toAppend = (_toAppend = toAppend).concat.apply(_toAppend, [variant].concat(_toConsumableArray(flattenVariants(variant))));
});
toRemove.forEach(function (variant) {
return variant.remove();
});
return toAppend;
}