# Avatar
A round profile picture with an initials fallback.
Source page: https://dinachi.dev/docs/components/avatar

## Installation
```bash
npx @dinachi/cli@latest add avatar
```
Adds to your project:
- `components/ui/avatar/avatar.tsx`
- `components/ui/avatar/index.ts`
Installs:
- `@base-ui/react` ^1.9.0
- `class-variance-authority` ^0.7.1

## Usage
```tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"

export function UserAvatar() {
  return (
    <Avatar>
      <AvatarImage src="/avatars/jane.png" alt="Jane Doe" />
      <AvatarFallback>JD</AvatarFallback>
    </Avatar>
  )
}
```

## Anatomy
```tsx
<Avatar>
  <AvatarImage />
  <AvatarFallback />
</Avatar>
```
- `Avatar`: The round frame.
- `AvatarImage`: The picture. Hidden while loading or failed.
- `AvatarFallback`: Initials or an icon until the image loads.

## Examples

### Default
An image with initials as the fallback.
```tsx
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar';

export function Example() {
  return (
    <Avatar>
      <AvatarImage src="/avatar.png" alt="@raymond-ui" />
      <AvatarFallback>RA</AvatarFallback>
    </Avatar>
  );
}
```

### Sizes
Small, medium and large.
```tsx
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar';

export function Example() {
  return (
    <div className="flex items-center gap-4">
      <Avatar size="sm">
        <AvatarImage src="/avatar.png" />
        <AvatarFallback>S</AvatarFallback>
      </Avatar>
      <Avatar size="md">
        <AvatarImage src="/avatar.png" />
        <AvatarFallback>M</AvatarFallback>
      </Avatar>
      <Avatar size="lg">
        <AvatarImage src="/avatar.png" />
        <AvatarFallback>L</AvatarFallback>
      </Avatar>
    </div>
  );
}
```

### Keep mounted
The image loads in place, with the fallback beneath it.
```tsx
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar';

export function Example() {
  return (
    <div className="flex items-center gap-4">
      <Avatar>
        <AvatarFallback>DN</AvatarFallback>
        <AvatarImage keepMounted src="/avatar.png" alt="@raymond-ui" />
      </Avatar>
      <Avatar>
        <AvatarFallback>JD</AvatarFallback>
        <AvatarImage keepMounted src="https://broken-link.invalid/avatar.png" alt="Broken" />
      </Avatar>
    </div>
  );
}
```

## Notes

### Image optimizers
`AvatarImage` preloads `src` and renders once loaded, which breaks `next/image` and `loading="lazy"`. Add `keepMounted` to render it at once; the fallback shows beneath until it loads, and stays if it fails.

```tsx
import Image from "next/image"

<Avatar>
  <AvatarFallback>JD</AvatarFallback>
  <AvatarImage
    keepMounted
    render={<Image src="/avatars/jane.png" width={40} height={40} alt="Jane Doe" />}
  />
</Avatar>
```

## API reference

### Avatar
The round frame.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"sm" \| "md" \| "lg"` | `"md"` | 32, 40 or 48px square. |
| `className` | `string` | - | Merged over the size; your own width and height win. |

### AvatarImage
The picture. Hidden while loading or failed.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `src` | `string` | - | The image to load. |
| `alt` | `string` | - | Names the person for screen readers. |
| `keepMounted` | `boolean` | `false` | Renders the image before it loads. Needed for next/image and lazy loading. |
| `onLoadingStatusChange` | `(status: "idle" \| "loading" \| "loaded" \| "error") => void` | - | Runs as the image loads or fails. |
| `render` | `ReactElement` | - | Swap the element, for example for next/image. |

### AvatarFallback
Initials or an icon until the image loads.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `delay` | `number` | `0` | Milliseconds before showing, so a fast image skips the fallback. |

## Accessibility
- Give `AvatarImage` an alt naming the person, or an empty alt when the name is beside it.
- Fallback initials are read as letters. Prefer a visible name beside the avatar.

## Source
`components/ui/avatar/avatar.tsx`
```tsx
import * as React from "react";
import { Avatar as BaseAvatar } from "@base-ui/react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";

const avatarVariants = cva(
  "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full",
  {
    variants: {
      size: {
        sm: "h-8 w-8",
        md: "h-10 w-10",
        lg: "h-12 w-12",
      },
    },
    defaultVariants: {
      size: "md",
    },
  }
);

const Avatar = React.forwardRef<
  React.ComponentRef<typeof BaseAvatar.Root>,
  React.ComponentPropsWithoutRef<typeof BaseAvatar.Root> &
    VariantProps<typeof avatarVariants>
>(({ className, size, ...props }, ref) => (
  <BaseAvatar.Root
    data-slot="avatar"
    ref={ref}
    className={cn(avatarVariants({ size }), className)}
    {...props}
  />
));
Avatar.displayName = "Avatar";

const AvatarImage = React.forwardRef<
  React.ComponentRef<typeof BaseAvatar.Image>,
  React.ComponentPropsWithoutRef<typeof BaseAvatar.Image>
>(({ className, ...props }, ref) => (
  <BaseAvatar.Image
    data-slot="avatar-image"
    ref={ref}
    className={cn(
      "absolute inset-0 z-[1] aspect-square h-full w-full object-cover data-[error]:invisible data-[loading]:invisible",
      className
    )}
    {...props}
  />
));
AvatarImage.displayName = "AvatarImage";

const AvatarFallback = React.forwardRef<
  React.ComponentRef<typeof BaseAvatar.Fallback>,
  React.ComponentPropsWithoutRef<typeof BaseAvatar.Fallback>
>(({ className, ...props }, ref) => (
  <BaseAvatar.Fallback
    data-slot="avatar-fallback"
    ref={ref}
    className={cn(
      "absolute inset-0 flex h-full w-full items-center justify-center rounded-full bg-muted",
      className
    )}
    {...props}
  />
));
AvatarFallback.displayName = "AvatarFallback";

export { Avatar, AvatarImage, AvatarFallback };
```
