diff --git a/components/avatar/avatar.tsx b/components/avatar/avatar.tsx index dba3a841cb1b..080cb27c376e 100644 --- a/components/avatar/avatar.tsx +++ b/components/avatar/avatar.tsx @@ -12,6 +12,7 @@ import useBreakpoint from '../grid/hooks/useBreakpoint'; import type { AvatarContextType, AvatarSize } from './AvatarContext'; import AvatarContext from './AvatarContext'; import useStyle from './style'; +import useCSSVar from './style/cssVar'; export interface AvatarProps { /** Shape of avatar, options: `circle`, `square` */ @@ -144,7 +145,8 @@ const InternalAvatar: React.ForwardRefRenderFunction('Avatar', prepareComponentToken); diff --git a/components/avatar/style/index.ts b/components/avatar/style/index.ts index 1b363bc3cb5e..c611f1d496d7 100644 --- a/components/avatar/style/index.ts +++ b/components/avatar/style/index.ts @@ -1,6 +1,7 @@ -import type { CSSObject } from '@ant-design/cssinjs'; +import { unit, type CSSObject } from '@ant-design/cssinjs'; + import { resetComponent } from '../../style'; -import type { FullToken, GenerateStyle } from '../../theme/internal'; +import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal'; import { genComponentStyleHook, mergeToken } from '../../theme/internal'; export interface ComponentToken { @@ -75,13 +76,14 @@ const genBaseStyle: GenerateStyle = (token) => { borderRadiusSM, lineWidth, lineType, + calc, } = token; // Avatar size style const avatarSizeStyle = (size: number, fontSize: number, radius: number): CSSObject => ({ width: size, height: size, - lineHeight: `${size - lineWidth * 2}px`, + lineHeight: unit(calc(size).sub(calc(lineWidth).mul(2)).equal()), borderRadius: '50%', [`&${componentCls}-square`]: { @@ -116,7 +118,7 @@ const genBaseStyle: GenerateStyle = (token) => { textAlign: 'center', verticalAlign: 'middle', background: avatarBg, - border: `${lineWidth}px ${lineType} transparent`, + border: `${unit(lineWidth)} ${lineType} transparent`, [`&-image`]: { background: 'transparent', @@ -169,7 +171,33 @@ const genGroupStyle: GenerateStyle = (token) => { }; }; -export default genComponentStyleHook( +export const prepareComponentToken: GetDefaultToken<'Avatar'> = (token) => { + const { + controlHeight, + controlHeightLG, + controlHeightSM, + fontSize, + fontSizeLG, + fontSizeXL, + fontSizeHeading3, + marginXS, + marginXXS, + colorBorderBg, + } = token; + return { + containerSize: controlHeight, + containerSizeLG: controlHeightLG, + containerSizeSM: controlHeightSM, + textFontSize: Math.round((fontSizeLG + fontSizeXL) / 2), + textFontSizeLG: fontSizeHeading3, + textFontSizeSM: fontSize, + groupSpace: marginXXS, + groupOverlapping: -marginXS, + groupBorderColor: colorBorderBg, + }; +}; + +export default genComponentStyleHook<'Avatar'>( 'Avatar', (token) => { const { colorTextLightSolid, colorTextPlaceholder } = token; @@ -179,33 +207,5 @@ export default genComponentStyleHook( }); return [genBaseStyle(avatarToken), genGroupStyle(avatarToken)]; }, - (token) => { - const { - controlHeight, - controlHeightLG, - controlHeightSM, - - fontSize, - fontSizeLG, - fontSizeXL, - fontSizeHeading3, - - marginXS, - marginXXS, - colorBorderBg, - } = token; - return { - containerSize: controlHeight, - containerSizeLG: controlHeightLG, - containerSizeSM: controlHeightSM, - - textFontSize: Math.round((fontSizeLG + fontSizeXL) / 2), - textFontSizeLG: fontSizeHeading3, - textFontSizeSM: fontSize, - - groupSpace: marginXXS, - groupOverlapping: -marginXS, - groupBorderColor: colorBorderBg, - }; - }, + prepareComponentToken, );