Avatar
Avatar (아바타)
Avatar는 사용자나 대상을 나타내는 원형(기본) 또는 사각형 이미지·아이콘·문자 컴포넌트예요.
출처: 문서
본문
예제 (Examples)
기본 (Basic)
세 가지 크기와 두 가지 모양을 사용할 수 있어요.
import React from 'react';
import { UserOutlined } from '@ant-design/icons';
import { Avatar, Space } from 'antd';
const App: React.FC = () => (
<Space vertical size={16}>
<Space wrap size={16}>
<Avatar size={64} icon={<UserOutlined />} />
<Avatar size="large" icon={<UserOutlined />} />
<Avatar icon={<UserOutlined />} />
<Avatar size="small" icon={<UserOutlined />} />
<Avatar size={14} icon={<UserOutlined />} />
</Space>
<Space wrap size={16}>
<Avatar shape="square" size={64} icon={<UserOutlined />} />
<Avatar shape="square" size="large" icon={<UserOutlined />} />
<Avatar shape="square" icon={<UserOutlined />} />
<Avatar shape="square" size="small" icon={<UserOutlined />} />
<Avatar shape="square" size={14} icon={<UserOutlined />} />
</Space>
</Space>
);
export default App;
타입 (Type)
이미지, 아이콘, 문자를 지원하고, 뒤의 두 종류 아바타는 색과 배경색을 커스터마이즈할 수 있어요.
import React from 'react';
import { UserOutlined } from '@ant-design/icons';
import { Avatar, Space } from 'antd';
const url = 'https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg';
const App: React.FC = () => (
<Space size={16} wrap>
<Avatar icon={<UserOutlined />} />
<Avatar>U</Avatar>
<Avatar size={40}>USER</Avatar>
<Avatar src={url} />
<Avatar src={<img draggable={false} src={url} alt="avatar" />} />
<Avatar style={{ backgroundColor: '#fde3cf', color: '#f56a00' }}>U</Avatar>
<Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
</Space>
);
export default App;
폰트 크기 자동 설정 (Autoset Font Size)
문자 타입 아바타에서 문자가 너무 길어 표시가 어려울 때, 폰트 크기를 아바타 너비에 맞춰 자동 조절할 수 있어요. gap으로 좌우 사이의 단위 거리를 설정할 수도 있어요.
import React, { useState } from 'react';
import { Avatar, Button } from 'antd';
const UserList = ['U', 'Lucy', 'Tom', 'Edward'];
const ColorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];
const GapList = [4, 3, 2, 1];
const App: React.FC = () => {
const [user, setUser] = useState(UserList[0]);
const [color, setColor] = useState(ColorList[0]);
const [gap, setGap] = useState(GapList[0]);
const changeUser = () => {
const index = UserList.indexOf(user);
setUser(index < UserList.length - 1 ? UserList[index + 1] : UserList[0]);
setColor(index < ColorList.length - 1 ? ColorList[index + 1] : ColorList[0]);
};
const changeGap = () => {
const index = GapList.indexOf(gap);
setGap(index < GapList.length - 1 ? GapList[index + 1] : GapList[0]);
};
return (
<>
<Avatar style={{ backgroundColor: color, verticalAlign: 'middle' }} size="large" gap={gap}>
{user}
</Avatar>
<Button
size="small"
style={{ margin: '0 16px', verticalAlign: 'middle' }}
onClick={changeUser}
>
ChangeUser
</Button>
<Button size="small" style={{ verticalAlign: 'middle' }} onClick={changeGap}>
changeGap
</Button>
</>
);
};
export default App;
Badge와 함께 (With Badge)
보통 알림과 통지에 사용돼요.
import React from 'react';
import { UserOutlined } from '@ant-design/icons';
import { Avatar, Badge, Space } from 'antd';
const App: React.FC = () => (
<Space size={24}>
<Badge count={1}>
<Avatar shape="square" icon={<UserOutlined />} />
</Badge>
<Badge dot>
<Avatar shape="square" icon={<UserOutlined />} />
</Badge>
</Space>
);
export default App;
Avatar.Group
아바타 그룹 표시예요.
import React from 'react';
import { AntDesignOutlined, UserOutlined } from '@ant-design/icons';
import { Avatar, Divider, Tooltip } from 'antd';
const App: React.FC = () => (
<>
<Avatar.Group>
<Avatar src="https://api.dicebear.com/10.x/lorelei/svg?seed=1" />
<a href="https://ant.design">
<Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
</a>
<Tooltip title="Ant User" placement="top">
<Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
</Tooltip>
<Avatar style={{ backgroundColor: '#1677ff' }} icon={<AntDesignOutlined />} />
</Avatar.Group>
<Divider />
<Avatar.Group
max={{
count: 2,
style: { color: '#f56a00', backgroundColor: '#fde3cf' },
}}
>
<Avatar src="https://api.dicebear.com/10.x/lorelei/svg?seed=2" />
<Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
<Tooltip title="Ant User" placement="top">
<Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
</Tooltip>
<Avatar style={{ backgroundColor: '#1677ff' }} icon={<AntDesignOutlined />} />
</Avatar.Group>
<Divider />
<Avatar.Group
size="large"
max={{
count: 2,
style: { color: '#f56a00', backgroundColor: '#fde3cf' },
}}
>
<Avatar src="https://api.dicebear.com/10.x/lorelei/svg?seed=3" />
<Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
<Tooltip title="Ant User" placement="top">
<Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
</Tooltip>
<Avatar style={{ backgroundColor: '#1677ff' }} icon={<AntDesignOutlined />} />
</Avatar.Group>
<Divider />
<Avatar.Group
size="large"
max={{
count: 2,
style: { color: '#f56a00', backgroundColor: '#fde3cf', cursor: 'pointer' },
popover: { trigger: 'click' },
}}
>
<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
<Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
<Tooltip title="Ant User" placement="top">
<Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
</Tooltip>
<Avatar style={{ backgroundColor: '#1677ff' }} icon={<AntDesignOutlined />} />
</Avatar.Group>
<Divider />
<Avatar.Group shape="square">
<Avatar style={{ backgroundColor: '#fde3cf' }}>A</Avatar>
<Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
<Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
<Avatar style={{ backgroundColor: '#1677ff' }} icon={<AntDesignOutlined />} />
</Avatar.Group>
</>
);
export default App;
maxCount가 오버플로를 포함 (maxCount includes overflow)
Avatar.Group을 HOC로 감싸 overflowInFinal prop을 추가해요. 활성화하면 max.count는 표시할 총 요소 수를 뜻하고, 오버플로 표시를 위해 1개 슬롯을 남겨 둡니다.
import React, { useState } from 'react';
import { toArray } from '@rc-component/util';
import { Avatar, Flex, InputNumber, Switch } from 'antd';
import type { GetProps } from 'antd';
type AvatarGroupProps = GetProps<typeof Avatar.Group>;
const AvatarGroupOverflow: React.FC<AvatarGroupProps & { overflowInFinal?: boolean }> = (props) => {
const { overflowInFinal, ...restProps } = props;
const mergedMaxCount = props.max?.count ?? 3;
const childrenCount = toArray(props.children).length;
if (!overflowInFinal || mergedMaxCount >= childrenCount) {
return <Avatar.Group {...restProps} />;
}
return (
<Avatar.Group
{...restProps}
max={{
...props.max,
count: Math.max(1, mergedMaxCount - 1),
}}
/>
);
};
const App: React.FC = () => {
const [avatarCount, setAvatarCount] = useState(4);
const [overflowInFinal, setOverflowInFinal] = useState(true);
return (
<Flex vertical gap="middle">
<Flex gap={24}>
<span>Avatar count: </span>
<InputNumber
style={{ width: 120 }}
min={2}
max={10}
value={avatarCount}
onChange={(value) => setAvatarCount(value!)}
aria-label="Avatar count"
mode="spinner"
/>
</Flex>
<Flex gap={8}>
<span>overflowInFinal: </span>
<Switch
checked={overflowInFinal}
onChange={setOverflowInFinal}
aria-label="overflowInFinal"
/>
</Flex>
<AvatarGroupOverflow
max={{
count: 3,
style: { backgroundColor: '#52c41a', color: '#fff' },
}}
overflowInFinal={overflowInFinal}
>
{Array.from({ length: avatarCount }, (_, i) => (
<Avatar key={i} style={{ backgroundColor: '#f56a00' }}>
{String.fromCharCode(65 + i)}
</Avatar>
))}
</AvatarGroupOverflow>
</Flex>
);
};
export default App;
반응형 크기 (Responsive Size)
Avatar 크기를 화면 크기에 따라 자동 조절할 수 있어요.
import React from 'react';
import { AntDesignOutlined } from '@ant-design/icons';
import { Avatar } from 'antd';
const App: React.FC = () => (
<Avatar
size={{ xs: 24, sm: 32, md: 40, lg: 64, xl: 80, xxl: 100 }}
icon={<AntDesignOutlined />}
/>
);
export default App;
API
공통 props 참고: Common props
Avatar
| 속성 | 설명 | 타입 | 기본값 | 버전 | 전역 설정 |
|---|---|---|---|---|---|
| alt | 이미지를 설명하는 대체 텍스트 정의 | string | - | × | |
| gap | 문자 타입의 좌우 사이 단위 거리 | number | 4 | 4.3.0 | × |
| icon | 아이콘 아바타용 커스텀 아이콘 타입 | ReactNode | - | × | |
| shape | 아바타의 모양 | circle | square |
circle |
× | |
| size | 아바타의 크기 | number | large | medium | small | { xs: number, sm: number, ...} |
medium |
4.7.0 | × |
| src | 이미지 아바타용 이미지 주소 또는 이미지 요소 | string | ReactNode | - | ReactNode: 4.8.0 | × |
| srcSet | 다른 화면 해상도에 사용할 소스 목록 | string | - | × | |
| draggable | 이미지 드래그 허용 여부 | boolean | 'true' | 'false' |
true | × | |
| crossOrigin | CORS 설정 속성 | 'anonymous' | 'use-credentials' | '' |
- | 4.17.0 | × |
| onError | img 로드 오류 시 핸들러, 기본 폴백 동작을 막으려면 false 반환 | () => boolean | - | × |
팁: 이미지 로드 오류의 폴백으로
icon이나children을 설정할 수 있어요. 우선순위는icon>children이에요.
Avatar.Group 4.5.0+
| 속성 | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| max | 최대 표시 관련 설정 | { count?: number; style?: CSSProperties; popover?: PopoverProps } |
- | 5.18.0 |
폐기됨, max={{ count: number }}를 사용하세요 |
number | - | ||
폐기됨, max={{ popover: PopoverProps }}를 사용하세요 |
top | bottom |
top |
||
폐기됨, max={{ popover: PopoverProps }}를 사용하세요 |
hover | focus | click |
hover |
||
폐기됨, max={{ style: CSSProperties }}를 사용하세요 |
CSSProperties | - | ||
| size | 아바타의 크기 | number | large | medium | small | { xs: number, sm: number, ...} |
medium |
4.8.0 |
| shape | 아바타의 모양 | circle | square |
circle |
5.8.0 |
디자인 토큰 (Design Token)
컴포넌트 토큰 (Avatar)
| 토큰 이름 | 설명 | 타입 | 기본값 |
|---|---|---|---|
| containerSize | Avatar의 크기 | number | 32 |
| containerSizeLG | 큰 Avatar의 크기 | number | 40 |
| containerSizeSM | 작은 Avatar의 크기 | number | 24 |
| groupBorderColor | 그룹 내 아바타의 테두리색 | string | #ffffff |
| groupOverlapping | 그룹 내 아바타의 겹침 | number | -8 |
| groupSpace | 그룹 내 아바타 사이 간격 | number | 4 |
| iconFontSize | Avatar 아이콘의 폰트 크기 | number | 18 |
| iconFontSizeLG | 큰 Avatar 아이콘의 폰트 크기 | string | number | 24 |
| iconFontSizeSM | 작은 Avatar 아이콘의 폰트 크기 | number | 14 |
| textFontSize | Avatar의 폰트 크기 | number | 14 |
| textFontSizeLG | 큰 Avatar의 폰트 크기 | number | 14 |
| textFontSizeSM | 작은 Avatar의 폰트 크기 | number | 14 |
전역 토큰 (Global Token)
| 토큰 이름 | 설명 | 타입 | 기본값 |
|---|---|---|---|
| borderRadius | 기본 컴포넌트의 테두리 반지름 | number | |
| borderRadiusLG | LG 크기 테두리 반지름, Card, Modal 등 큰 반지름 컴포넌트에 사용 | number | |
| borderRadiusSM | SM 크기 테두리 반지름, Button, Input, Select 등 작은 크기 입력 컴포넌트에 사용 | number | |
| colorText | W3C 표준을 준수하는 기본 텍스트 색. 가장 어두운 중성색이기도 함. | string | |
| colorTextLightSolid | 배경색이 있는 텍스트의 하이라이트 색 제어. 예: Primary Button 컴포넌트의 텍스트 | string | |
| colorTextPlaceholder | placeholder 텍스트 색 제어. | string | |
| fontFamily | 시스템 기본 인터페이스 폰트와 화면 표시에 적합한 대체 폰트 라이브러리 세트 제공 | string | |
| fontSize | 디자인 시스템에서 가장 널리 사용되는 폰트 크기. | number | |
| lineHeight | 텍스트의 줄 높이. | number | |
| lineType | 기본 컴포넌트의 테두리 스타일 | string | |
| lineWidth | 기본 컴포넌트의 테두리 너비 | number |
더 알아보기 (Learn more)
- Badge 컴포넌트 — 알림 표시
- Tooltip 컴포넌트 — 호버 설명
- Ant Design 시작하기 — 프로젝트 설정