Group avatars together.
Installation
npx shadcn@latest add @caprice/avatar-groupnpm install @caprice-ui/reactInstall the following dependencies:
npm install @base-ui/react lucide-reactAdd a cn helper
import { defineConfig, type VariantProps } from 'cva';import { twMerge } from 'tailwind-merge';export const { compose, cva, cx: cn,} = defineConfig({ hooks: { onComplete: (className: string) => twMerge(className), },});export type { VariantProps };Copy and paste the following code into your project.
'use client';import { mergeProps } from '@base-ui/react/merge-props';import { useRender } from '@base-ui/react/use-render';import { cn } from '@/lib/utils';/** * Displays a group of avatars. * Renders a `<div>` element. * * Documentation: [Caprice UI Avatar Group](https://caprice-ui.com/docs/components/avatar-group) */export namespace Root { export type Props = useRender.ComponentProps<'div'> & { /** * The spacing between the avatars. * @default -1.5 */ spacing?: number; };}export function Root({ className, spacing = -1.5, render, ...props }: Root.Props) { const defaultProps: useRender.ElementProps<'div'> & { 'data-slot'?: string } = { 'data-slot': 'avatar-group', className: cn('flex flex-wrap items-center space-x-[--spacing(var(--gap))]', className), style: { '--gap': spacing, } as React.CSSProperties, }; return useRender({ render, defaultTagName: 'div', props: mergeProps<'div'>(defaultProps, props), });}Update the import paths to match your project setup.
Usage
import * as AvatarGroup from "@/components/caprice-ui/avatar-group"import * as Avatar from "@/components/caprice-ui/avatar"import * as AvatarGroup from "@caprice-ui/react/avatar-group"import * as Avatar from "@caprice-ui/react/avatar"<AvatarGroup.Root>
<Avatar.Root>
<Avatar.Image src="https://github.com/gomah.png" />
<Avatar.Fallback>TM</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root>
<Avatar.Image src="https://github.com/fabrelea.png" />
<Avatar.Fallback>LF</Avatar.Fallback>
</Avatar.Root>
</AvatarGroup.Root>