index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { unit } from '@ant-design/cssinjs';
  2. import { resetComponent } from '../../style';
  3. import { genStyleHooks, mergeToken } from '../../theme/internal';
  4. const genBaseStyle = token => {
  5. const {
  6. antCls,
  7. componentCls,
  8. iconCls,
  9. avatarBg,
  10. avatarColor,
  11. containerSize,
  12. containerSizeLG,
  13. containerSizeSM,
  14. textFontSize,
  15. textFontSizeLG,
  16. textFontSizeSM,
  17. iconFontSize,
  18. iconFontSizeLG,
  19. iconFontSizeSM,
  20. borderRadius,
  21. borderRadiusLG,
  22. borderRadiusSM,
  23. lineWidth,
  24. lineType
  25. } = token;
  26. // Avatar size style
  27. const avatarSizeStyle = (size, fontSize, iconFontSize, radius) => ({
  28. width: size,
  29. height: size,
  30. borderRadius: '50%',
  31. fontSize,
  32. [`&${componentCls}-square`]: {
  33. borderRadius: radius
  34. },
  35. [`&${componentCls}-icon`]: {
  36. fontSize: iconFontSize,
  37. [`> ${iconCls}`]: {
  38. margin: 0
  39. }
  40. }
  41. });
  42. return {
  43. [componentCls]: Object.assign(Object.assign(Object.assign(Object.assign({}, resetComponent(token)), {
  44. position: 'relative',
  45. display: 'inline-flex',
  46. justifyContent: 'center',
  47. alignItems: 'center',
  48. overflow: 'hidden',
  49. color: avatarColor,
  50. whiteSpace: 'nowrap',
  51. textAlign: 'center',
  52. verticalAlign: 'middle',
  53. background: avatarBg,
  54. border: `${unit(lineWidth)} ${lineType} transparent`,
  55. '&-image': {
  56. background: 'transparent'
  57. },
  58. [`${antCls}-image-img`]: {
  59. display: 'block'
  60. }
  61. }), avatarSizeStyle(containerSize, textFontSize, iconFontSize, borderRadius)), {
  62. '&-lg': Object.assign({}, avatarSizeStyle(containerSizeLG, textFontSizeLG, iconFontSizeLG, borderRadiusLG)),
  63. '&-sm': Object.assign({}, avatarSizeStyle(containerSizeSM, textFontSizeSM, iconFontSizeSM, borderRadiusSM)),
  64. '> img': {
  65. display: 'block',
  66. width: '100%',
  67. height: '100%',
  68. objectFit: 'cover'
  69. }
  70. })
  71. };
  72. };
  73. const genGroupStyle = token => {
  74. const {
  75. componentCls,
  76. groupBorderColor,
  77. groupOverlapping,
  78. groupSpace
  79. } = token;
  80. return {
  81. [`${componentCls}-group`]: {
  82. display: 'inline-flex',
  83. [componentCls]: {
  84. borderColor: groupBorderColor
  85. },
  86. '> *:not(:first-child)': {
  87. marginInlineStart: groupOverlapping
  88. }
  89. },
  90. [`${componentCls}-group-popover`]: {
  91. [`${componentCls} + ${componentCls}`]: {
  92. marginInlineStart: groupSpace
  93. }
  94. }
  95. };
  96. };
  97. export const prepareComponentToken = token => {
  98. const {
  99. controlHeight,
  100. controlHeightLG,
  101. controlHeightSM,
  102. fontSize,
  103. fontSizeLG,
  104. fontSizeXL,
  105. fontSizeHeading3,
  106. marginXS,
  107. marginXXS,
  108. colorBorderBg
  109. } = token;
  110. return {
  111. containerSize: controlHeight,
  112. containerSizeLG: controlHeightLG,
  113. containerSizeSM: controlHeightSM,
  114. textFontSize: fontSize,
  115. textFontSizeLG: fontSize,
  116. textFontSizeSM: fontSize,
  117. iconFontSize: Math.round((fontSizeLG + fontSizeXL) / 2),
  118. iconFontSizeLG: fontSizeHeading3,
  119. iconFontSizeSM: fontSize,
  120. groupSpace: marginXXS,
  121. groupOverlapping: -marginXS,
  122. groupBorderColor: colorBorderBg
  123. };
  124. };
  125. export default genStyleHooks('Avatar', token => {
  126. const {
  127. colorTextLightSolid,
  128. colorTextPlaceholder
  129. } = token;
  130. const avatarToken = mergeToken(token, {
  131. avatarBg: colorTextPlaceholder,
  132. avatarColor: colorTextLightSolid
  133. });
  134. return [genBaseStyle(avatarToken), genGroupStyle(avatarToken)];
  135. }, prepareComponentToken);