important.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = important;
  4. var _errors = /*#__PURE__*/_interopRequireDefault( /*#__PURE__*/require("../internalHelpers/_errors"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  6. /**
  7. * 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.
  8. *
  9. * @example
  10. * // Styles as object usage
  11. * const styles = {
  12. * ...important(cover())
  13. * }
  14. *
  15. * // styled-components usage
  16. * const div = styled.div`
  17. * ${important(cover())}
  18. * `
  19. *
  20. * // CSS as JS Output
  21. *
  22. * div: {
  23. * 'position': 'absolute !important',
  24. * 'top': '0 !important',
  25. * 'right: '0 !important',
  26. * 'bottom': '0 !important',
  27. * 'left: '0 !important'
  28. * }
  29. */
  30. function important(styleBlock, rules) {
  31. if (typeof styleBlock !== 'object' || styleBlock === null) {
  32. throw new _errors["default"](75, typeof styleBlock);
  33. }
  34. var newStyleBlock = {};
  35. Object.keys(styleBlock).forEach(function (key) {
  36. if (typeof styleBlock[key] === 'object' && styleBlock[key] !== null) {
  37. newStyleBlock[key] = important(styleBlock[key], rules);
  38. } else if (!rules || rules && (rules === key || rules.indexOf(key) >= 0)) {
  39. newStyleBlock[key] = styleBlock[key] + " !important";
  40. } else {
  41. newStyleBlock[key] = styleBlock[key];
  42. }
  43. });
  44. return newStyleBlock;
  45. }
  46. module.exports = exports.default;