Display

Avatar.

A round player portrait with an image and mono initials as the fallback. Group several into an overlapping stack.

SCVC

Installation#

With the CLI

$npx lanterncn add avatar

This also installs the Lantern theme and any Lantern UI components it depends on. The shadcn CLI works directly too: npx shadcn@latest add httptim/lantern-ui/avatar. See Installation if your project is not set up yet.

Manually

  1. Install the dependencies

    npm install radix-ui
  2. Copy the source into your project

    components/ui/avatar.tsx
    "use client";
    
    import * as React from "react";
    import { Avatar as AvatarPrimitive } from "radix-ui";
    
    import { cn } from "@/lib/utils";
    
    function Avatar({ className, ...props }: React.ComponentProps<typeof AvatarPrimitive.Root>) {
      return (
        <AvatarPrimitive.Root
          data-slot="avatar"
          className={cn("relative flex size-9 shrink-0 overflow-hidden rounded-full border border-input bg-secondary", className)}
          {...props}
        />
      );
    }
    
    function AvatarImage({ className, ...props }: React.ComponentProps<typeof AvatarPrimitive.Image>) {
      return <AvatarPrimitive.Image data-slot="avatar-image" className={cn("aspect-square size-full object-cover", className)} {...props} />;
    }
    
    function AvatarFallback({ className, ...props }: React.ComponentProps<typeof AvatarPrimitive.Fallback>) {
      return (
        <AvatarPrimitive.Fallback
          data-slot="avatar-fallback"
          className={cn(
            "flex size-full items-center justify-center bg-accent font-mono text-[11px] font-semibold tracking-wider text-[#b7ca9e] uppercase",
            className,
          )}
          {...props}
        />
      );
    }
    
    /** Overlapping row of avatars. Each avatar gets a ring in the background color. */
    function AvatarGroup({ className, ...props }: React.ComponentProps<"div">) {
      return (
        <div
          data-slot="avatar-group"
          className={cn("flex -space-x-2.5 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background", className)}
          {...props}
        />
      );
    }
    
    export { Avatar, AvatarImage, AvatarFallback, AvatarGroup };
  3. Update the import paths to match your project

    The source imports cn from @/lib/utils.

Usage#

import { Avatar, AvatarImage, AvatarFallback, AvatarGroup } from "@/components/ui/avatar";
<Avatar>
  <AvatarImage src="/player.png" alt="Player" />
  <AvatarFallback>PL</AvatarFallback>
</Avatar>

Examples#

Fallback initials

T7OPHB

Stacked group

SCMKJD+4
7 signed the guestbook