cssVariables.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* eslint-disable import/prefer-default-export, prefer-destructuring */
  2. import { TinyColor } from '@ctrl/tinycolor';
  3. import { generate } from '@ant-design/colors';
  4. import { updateCSS } from '../vc-util/Dom/dynamicCSS';
  5. import canUseDom from '../_util/canUseDom';
  6. import warning from '../_util/warning';
  7. const dynamicStyleMark = `-ant-${Date.now()}-${Math.random()}`;
  8. export function getStyle(globalPrefixCls, theme) {
  9. const variables = {};
  10. const formatColor = (color, updater) => {
  11. let clone = color.clone();
  12. clone = (updater === null || updater === void 0 ? void 0 : updater(clone)) || clone;
  13. return clone.toRgbString();
  14. };
  15. const fillColor = (colorVal, type) => {
  16. const baseColor = new TinyColor(colorVal);
  17. const colorPalettes = generate(baseColor.toRgbString());
  18. variables[`${type}-color`] = formatColor(baseColor);
  19. variables[`${type}-color-disabled`] = colorPalettes[1];
  20. variables[`${type}-color-hover`] = colorPalettes[4];
  21. variables[`${type}-color-active`] = colorPalettes[6];
  22. variables[`${type}-color-outline`] = baseColor.clone().setAlpha(0.2).toRgbString();
  23. variables[`${type}-color-deprecated-bg`] = colorPalettes[0];
  24. variables[`${type}-color-deprecated-border`] = colorPalettes[2];
  25. };
  26. // ================ Primary Color ================
  27. if (theme.primaryColor) {
  28. fillColor(theme.primaryColor, 'primary');
  29. const primaryColor = new TinyColor(theme.primaryColor);
  30. const primaryColors = generate(primaryColor.toRgbString());
  31. // Legacy - We should use semantic naming standard
  32. primaryColors.forEach((color, index) => {
  33. variables[`primary-${index + 1}`] = color;
  34. });
  35. // Deprecated
  36. variables['primary-color-deprecated-l-35'] = formatColor(primaryColor, c => c.lighten(35));
  37. variables['primary-color-deprecated-l-20'] = formatColor(primaryColor, c => c.lighten(20));
  38. variables['primary-color-deprecated-t-20'] = formatColor(primaryColor, c => c.tint(20));
  39. variables['primary-color-deprecated-t-50'] = formatColor(primaryColor, c => c.tint(50));
  40. variables['primary-color-deprecated-f-12'] = formatColor(primaryColor, c => c.setAlpha(c.getAlpha() * 0.12));
  41. const primaryActiveColor = new TinyColor(primaryColors[0]);
  42. variables['primary-color-active-deprecated-f-30'] = formatColor(primaryActiveColor, c => c.setAlpha(c.getAlpha() * 0.3));
  43. variables['primary-color-active-deprecated-d-02'] = formatColor(primaryActiveColor, c => c.darken(2));
  44. }
  45. // ================ Success Color ================
  46. if (theme.successColor) {
  47. fillColor(theme.successColor, 'success');
  48. }
  49. // ================ Warning Color ================
  50. if (theme.warningColor) {
  51. fillColor(theme.warningColor, 'warning');
  52. }
  53. // ================= Error Color =================
  54. if (theme.errorColor) {
  55. fillColor(theme.errorColor, 'error');
  56. }
  57. // ================= Info Color ==================
  58. if (theme.infoColor) {
  59. fillColor(theme.infoColor, 'info');
  60. }
  61. // Convert to css variables
  62. const cssList = Object.keys(variables).map(key => `--${globalPrefixCls}-${key}: ${variables[key]};`);
  63. return `
  64. :root {
  65. ${cssList.join('\n')}
  66. }
  67. `.trim();
  68. }
  69. export function registerTheme(globalPrefixCls, theme) {
  70. const style = getStyle(globalPrefixCls, theme);
  71. if (canUseDom()) {
  72. updateCSS(style, `${dynamicStyleMark}-dynamic-theme`);
  73. } else {
  74. warning(false, 'ConfigProvider', 'SSR do not support dynamic theme with css variables.');
  75. }
  76. }