Avatar

The Avatar component is used to display user profile images or icons, often in a circular or rounded format. It can be customized in size and shape to fit various design needs.

Avatar Component Documentation

A customizable avatar component supporting images, fallback text, icons, and various styles.

Complete Installation Guide

Comprehensive step-by-step instructions to install and use the Avatarcomponent in your React/Next.js project with TypeScript and JavaScript support.

Step 1: Install Component

Description: Install the component using our CLI tool

Type: cli

TypeScript Installation Command:

npx screenui add avatar --lang ts --path src/components

JavaScript Installation Command:

npx screenui add avatar --lang js --path src/components

Step 2: Usage Example

Description: Copy and use the component in your project

Type: usage

TypeScript Implementation

Complete TypeScript code examples for the Avatar component with full type safety, props interface, and implementation details.

File: Avatar.tsx

Language: tsx

File Type: TypeScript React Component


import { Avatar } from "@/components/Avatar";
import { Star } from "lucide-react";

export function Example() {
  return (
    <Avatar
      as="button"
      src="/user.png"
      clickable
      className="bg-gray-200 hover:ring-4 hover:ring-blue-200"
      onClick={() => console.log("Avatar clicked")}
    />

    <Avatar
      src="/user.png"
      fallback="John Doe"
      className="bg-blue-500 text-white ring-2 ring-blue-300"
    />

    <Avatar
      fallbackContent={<Star className="w-6 h-6" />}
      className="bg-yellow-100 text-yellow-600"
    />

    <Avatar
      variant="square"
      size="xl"
      src="/user.png"
      className="bg-gray-100 border-2 border-gray-300"
    />
  );
}

JavaScript Implementation

Complete JavaScript code examples for the Avatar component with ES6+ syntax, modern React patterns, and compatibility notes.

File: Avatar.jsx

Language: jsx

File Type: JavaScript React Component


import { Avatar } from "@/components/Avatar";
import { Star } from "lucide-react";

export function Example() {
  return (
    <Avatar
      as="button"
      src="/user.png"
      clickable
      className="bg-gray-200 hover:ring-4 hover:ring-blue-200"
      onClick={() => console.log("Avatar clicked")}
    />

    <Avatar
      src="/user.png"
      fallback="John Doe"
      className="bg-blue-500 text-white ring-2 ring-blue-300"
    />

    <Avatar
      fallbackContent={<Star className="w-6 h-6" />}
      className="bg-yellow-100 text-yellow-600"
    />

    <Avatar
      variant="square"
      size="xl"
      src="/user.png"
      className="bg-gray-100 border-2 border-gray-300"
    />
  );
}

Required Dependencies and Prerequisites

The following packages and tools are required to use this component. Ensure all dependencies are installed and properly configured.

  • react - Core React library for component functionality
  • tailwindcss - Utility-first CSS framework for styling
  • lucide-react - Icon library for visual elements

Usage Examples and Best Practices

Practical examples and recommended patterns for implementing and customizing the Avatar component in your React/Next.js application.

Basic Usage

Import and use the Avatar component in your React components. The component supports both TypeScript and JavaScript projects.

Advanced Configuration

Customize the component with props, styling, event handlers, and advanced patterns as shown in the code examples above.

Integration Notes

This component is designed to work seamlessly with Next.js, Tailwind CSS, and modern React patterns. It supports server-side rendering, static generation, and client-side hydration.

Accessibility Features

The component includes built-in accessibility features such as ARIA labels, keyboard navigation, screen reader support, and focus management.

Performance Considerations

Optimized for performance with React.memo, efficient re-rendering, and minimal bundle impact. Supports code splitting and lazy loading.

Frequently Asked Questions

How do I customize the styling?

Use Tailwind CSS classes via the className prop, or apply custom CSS using CSS modules or styled-components.

Is this component accessible?

Yes, the component follows WCAG guidelines and includes proper ARIA attributes, keyboard navigation, and screen reader support.

Can I use this with other frameworks?

This component is designed for React/Next.js, but the patterns can be adapted for other frameworks like Vue.js or Angular.

Avatar Component

UI Componentsv1.0.0

A customizable avatar component supporting images, fallback text, icons, and various styles.

Last updated: 2025-09-20

screenui
Avatar
Avatar Examples

Usage Notes

Shape Variants

  • circular : Perfectly round avatar. Most common usage for profile pictures.
  • rounded : Avatar with slightly rounded corners for a softer look.
  • square : Hard-cornered avatar for a sharp and modern style.

Sizes

  • xs : Tiny avatar for compact UI elements.
  • sm : Small avatar for lists, menus, or sidebars.
  • md (default) : Balanced size for most use cases.
  • lg : Large avatar for cards, profiles, or highlights.
  • xl / 2xl / 3xl : Extra large avatars for hero sections or main profile displays.

Clickable

When as="button" or clickable is set, the avatar becomes interactive with hover and focus styles.

<Avatar as="button" clickable src="/user.jpg" />

Fallbacks

  • Initials : Provide a name in `fallback` prop to auto-generate initials.
  • Custom Content : Use `fallbackContent` to display any custom React node.
  • Default Icon : If no `src` or fallback is provided, a default user icon is shown.

Skeleton / Placeholder

While loading images, you can show a skeleton placeholder for a smoother experience.

<AvatarSkeleton variant="circular" />

Disabled State

Set disabled to visually indicate that the avatar is not interactive.

<Avatar as="button" disabled src="/user.jpg" />