Avatar

A circular element that displays a user's profile image or initials as a fallback.

Loading...
<twig:Avatar>
    <twig:Avatar:Image src="https://github.com/symfony.png" alt="@symfony" />
</twig:Avatar>

Installation

Ensure the Symfony UX Toolkit is installed in your Symfony app:

$ composer require --dev symfony/ux-toolkit

Then, run the following command to install the component and its dependencies:

$ bin/console ux:install avatar --kit shadcn

The UX Toolkit is not mandatory to install a component. You can install it manually by following the next steps:

  1. Copy the following file(s) into your Symfony app:
    templates/components/Avatar.html.twig
    <span
        class="{{ 'relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full ' ~ attributes.render('class')|tailwind_merge }}"
        data-slot="avatar"
        {{ attributes }}
    >
        {%- block content %}{% endblock -%}
    </span>
    
    templates/components/Avatar/Image.html.twig
    <img
        class="{{ 'aspect-square h-full w-full ' ~ attributes.render('class')|tailwind_merge }}"
        {{ attributes.defaults({alt: ''}) }}
    />
    
    templates/components/Avatar/Text.html.twig
    <span
        class="{{ 'flex h-full w-full items-center justify-center rounded-full bg-muted ' ~ attributes.render('class')|tailwind_merge }}"
        {{ attributes }}
    >
        {%- block content %}{% endblock -%}
    </span>
    
  2. If necessary, install the following Composer dependencies:
    $ composer require tales-from-a-dev/twig-tailwind-extra:^1.0.0
  3. And the most important, enjoy!

Usage

<twig:Avatar>
    <twig:Avatar:Image src="https://github.com/symfony.png" alt="@symfony" />
</twig:Avatar>

Examples

Avatar with Image

Loading...
<twig:Avatar>
    <twig:Avatar:Image src="https://github.com/symfony.png" alt="@symfony" />
</twig:Avatar>

Avatar with Text

Loading...
<div class="flex gap-1">
    <twig:Avatar>
        <twig:Avatar:Text>FP</twig:Avatar:Text>
    </twig:Avatar>
    <twig:Avatar>
        <twig:Avatar:Text class="bg-red-500 text-red-50">FP</twig:Avatar:Text>
    </twig:Avatar>
</div>

Avatar Group

Loading...
<div class="flex -space-x-2">
    <twig:Avatar>
        <twig:Avatar:Image src="https://github.com/symfony.png" alt="@symfony" />
    </twig:Avatar>
    <twig:Avatar>
        <twig:Avatar:Text>FP</twig:Avatar:Text>
    </twig:Avatar>
    <twig:Avatar>
        <twig:Avatar:Text class="bg-red-500 text-red-50">FP</twig:Avatar:Text>
    </twig:Avatar>
</div>