@atlaskit/media-avatar-picker

中文文档
28.3.5发布于 2026/10/01Apache-2.0TypeScriptESM + CJS近 30 天下载 8042

A component to select, drag and resize image avatars. It also provides a default list of predefined avatars.

原始使用文档
文档目录13 节
  1. Installation
  2. Using the component
  3. Local Image Upload and Cropping
  4. Pre-defined Avatars
  5. Client-side only
  6. Limitations
  7. Error Handling
  8. Accessibility
  9. Keyboard Navigation
  10. Screen Reader Support
  11. Features
  12. API
  13. Usage Example

@atlaskit/media-avatar-picker

Provides a component to select, drag and resize image avatars. It also provides a default list of predefined avatars.

Installation

yarn add @atlaskit/media-avatar-picker

Using the component

Local Image Upload and Cropping

The AvatarPickerDialog allows the user to upload or drop a local image, then pan and zoom to a desired cropped view.

The default zoom level should fit the image within the crop area. Images smaller than the crop area are scaled up.

The component constrains the panning and scaling of the image to ensure that only valid regions are selectable by the user.

Pre-defined Avatars

The component also allows the user to select from pre-defined avatar images. These can be provided to the component via it's avatars property.

Pre-defined avatars are hidden when an image is chosen by the user.

Client-side only

The component is currently designed to pass back a selected image or avatar. It does not use any specific APIs for storage or upload, that is currently the responsibility of the consumer. In other words you will need a solution to store the selected image or avatar.

Limitations

The component currently only accepts image/gif, image/jpeg, and image/png mime types. SVG is not currently not supported.

The component only accepts images up to 10Mib.

Error Handling

The component will handle the following errors on the client:

  • Bad format - if the user uploads an image with a bad format, or drops an invalid mime type, an error will be shown
  • Image size > 10Mib - if the user uploads or drops an image greater than 10Mib in size, an error message will be shown

See the errorMessage property below to set your own custom error message if required.

Accessibility

This component is designed to be fully accessible and follows WCAG guidelines:

Keyboard Navigation

  • Image Cropping: Use arrow keys to move and position the image within the crop area
  • Tab Navigation: All interactive elements are accessible via keyboard
  • Focus Management: Clear visual focus indicators throughout the interface

Screen Reader Support

  • ARIA Labels: Descriptive labels for all interactive elements
  • Live Announcements: Real-time feedback when moving images during cropping
  • Semantic Markup: Proper roles and structure for assistive technologies

Features

  • Fully keyboard accessible image cropping and navigation
  • Screen reader announcements for image positioning
  • High contrast focus indicators
  • Logical tab order throughout the interface

API

onImagePicked?: (file: File, crop: CropProperties) => void

This property is raised when the user clicks the Save button and there is a selected image. Two arguments are passed, the file:File which is a blob, and the crop settings which is an object containing x:number,y:number, and size:number values, which are all relative to the coordinates of the selected image.

Note Due to limitations on Safari <= 10.0 and IE11, a Blob object will be returned instead of a File. This still allows access to the image byte data to facilitate uploads, essentially minus the filename and date attributes.

The x and y represent the origin of the crop area. The size value is a single value which represents the width and height of the crop area. To get the crop area from the selected image, simply take the clipped rect of (x, y, size, size) from the given image.

onImagePickedDataURI?: (dataURI: string) => void

This property is raised when the user clicks the Save button and there is a selected image. The selected image is provided as a dataURI string.

onAvatarPicked: (avatar: Avatar) => void

This property is raised when the user clicks the Save button and there is a pre-defined avatar selected, and no image selected. An Avatar object with a dataURI property is passed.

onCancel: Function

This property is raised when the user clicks Cancel button.

Note this does not close the dialog. It is up to the consumer to re-render and remove the dialog from the UI.

imageSource?: string

(optional) This property is used to set the selected image so that the component opens up with it visible already.

The value should be a valid dataURI string. If an invalid dataURI is given, the bad format error state will be triggered and a message shown.

avatars: Array<Avatar>

This property is used to provide an array of pre-defined avatars. The Avatar object is a simple type with a single dataURI: string property. For convenience, this type is exported from the @atlassian/media-avatar-picker module along with the AvatarPickerDialog component.

defaultSelectedAvatar?: Avatar

(optional) This property is used along with the avatar property. It allows you to set the currently selected pre-defined avatar. By default, there is no pre-defined avatar selected, even if the avatars property is set.

title?: string

(optional) The title text for the dialog. The default is Upload an avatar.

primaryButtonText?: string

(optional) The primary button text. The default is Save.

errorMessage?: string

(optional) This property allows the consumer to display an error message. This may occur from a call to a service. The string is clipped if greater than 125 charaters (approximately 3 lines within the dialog).

Usage Example

Below is an example of rendering an `AvatarPickerDialog`. The dialog should be wrapped in a `ModalTransition` component so it fades out when closed.

import { AvatarPickerDialog, Avatar } from '@atlaskit/media-avatar-picker';
import { ModalTransition } from '@atlaskit/modal-dialog';

const avatars: Array<Avatar> = [{ dataURI: 'some-data-uri' }];

const App = ({ isOpen }) => (
  <ModalTransition>
    {isOpen && (
      <AvatarPickerDialog
        avatars={avatars}
        onImagePicked={(selectedImage, crop) => {
          console.log(selectedImage.size, crop.x, crop.y, crop.size);
        }}
        onAvatarPicked={selectedAvatar =>
          console.log(selectedAvatar.dataURI)
        }
        onCancel={() => /* we need to close the dialog... */}
      />
    )}
  </ModalTransition>
);

版本与发布记录

@atlaskit/media-avatar-picker 共发布 355 个版本,首次发布于 2017/04/19;过去 12 个月发布了 50 个版本。

版本发布日期距今
28.3.5latest2026/10/011 个月前
28.3.42026/09/241 个月前
28.3.32026/09/231 个月前
28.3.22026/09/171 个月前
28.3.12026/09/151 个月前
28.3.02026/08/302 个月前

其他发布通道:hotfix → 16.0.0-HOT-87089.4,next → 24.3.3。

关于 @atlaskit/media-avatar-picker 的常见问题

@atlaskit/media-avatar-picker 是什么?

A component to select, drag and resize image avatars. It also provides a default list of predefined avatars.

如何安装 @atlaskit/media-avatar-picker?

在项目目录运行 npm install @atlaskit/media-avatar-picker,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.2.0)、react-intl(^5.25.1 || ^6.0.0 || ^7.0.0)、@emotion/react(^11.7.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atlaskit/media-avatar-picker 支持 TypeScript 吗?

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

@atlaskit/media-avatar-picker 支持 ESM 还是 CommonJS?

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

@atlaskit/media-avatar-picker 可以商用吗?

@atlaskit/media-avatar-picker 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@atlaskit/media-avatar-picker 的下载量有多少?

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

@atlaskit/media-avatar-picker 还在维护吗?

最新版本 28.3.5 发布于 2026/10/01(1 个月前);至今共发布 355 个版本,其中过去 12 个月发布了 50 个;首次发布于 2017/04/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。