# Object Image (https://s3.dimah.dev/docs/ui/components/object-image)

Show an image from a private bucket. `ObjectImage` signs an inline URL with
[`useObjectUrl`](https://s3.dimah.dev/docs/react/hooks/object-url), refreshes it before it
expires, and shares it with other images of the same object.

```tsx title="object-image-demo.tsx"
"use client";

import { useState } from "react";
import { ImageIcon } from "lucide-react";
import { useUpload } from "@dimah-s3/react";
import { ObjectImage, Upload, UploadToasts, UploadTrigger } from "@dimah-s3/ui";
import { rememberDemoFile } from "@/lib/demo/client-object-store";

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

  return (
    <div className="flex flex-col items-center gap-4">
      <ObjectImage
        route="avatar"
        objectKey={imageKey}
        alt="Uploaded image"
        className="size-40 rounded-xl border object-cover"
        fallback={
          <div className="flex size-40 items-center justify-center rounded-xl border border-dashed text-fd-muted-foreground">
            <ImageIcon className="size-6" aria-hidden />
          </div>
        }
      />
      <Upload upload={upload}>
        <UploadTrigger size="sm">Choose an image</UploadTrigger>
      </Upload>
      <UploadToasts upload={upload} />
    </div>
  );
}
```

## Installation [#installation]

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

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

Adds `@/components/dimah-s3/preview/object-image`.
</Tab>
</Tabs>

## Usage [#usage]

```tsx
"use client";

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

export function CoverImage({ coverKey }: { coverKey: string | null }) {
  return (
    <ObjectImage
      route="covers"
      objectKey={coverKey}
      alt="Album cover"
      className="size-40 rounded-md object-cover"
      fallback={<div className="bg-muted size-40 rounded-md" />}
    />
  );
}
```

`route` must allow downloads. Its server
[download hooks](https://s3.dimah.dev/docs/server/hooks/download) decide who may read the object,
as for any download.

## Fallback [#fallback]

`fallback` shows while there is no key or URL yet, and when signing or loading
the image fails.

## Accessibility [#accessibility]

`alt` is required. Pass `alt=""` when the image is decorative, such as next to
a name that already describes it.

## API Reference [#api-reference]

Also accepts every `img` prop except `src`.

| Prop        | Type             | Default  | Description                                           |
| ----------- | ---------------- | -------- | ----------------------------------------------------- |
| `route`     | `string`         | required | Route that signs the URL                              |
| `objectKey` | `string \| null` | required | Key of the stored object. `null` shows `fallback`     |
| `alt`       | `string`         | required | Alternative text. `""` for a decorative image         |
| `fallback`  | `ReactNode`      | —        | Shown while there is no URL, and when the image fails |
| `api`       | `S3Api`          | —        | Signs the URL. Optional inside `S3Provider`           |