# Custom UI (https://s3.dimah.dev/docs/ui/custom-ui)

When the components do not fit your design, build on the same hooks. Pick the
level that matches how much you want to change.

| You want to                              | Use                                                                       |
| ---------------------------------------- | ------------------------------------------------------------------------- |
| Restyle a part                           | `className`, `variant`, `children`, and [theme tokens](https://s3.dimah.dev/docs/ui/theming)  |
| Add a control next to the built-in parts | A part of your own with `useUploadContext()` and friends                  |
| Change a component's markup              | The [shadcn registry](https://s3.dimah.dev/docs/ui/installation), then edit the copied source |
| Design everything yourself               | The [`@dimah-s3/react`](https://s3.dimah.dev/docs/react) hooks, without `@dimah-s3/ui`        |

## A part of your own [#a-part-of-your-own]

Inside a root, a context hook returns the root's hook. Your part sits next to
the built-in ones and reads the same state.

```tsx
"use client";

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

export function UploadCount() {
  const { upload } = useUploadContext();
  const done = upload.files.filter((file) => file.phase === "success").length;

  if (upload.files.length === 0) return null;
  return (
    <p className="text-muted-foreground text-sm">
      {done} of {upload.files.length} uploaded
    </p>
  );
}
```

```tsx
<Upload upload={upload}>
  <UploadTrigger />
  <UploadCount />
</Upload>
```

| Root          | Context hook              | Returns                                                   |
| ------------- | ------------------------- | --------------------------------------------------------- |
| `Upload`      | `useUploadContext()`      | `upload`, `isFull`, `hideUploaded`                        |
| `UploadField` | `useUploadFieldContext()` | `value`, `items`, `remaining`, `remove`                   |
| `Download`    | `useDownloadContext()`    | `download`                                                |
| `Delete`      | `useDeleteContext()`      | `delete`, `confirmation`, `request`, `confirm`, `dismiss` |
| `ObjectList`  | `useObjectListContext()`  | `list`, `selection`, the hooks, `view`, commands          |

Use [`FileAttachment`](https://s3.dimah.dev/docs/ui/components/attachment) and its building blocks
to match the look of the built-in cards.

## Your own toasts [#your-own-toasts]

To report a hook through another toast library, replace `UploadToasts`,
`DownloadToasts`, or `DeleteToasts` with a component that calls
`useToastOwner`. It returns `true` for one owner per hook, and the
`ObjectList` built-in toasts step aside while it is mounted.

```tsx
"use client";

import { useEffect } from "react";
import { toast } from "sonner";
import { useFormatError, type UseUploadReturn } from "@dimah-s3/react";
import { useToastOwner } from "@dimah-s3/ui";

export function SonnerUploadToasts({ upload }: { upload: UseUploadReturn }) {
  const active = useToastOwner(upload.instanceId);
  const formatError = useFormatError();

  useEffect(() => {
    if (active && upload.error) toast.error(formatError(upload.error));
  }, [active, upload.error, formatError]);

  return null;
}
```

Pass `{ weak: true }` for an owner that should step aside for another one.

## Fully headless [#fully-headless]

The hooks hold every state the components show. This picker uses
`useUpload` and shadcn [Attachment](https://ui.shadcn.com/docs/components/attachment)
directly:

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

import { useUpload, type UploadFileState } from "@dimah-s3/react";
import { FileIcon, LoaderIcon } from "lucide-react";
import {
  Attachment,
  AttachmentContent,
  AttachmentDescription,
  AttachmentMedia,
  AttachmentTitle,
} from "@/components/ui/attachment";
import { Button } from "@/components/ui/button";

function attachmentState(file: UploadFileState) {
  if (file.phase === "uploading") return "uploading" as const;
  if (file.phase === "error") return "error" as const;
  if (file.phase === "success") return "done" as const;
  return "processing" as const;
}

function describe(file: UploadFileState) {
  if (file.phase === "error") return file.error?.message ?? "Upload failed";
  if (file.phase === "success") return "Uploaded";
  if (file.phase === "uploading") return `${file.progress.percent}%`;
  return "Preparing…";
}

/** Headless upload + stock shadcn Attachment from the docs app. */
export function CustomUploadDemo() {
  const upload = useUpload({
    route: "avatar",
    maxFileSize: 75 * 1024 * 1024,
  });
  const { file } = upload;

  return (
    <div className="flex w-full max-w-sm flex-col gap-2">
      <input {...upload.getInputProps()} />
      <Button type="button" size="sm" onClick={() => upload.open()}>
        Choose file
      </Button>

      {file ? (
        <Attachment state={attachmentState(file)} size="sm">
          <AttachmentMedia>
            {upload.isPending ? (
              <LoaderIcon className="animate-spin" />
            ) : (
              <FileIcon />
            )}
          </AttachmentMedia>
          <AttachmentContent>
            <AttachmentTitle>{file.fileName}</AttachmentTitle>
            <AttachmentDescription>{describe(file)}</AttachmentDescription>
          </AttachmentContent>
        </Attachment>
      ) : null}
    </div>
  );
}
```

The core of it:

```tsx
"use client";

import { useUpload } from "@dimah-s3/react";
import { Button } from "@/components/ui/button";

export function CustomFilePicker() {
  const upload = useUpload({ route: "avatar" });

  return (
    <div className="flex items-center gap-4">
      <input {...upload.getInputProps()} />
      <Button type="button" onClick={() => upload.open()}>
        {upload.isPending
          ? `Uploading ${upload.progress.percent}%`
          : "Choose file"}
      </Button>
    </div>
  );
}
```

`upload.progress` holds the bytes sent across the queue, and each entry of
`upload.files` has its own `phase`, `progress`, and `error`. See
[useUpload](https://s3.dimah.dev/docs/react/hooks/upload), [useDownload](https://s3.dimah.dev/docs/react/hooks/download),
and [useDelete](https://s3.dimah.dev/docs/react/hooks/delete) for everything the hooks return.