@dt-dds/react-avatar

中文文档
1.0.0-beta.86发布于 2026/11/13MITTypeScriptESM + CJS近 30 天下载 2405

Avatars can be used to display circular user profile in user menu, tables etc.

原始使用文档
文档目录17 节
  1. Figma Reference
  2. Avatar Usage
  3. Default
  4. With photo
  5. Collapsed
  6. Thumbnail
  7. Sizes
  8. Types with different sizes
  9. API
  10. Avatar
  11. Default Avatar States Styling in IconButton
  12. Avatar Data Attributes
  13. Stack
  14. Commands
  15. Compilation
  16. Versioning
  17. © License

Avatar

Avatars can be used to display circular user profile in user menu, tables etc.

This component is purely presentational and for Interactivity it can be used with IconButton.

Figma Reference

Figma

Avatar Usage

Default

import { Avatar } from '@dt-dds/react';

export const App = () => {
  return <Avatar title='User Name' size='medium' />;
};

With photo

import { Avatar } from '@dt-dds/react';

export const App = () => {
  return <Avatar type='photo' imageSrc='/profile.png' size='medium' />;
};

Collapsed

import { Avatar } from '@dt-dds/react';

export const App = () => {
  return <Avatar type='collapsed' collapsedCount='+1' size='medium' />;
};

Thumbnail

import { Avatar } from '@dt-dds/react';

export const App = () => {
  return <Avatar type='thumbnail' size='medium' />;
};

Sizes

import { Avatar } from '@dt-dds/react';

export const App = () => {
  return (
    <>
      <Avatar title='User Name' size='small' />
      <Avatar title='User Name' size='medium' />
      <Avatar title='User Name' size='large' />
    </>
  );
};

Types with different sizes

import { Avatar } from '@dt-dds/react';

export const App = () => {
  return (
    <>
      <Avatar type='letter' title='John Doe' size='large' />
      <Avatar type='photo' imageSrc='/profile.png' size='small' />
      <Avatar type='thumbnail' size='medium' />
      <Avatar type='collapsed' collapsedCount='+4' size='medium' />
    </>
  );
};

API

Avatar

This component can contain upto two simple characters by passing a string in prop title

PropertyTypeDefaultDescription
titlestring-Letter characters shown in the avatar
typeAvatarType'letter'Selects which visual style the Avatar uses. Accepts:
• 'letter' – shows initials.
• 'collapsed' – shows "+N" for group overflow.
• 'thumbnail' – generic placeholder icon.
• 'photo' – uses imageSrc with automatic fallback to type thumbnail.
sizeAvatarSize'medium'Controls the Avatar dimensions. Accepts:
• 'small' – compact UI.
• 'medium' – default.
• 'large' – prominent/profile use.
imageSrcstring-Optional image path (will only work with "Photo" type)
dataTestIdstringavatarAvatar test identifier
collapsedCountstring'+1'Number displayed inside a collapsed avatar (max 3 characters). Defaults to '+1' when type="collapsed".
customInitialsstring-Custom initials to display (max 2 characters). Replaces title if present.
hasTooltipBooleanfalseShows tooltip when hovering on the Avatar

Default Avatar States Styling in IconButton

When Avatar is composed with IconButton, the interactive states are handled by IconButton's styles. The following table shows the default appearance:

Avatar TypeHover StateDisabled State
Letterprimary.darkprimary.light
Thumbnailprimary.darkprimary.light
Collapsedprimary.lightcontent.contrast
PhotoNo change38% opacity

Customization: Use cssOverrides prop to override the default hover states or apply custom theme colors.

Avatar Data Attributes

When using Avatar inside IconButton, target the following data attributes with cssOverrides:

AttributeAvatar Type
[data-avatar-type='letter']letter
[data-avatar-type='thumbnail']thumbnail
[data-avatar-type='collapsed']collapsed
[data-avatar-type='photo']photo

The IconButton provides a cssOverrides prop that allows you to apply custom CSS styles to the IconButton and its children. This enables full styling flexibility when used with Avatar.

Example : Avatar with Custom Styling

Target Avatar types using [data-avatar-type] attribute selectors to customize their interactive states:

import { IconButton } from '@dt-dds/react-icon-button';
import { Avatar } from '@dt-dds/react-avatar';
import { css } from '@emotion/react';

export const CustomAvatarButton = () => {
  const avatarHoverStyles = css`
    &:hover:not(:disabled) {
      [data-avatar-type='letter'] {
        background-color: #a020f0;
      }
    }
  `;

  return (
    <IconButton ariaLabel='User' cssOverrides={avatarHoverStyles}>
      <Avatar title='John Doe' type='letter' customInitials='JD' />
    </IconButton>
  );
};

Stack

  • TypeScript for static type checking
  • React — JavaScript library for user interfaces
  • Emotion — for writing css styles with JavaScript
  • Storybook — UI component environment powered by Vite
  • Jest - JavaScript Testing Framework
  • React Testing Library - to test UI components in a user-centric way
  • ESLint for code linting
  • Prettier for code formatting
  • Tsup — TypeScript bundler powered by esbuild
  • Yarn from managing packages

Commands

  • yarn build - Build the package
  • yarn dev - Run the package locally
  • yarn lint - Lint all files within this package
  • yarn test - Run all unit tests
  • yarn test:report - Open the test coverage report
  • yarn test:update:snapshot - Run all unit tests and update the snapshot

Compilation

Running yarn build from the root of the package will use tsup to compile the raw TypeScript and React code to plain JavaScript.

The /dist folder contains the compiled output.

avatar
└── dist
    ├── index.d.ts  <-- Types
    ├── index.js    <-- CommonJS version
    └── index.mjs   <-- ES Modules version
    ...

Versioning

Follows semantic versioning

© License

Licensed under MIT License

版本与发布记录

@dt-dds/react-avatar 共发布 37 个版本,首次发布于 2025/08/07;过去 12 个月发布了 26 个版本。

版本发布日期距今
1.0.0-beta.86latest2026/11/13今天
1.0.0-beta.852026/11/049 天前
1.0.0-beta.842026/10/2816 天前
1.0.0-beta.832026/10/2321 天前
1.0.0-beta.822026/10/1925 天前
1.0.0-beta.812026/10/1628 天前

关于 @dt-dds/react-avatar 的常见问题

@dt-dds/react-avatar 是什么?

Avatars can be used to display circular user profile in user menu, tables etc.

如何安装 @dt-dds/react-avatar?

在项目目录运行 npm install @dt-dds/react-avatar,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=17.0.2)、react-dom(>=17.0.2)、@emotion/css(^11.7.1)、@emotion/react(^11.8.2)、@emotion/styled(^11.8.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@dt-dds/react-avatar 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dt-dds/react-avatar 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dt-dds/react-avatar 可以商用吗?

@dt-dds/react-avatar 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dt-dds/react-avatar 的下载量有多少?

根据 npm 官方下载统计,@dt-dds/react-avatar 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,405 次,日均约 80 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dt-dds/react-avatar 还在维护吗?

最新版本 1.0.0-beta.86 发布于 2026/11/13(今天);至今共发布 37 个版本,其中过去 12 个月发布了 26 个;首次发布于 2025/08/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。