icon.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { defaultIconProps } from '../icon/defaults.mjs';
  2. import { getCommonCSSRules, generateItemCSSRules, generateItemContent } from './common.mjs';
  3. import { formatCSS } from './format.mjs';
  4. import '../svg/html.mjs';
  5. import '../svg/size.mjs';
  6. import '../svg/url.mjs';
  7. import '../icon/square.mjs';
  8. import '../svg/build.mjs';
  9. import '../customisations/defaults.mjs';
  10. import '../svg/defs.mjs';
  11. function getIconCSS(icon, options = {}) {
  12. const body = options.customise ? options.customise(icon.body) : icon.body;
  13. const mode = options.mode || (options.color || !body.includes("currentColor") ? "background" : "mask");
  14. let varName = options.varName;
  15. if (varName === void 0 && mode === "mask") {
  16. varName = "svg";
  17. }
  18. const newOptions = {
  19. ...options,
  20. // Override mode and varName
  21. mode,
  22. varName
  23. };
  24. if (mode === "background") {
  25. delete newOptions.varName;
  26. }
  27. const rules = {
  28. ...options.rules,
  29. ...getCommonCSSRules(newOptions),
  30. ...generateItemCSSRules(
  31. {
  32. ...defaultIconProps,
  33. ...icon,
  34. body
  35. },
  36. newOptions
  37. )
  38. };
  39. const selector = options.iconSelector || ".icon";
  40. return formatCSS(
  41. [
  42. {
  43. selector,
  44. rules
  45. }
  46. ],
  47. newOptions.format
  48. );
  49. }
  50. function getIconContentCSS(icon, options) {
  51. const body = options.customise ? options.customise(icon.body) : icon.body;
  52. const content = generateItemContent(
  53. {
  54. ...defaultIconProps,
  55. ...icon,
  56. body
  57. },
  58. options
  59. );
  60. const selector = options.iconSelector || ".icon::after";
  61. return formatCSS(
  62. [
  63. {
  64. selector,
  65. rules: {
  66. ...options.rules,
  67. content
  68. }
  69. }
  70. ],
  71. options.format
  72. );
  73. }
  74. export { getIconCSS, getIconContentCSS };