'use client' import * as AvatarPrimitive from '@radix-ui/react-avatar' import { createContext, forwardRef, useContext } from 'react' import type { VariantProps } from 'tailwind-variants' import { tv } from 'tailwind-variants' import type { Merge } from 'type-fest' const avatar = tv({ slots: { root: 'relative flex shrink-0 overflow-hidden rounded-full', image: 'object-cover object-center h-full w-full', fallback: 'flex h-full w-full items-center justify-center rounded-full bg-bg--active', }, variants: { size: { sm: { root: 'size-8', }, md: { root: 'size-9', }, lg: { root: 'size-10', }, }, }, defaultVariants: { size: 'md', }, }) type AvatarVariantProps = VariantProps const AvatarContext = createContext(avatar.defaultVariants) const AvatarRoot = forwardRef< React.ElementRef, Merge, AvatarVariantProps> >(({ size, ...props }, ref) => { const variantProps = { size } const { root } = avatar(variantProps) return ( ) }) AvatarRoot.displayName = AvatarPrimitive.Root.displayName const AvatarImage = forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, ref) => { const variantProps = useContext(AvatarContext) const { image } = avatar(variantProps) return ( ) }) AvatarImage.displayName = AvatarPrimitive.Image.displayName const AvatarFallback = forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >((props, ref) => { const variantProps = useContext(AvatarContext) const { fallback } = avatar(variantProps) return ( ) }) AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName const Avatar = Object.assign(AvatarRoot, { Image: AvatarImage, Fallback: AvatarFallback, }) export default Avatar export { avatar, AvatarPrimitive }