1. docs
  2. components
  3. avatar

Avatar

Displays an Avatar.


Preview

Sonja Balmann

Installation

Copy and paste the following code into your project.

/* eslint-disable @next/next/no-img-element */
import { cn } from "@/lib/utils";
import { DetailedHTMLProps, ImgHTMLAttributes } from "react";
import { tv, VariantProps } from "tailwind-variants";

const avatarVariants = tv({
  slots: {
    dot: "absolute min-w-[12px] min-h-[12px] rounded-full py-1 px-1 text-xs font-medium content-[''] leading-none grid place-items-center bg-green-500 text-white border-2 border-background",
    img: "inline-block object-cover object-center",
  },
  variants: {
    size: {
      sm: { img: "w-8 h-8" },
      md: { img: "w-12 h-12" },
      lg: { img: "w-16 h-16" },
    },
    shape: {
      square: { img: "rounded-md" },
      circle: { img: "rounded-full" },
    },
    border: {
      ring: { img: "border border-2 p-0.5" },
      solid: { img: "border border-2" },
    },
    dotPlacement: {
      none: { dot: "hidden" },
      "top-right": { dot: "top-0 right-0" },
      "bottom-right": { dot: "bottom-0 right-0" },
    },
  },
  compoundVariants: [
    {
      dotPlacement: "bottom-right",
      shape: "square",
      class: { dot: "translate-x-2/4 translate-y-2/4" },
    },
    {
      dotPlacement: "top-right",
      shape: "square",
      class: { dot: "translate-x-2/4 -translate-y-2/4" },
    },
  ],
  defaultVariants: {
    size: "md",
    shape: "circle",
    dotPlacement: "none",
  },
});

type AvatarProps = DetailedHTMLProps<
  ImgHTMLAttributes<HTMLImageElement>,
  HTMLImageElement
> &
  VariantProps<typeof avatarVariants>;

function Avatar({
  border,
  size,
  shape,
  className,
  dotPlacement,
  alt,
  ...props
}: AvatarProps) {
  const { dot, img } = avatarVariants();
  return (
    <div className="relative inline-flex">
      <img
        className={cn(img({ size, shape, border }), className)}
        alt={alt}
        {...props}
      />
      <span className={cn(dot({ dotPlacement }))} />
    </div>
  );
}

export { Avatar };

Update the import paths to match your project setup.

Usage

import { Avatar } from "@/ui/avatar";
<Avatar name="John Doe" src="src" />

Props

Size

You can set the size of the avatar by passing the size prop. The default size is md.

Sonja Balmann
Sonja Balmann
Sonja Balmann

Shape

You can set the shape of the avatar by passing the shape prop. The default shape is circle.

Sonja Balmann
Sonja Balmann

Border

You can set the border of the avatar by passing the border prop. You can set it either ring or solid.

There is no default border.

ring border gives you a ring around the avatar, like an instagram story.

Sonja Balmann
Sonja Balmann

Status

You can set the status of the avatar by passing the dotPlacement prop. You can set it either top-right or bottom-right.

Sonja Balmann
Sonja Balmann
Sonja Balmann
Sonja Balmann

API Reference

NameTypeDefaultDescription
altstring—The alt text for the avatar image.
srcstring—The source of the image for the avatar.
sizestring'md'The size of the avatar. Possible values are 'sm', 'md', 'lg'.
shapestring'circle'The shape of the avatar. Possible values are 'circle', 'square'.
borderstring—The border of the avatar. Possible values are 'ring', 'solid'. There is no default border.
dotPlacementstring—The status indicator placement on the avatar. Possible values are 'top-right', 'bottom-right'.
typeimage type—The type of the avatar image.