utils.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _typeof from "@babel/runtime/helpers/esm/typeof";
  3. import { generate as generateColor } from '@ant-design/colors';
  4. import { updateCSS } from "rc-util/es/Dom/dynamicCSS";
  5. import { getShadowRoot } from "rc-util/es/Dom/shadow";
  6. import warn from "rc-util/es/warning";
  7. import React, { useContext, useEffect } from 'react';
  8. import IconContext from "./components/Context";
  9. function camelCase(input) {
  10. return input.replace(/-(.)/g, function (match, g) {
  11. return g.toUpperCase();
  12. });
  13. }
  14. export function warning(valid, message) {
  15. warn(valid, "[@ant-design/icons] ".concat(message));
  16. }
  17. export function isIconDefinition(target) {
  18. return _typeof(target) === 'object' && typeof target.name === 'string' && typeof target.theme === 'string' && (_typeof(target.icon) === 'object' || typeof target.icon === 'function');
  19. }
  20. export function normalizeAttrs() {
  21. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  22. return Object.keys(attrs).reduce(function (acc, key) {
  23. var val = attrs[key];
  24. switch (key) {
  25. case 'class':
  26. acc.className = val;
  27. delete acc.class;
  28. break;
  29. default:
  30. delete acc[key];
  31. acc[camelCase(key)] = val;
  32. }
  33. return acc;
  34. }, {});
  35. }
  36. export function generate(node, key, rootProps) {
  37. if (!rootProps) {
  38. return /*#__PURE__*/React.createElement(node.tag, _objectSpread({
  39. key: key
  40. }, normalizeAttrs(node.attrs)), (node.children || []).map(function (child, index) {
  41. return generate(child, "".concat(key, "-").concat(node.tag, "-").concat(index));
  42. }));
  43. }
  44. return /*#__PURE__*/React.createElement(node.tag, _objectSpread(_objectSpread({
  45. key: key
  46. }, normalizeAttrs(node.attrs)), rootProps), (node.children || []).map(function (child, index) {
  47. return generate(child, "".concat(key, "-").concat(node.tag, "-").concat(index));
  48. }));
  49. }
  50. export function getSecondaryColor(primaryColor) {
  51. // choose the second color
  52. return generateColor(primaryColor)[0];
  53. }
  54. export function normalizeTwoToneColors(twoToneColor) {
  55. if (!twoToneColor) {
  56. return [];
  57. }
  58. return Array.isArray(twoToneColor) ? twoToneColor : [twoToneColor];
  59. }
  60. // These props make sure that the SVG behaviours like general text.
  61. // Reference: https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4
  62. export var svgBaseProps = {
  63. width: '1em',
  64. height: '1em',
  65. fill: 'currentColor',
  66. 'aria-hidden': 'true',
  67. focusable: 'false'
  68. };
  69. export var iconStyles = "\n.anticon {\n display: inline-flex;\n align-items: center;\n color: inherit;\n font-style: normal;\n line-height: 0;\n text-align: center;\n text-transform: none;\n vertical-align: -0.125em;\n text-rendering: optimizeLegibility;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\n.anticon > * {\n line-height: 1;\n}\n\n.anticon svg {\n display: inline-block;\n}\n\n.anticon::before {\n display: none;\n}\n\n.anticon .anticon-icon {\n display: block;\n}\n\n.anticon[tabindex] {\n cursor: pointer;\n}\n\n.anticon-spin::before,\n.anticon-spin {\n display: inline-block;\n -webkit-animation: loadingCircle 1s infinite linear;\n animation: loadingCircle 1s infinite linear;\n}\n\n@-webkit-keyframes loadingCircle {\n 100% {\n -webkit-transform: rotate(360deg);\n transform: rotate(360deg);\n }\n}\n\n@keyframes loadingCircle {\n 100% {\n -webkit-transform: rotate(360deg);\n transform: rotate(360deg);\n }\n}\n";
  70. export var useInsertStyles = function useInsertStyles(eleRef) {
  71. var _useContext = useContext(IconContext),
  72. csp = _useContext.csp,
  73. prefixCls = _useContext.prefixCls,
  74. layer = _useContext.layer;
  75. var mergedStyleStr = iconStyles;
  76. if (prefixCls) {
  77. mergedStyleStr = mergedStyleStr.replace(/anticon/g, prefixCls);
  78. }
  79. if (layer) {
  80. mergedStyleStr = "@layer ".concat(layer, " {\n").concat(mergedStyleStr, "\n}");
  81. }
  82. useEffect(function () {
  83. var ele = eleRef.current;
  84. var shadowRoot = getShadowRoot(ele);
  85. updateCSS(mergedStyleStr, '@ant-design-icons', {
  86. prepend: !layer,
  87. csp: csp,
  88. attachTo: shadowRoot
  89. });
  90. }, []);
  91. };