# Avatar Upload (https://s3.dimah.dev/docs/ui/components/avatar-upload)

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.

```tsx title="avatar-upload-demo.tsx"
"use client";

import { useState } from "react";
import { useUpload } from "@dimah-s3/react";
import { AvatarUpload, UploadToasts } from "@dimah-s3/ui";
import { rememberDemoFile } from "@/lib/demo/client-object-store";

export function AvatarUploadDemo() {
  const [avatarKey, setAvatarKey] = useState<string | null>(null);
  const upload = useUpload({
    route: "avatar",
    accept: ["image/*"],
    maxFileSize: 5 * 1024 * 1024,
    // Demo only: keep the bytes in the browser so the image can be shown.
    onSuccess: (result, file) => rememberDemoFile(result.key, file),
  });

  return (
    <div className="flex flex-col items-center gap-3">
      <AvatarUpload
        upload={upload}
        route="avatar"
        objectKey={avatarKey}
        label="Change profile photo"
        onUploaded={(result) => setAvatarKey(result.key)}
      />
      <p className="text-sm text-fd-muted-foreground">
        Click or drop an image.
      </p>
      <UploadToasts upload={upload} />
    </div>
  );
}
```

## Installation [#installation]

<Tabs items={["npm","shadcn"]}>
<Tab value="npm">
```tsx
import { AvatarUpload } from "@dimah-s3/ui";
```
</Tab>

<Tab value="shadcn">
```npm
npx shadcn@latest add @dimah-s3/avatar-upload
```

Adds `@/components/dimah-s3/upload/avatar-upload` and the files it uses.
</Tab>
</Tabs>

Complete [Installation](https://s3.dimah.dev/docs/ui/installation) first: the package or registry
setup, the stylesheet, and one `<Toaster />` in your layout.

## Usage [#usage]

```tsx
"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 [#examples]

### Sizes and shapes [#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.

```tsx title="avatar-upload-shapes-demo.tsx"
"use client";

import { useState } from "react";
import { useUpload } from "@dimah-s3/react";
import { AvatarUpload, UploadToasts } from "@dimah-s3/ui";
import { rememberDemoFile } from "@/lib/demo/client-object-store";

export function AvatarUploadShapesDemo() {
  return (
    <div className="flex flex-wrap items-end justify-center gap-8">
      <Example label="64 px" size={64} />
      <Example label="96 px" size={96} />
      <Example label="Square, for logos" size={96} shape="square" />
    </div>
  );
}

function Example({
  label,
  size,
  shape,
}: {
  label: string;
  size: number;
  shape?: "circle" | "square";
}) {
  const [imageKey, setImageKey] = useState<string | null>(null);
  const upload = useUpload({
    route: "avatar",
    accept: ["image/*"],
    maxFileSize: 5 * 1024 * 1024,
    // Demo only: keep the bytes in the browser so the image can be shown.
    onSuccess: (result, file) => rememberDemoFile(result.key, file),
  });

  return (
    <div className="flex flex-col items-center gap-2">
      <AvatarUpload
        upload={upload}
        route="avatar"
        objectKey={imageKey}
        label={`Change image (${label})`}
        size={size}
        shape={shape}
        onUploaded={(result) => setImageKey(result.key)}
      />
      <span className="text-xs text-muted-foreground">{label}</span>
      <UploadToasts upload={upload} />
    </div>
  );
}
```

## Accessibility [#accessibility]

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

## API Reference [#api-reference]

| Prop         | Type                             | Default    | Description                                                   |
| ------------ | -------------------------------- | ---------- | ------------------------------------------------------------- |
| `upload`     | `UseUploadReturn`                | required   | The `useUpload` return that uploads the picked image          |
| `route`      | `string`                         | required   | Route that signs the image URL for `objectKey`                |
| `label`      | `string`                         | required   | Accessible name of the button, such as "Change profile photo" |
| `objectKey`  | `string \| null`                 | —          | Key of the current image. Without one, a placeholder shows    |
| `onUploaded` | `(result: UploadResult) => void` | —          | Runs once for each image that uploads                         |
| `size`       | `number`                         | `96`       | Width and height in pixels                                    |
| `shape`      | `"circle" \| "square"`           | `"circle"` | Round image, or rounded corners for logos and covers          |
| `api`        | `S3Api`                          | —          | Signs the image URL. Optional inside `S3Provider`             |
| `className`  | `string`                         | —          | Classes for the button                                        |