Avatar
Displays an Avatar.
Preview
Preview
Code
Installation
Copy and paste the following code into your project.
avatar.tsx
/* 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
Props
Size
You can set the size of the avatar by passing the size
prop. The default size is md
.
Preview
Code
Shape
You can set the shape of the avatar by passing the shape
prop. The default shape is circle
.
Preview
Code
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.
Preview
Code
Status
You can set the status of the avatar by passing the dotPlacement
prop.
You can set it either top-right
or bottom-right
.
Preview
Code
API Reference
Name | Type | Default | Description |
---|---|---|---|
alt | string | — | The alt text for the avatar image. |
src | string | — | The source of the image for the avatar. |
size | string | 'md' | The size of the avatar. Possible values are 'sm', 'md', 'lg'. |
shape | string | 'circle' | The shape of the avatar. Possible values are 'circle', 'square'. |
border | string | — | The border of the avatar. Possible values are 'ring', 'solid'. There is no default border. |
dotPlacement | string | — | The status indicator placement on the avatar. Possible values are 'top-right', 'bottom-right'. |
type | image type | — | The type of the avatar image. |