Avatar

Default rounded avatar that can show your users profile picture. ✨

AH


Installation

1. Install dependencies:

npm install @radix-ui/react-avatar

2. Copy the code 👇 into your project:

import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";
 
import { cn } from "@/lib/utils";
 
const Avatar = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Root
    ref={ref}
    className={cn(
      "relative flex h-14 w-14 border-2 border-black rounded-full overflow-hidden",
      className
    )}
    {...props}
  />
));
Avatar.displayName = "Avatar";
 
const AvatarImage = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Image>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Image
    ref={ref}
    className={cn("aspect-square h-full w-full", className)}
    {...props}
  />
));
AvatarImage.displayName = "Avatar.Image";
 
const AvatarFallback = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Fallback>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Fallback
    ref={ref}
    className={cn(
      "flex h-full w-full items-center justify-center rounded-full bg-muted bg-primary-400",
      className
    )}
    {...props}
  />
));
AvatarFallback.displayName = "Avatar.Fallback";
 
const AvatarComponent = Object.assign(Avatar, {
  Image: AvatarImage,
  Fallback: AvatarFallback,
});
 
export { AvatarComponent as Avatar };


Examples

Size variants

AHAHAH


Fallbacks

Fallbacks are helpfull when there ia an error loading the src of the avatar. You can set fallbacks with Avatar.Fallback component.


AH

Last Updated: 12 Oct, 2024