File size: 4,730 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
import { unit } from '@ant-design/cssinjs';
import type { CSSObject } from '@ant-design/cssinjs';
import { resetComponent } from '../../style';
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genStyleHooks, mergeToken } from '../../theme/internal';
export interface ComponentToken {
/**
* @desc 头像尺寸
* @descEN Size of Avatar
*/
containerSize: number;
/**
* @desc 大号头像尺寸
* @descEN Size of large Avatar
*/
containerSizeLG: number;
/**
* @desc 小号头像尺寸
* @descEN Size of small Avatar
*/
containerSizeSM: number;
/**
* @desc 头像文字大小
* @descEN Font size of Avatar
*/
textFontSize: number;
/**
* @desc 大号头像文字大小
* @descEN Font size of large Avatar
*/
textFontSizeLG: number;
/**
* @desc 小号头像文字大小
* @descEN Font size of small Avatar
*/
textFontSizeSM: number;
/**
* @desc 头像组间距
* @descEN Spacing between avatars in a group
*/
groupSpace: number;
/**
* @desc 头像组重叠宽度
* @descEN Overlapping of avatars in a group
*/
groupOverlapping: number;
/**
* @desc 头像组边框颜色
* @descEN Border color of avatars in a group
*/
groupBorderColor: string;
}
/**
* @desc Avatar 组件的 Token
* @descEN Token for Avatar component
*/
type AvatarToken = FullToken<'Avatar'> & {
avatarBgColor: string;
avatarBg: string;
avatarColor: string;
};
const genBaseStyle: GenerateStyle<AvatarToken> = (token) => {
const {
antCls,
componentCls,
iconCls,
avatarBg,
avatarColor,
containerSize,
containerSizeLG,
containerSizeSM,
textFontSize,
textFontSizeLG,
textFontSizeSM,
borderRadius,
borderRadiusLG,
borderRadiusSM,
lineWidth,
lineType,
} = token;
// Avatar size style
const avatarSizeStyle = (size: number, fontSize: number, radius: number): CSSObject => ({
width: size,
height: size,
borderRadius: '50%',
[`&${componentCls}-square`]: {
borderRadius: radius,
},
[`&${componentCls}-icon`]: {
fontSize,
[`> ${iconCls}`]: {
margin: 0,
},
},
});
return {
[componentCls]: {
...resetComponent(token),
position: 'relative',
display: 'inline-flex',
justifyContent: 'center',
alignItems: 'center',
overflow: 'hidden',
color: avatarColor,
whiteSpace: 'nowrap',
textAlign: 'center',
verticalAlign: 'middle',
background: avatarBg,
border: `${unit(lineWidth)} ${lineType} transparent`,
'&-image': {
background: 'transparent',
},
[`${antCls}-image-img`]: {
display: 'block',
},
...avatarSizeStyle(containerSize, textFontSize, borderRadius),
'&-lg': {
...avatarSizeStyle(containerSizeLG, textFontSizeLG, borderRadiusLG),
},
'&-sm': {
...avatarSizeStyle(containerSizeSM, textFontSizeSM, borderRadiusSM),
},
'> img': {
display: 'block',
width: '100%',
height: '100%',
objectFit: 'cover',
},
},
};
};
const genGroupStyle: GenerateStyle<AvatarToken> = (token) => {
const { componentCls, groupBorderColor, groupOverlapping, groupSpace } = token;
return {
[`${componentCls}-group`]: {
display: 'inline-flex',
[componentCls]: {
borderColor: groupBorderColor,
},
'> *:not(:first-child)': {
marginInlineStart: groupOverlapping,
},
},
[`${componentCls}-group-popover`]: {
[`${componentCls} + ${componentCls}`]: {
marginInlineStart: groupSpace,
},
},
};
};
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 genStyleHooks(
'Avatar',
(token) => {
const { colorTextLightSolid, colorTextPlaceholder } = token;
const avatarToken = mergeToken<AvatarToken>(token, {
avatarBg: colorTextPlaceholder,
avatarColor: colorTextLightSolid,
});
return [genBaseStyle(avatarToken), genGroupStyle(avatarToken)];
},
prepareComponentToken,
);
|