polished
Version:
A lightweight toolset for writing styles in Javascript.
47 lines (46 loc) • 1.47 kB
JavaScript
exports.__esModule = true;
exports["default"] = important;
var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
/**
* Helper for targeting rules in a style block generated by polished modules that need !important-level specificity. Can optionally specify a rule (or rules) to target specific rules.
*
* @example
* // Styles as object usage
* const styles = {
* ...important(cover())
* }
*
* // styled-components usage
* const div = styled.div`
* ${important(cover())}
* `
*
* // CSS as JS Output
*
* div: {
* 'position': 'absolute !important',
* 'top': '0 !important',
* 'right: '0 !important',
* 'bottom': '0 !important',
* 'left: '0 !important'
* }
*/
function important(styleBlock, rules) {
if (typeof styleBlock !== 'object' || styleBlock === null) {
throw new _errors["default"](75, typeof styleBlock);
}
var newStyleBlock = {};
Object.keys(styleBlock).forEach(function (key) {
if (typeof styleBlock[key] === 'object' && styleBlock[key] !== null) {
newStyleBlock[key] = important(styleBlock[key], rules);
} else if (!rules || rules && (rules === key || rules.indexOf(key) >= 0)) {
newStyleBlock[key] = styleBlock[key] + " !important";
} else {
newStyleBlock[key] = styleBlock[key];
}
});
return newStyleBlock;
}
module.exports = exports.default;
;