Skip to Content
dimah-s3v2.8.1

Avatar Upload

Image button that shows the current picture and uploads a new one, round or square.

A profile picture or logo that the user can replace. AvatarUpload shows the stored image; a click or a drop uploads a new one, with a progress ring over the preview.

Click or drop an image.

Installation

import { AvatarUpload } from "@dimah-s3/ui";

Complete Installation first: the package or registry setup, the stylesheet, and one <Toaster /> in your layout.

Usage

"use client";

import { useUpload } from "@dimah-s3/react";
import { AvatarUpload, UploadToasts } from "@dimah-s3/ui";

export function ProfilePhoto({
  avatarKey,
  saveAvatar,
}: {
  avatarKey: string | null;
  saveAvatar: (key: string) => void;
}) {
  const upload = useUpload({ route: "avatar", accept: ["image/*"] });

  return (
    <>
      <AvatarUpload
        upload={upload}
        route="avatar"
        objectKey={avatarKey}
        label="Change profile photo"
        onUploaded={(result) => saveAvatar(result.key)}
      />
      <UploadToasts upload={upload} />
    </>
  );
}

Save result.key in onUploaded, and pass it back as objectKey. route signs the image URL, so it needs download turned on.

Examples

Sizes and shapes

size sets the width and height in pixels. shape="square" rounds the corners instead of the whole image, for logos and covers.

64 px
96 px
Square, for logos

Accessibility

  • label is required: it is the button's accessible name.
  • A polite live region announces the upload's progress and errors.

API Reference

PropTypeDefaultDescription
uploadUseUploadReturnrequiredThe useUpload return that uploads the picked image
routestringrequiredRoute that signs the image URL for objectKey
labelstringrequiredAccessible name of the button, such as "Change profile photo"
objectKeystring | null—Key of the current image. Without one, a placeholder shows
onUploaded(result: UploadResult) => void—Runs once for each image that uploads
sizenumber96Width and height in pixels
shape"circle" | "square""circle"Round image, or rounded corners for logos and covers
apiS3Api—Signs the image URL. Optional inside S3Provider
classNamestring—Classes for the button

On this page