Avatar
An image element with a fallback for representing the user.
import Avatar from '@dinui/react/avatar'
export default function AvatarVariantDemo() { return ( <Avatar> <Avatar.Image src="https://github.com/dinwwwh.png" alt="@dinwwwh" /> <Avatar.Fallback>DN</Avatar.Fallback> </Avatar> )}Size
DNDNDN
import Avatar from '@dinui/react/avatar'
export default function AvatarVariantDemo() { return ( <div className="flex flex-wrap gap-6 items-center"> <Avatar size="sm"> <Avatar.Image src="https://github.com/dinwwwh.png" alt="@dinwwwh" /> <Avatar.Fallback>DN</Avatar.Fallback> </Avatar>
<Avatar size="md"> <Avatar.Image src="https://github.com/dinwwwh.png" alt="@dinwwwh" /> <Avatar.Fallback>DN</Avatar.Fallback> </Avatar>
<Avatar size="lg"> <Avatar.Image src="https://github.com/dinwwwh.png" alt="@dinwwwh" /> <Avatar.Fallback>DN</Avatar.Fallback> </Avatar> </div> )}Installation
-
Follow Installation Guide
To enable DinUI functionality in your project, you will need to properly set up Tailwind and install the necessary dependencies. -
All done
You now can start using this component in your project.
-
Follow Installation Guide
To enable DinUI functionality in your project, you will need to properly set up Tailwind and install the necessary dependencies. -
Run the following command in your project
Terminal window npx @dinui/cli@latest add avatarTerminal window yarn dlx @dinui/cli@latest add avatarTerminal window pnpm dlx @dinui/cli@latest add avatarTerminal window bunx @dinui/cli@latest add avatar -
Update the import paths to match your project setup
-
All done
You now can start using this component in your project.
-
Follow Installation Guide
To enable DinUI functionality in your project, you will need to properly set up Tailwind and install the necessary dependencies. -
Install dependencies
Terminal window npm install @radix-ui/react-avatar tailwind-variants tailwind-variants type-festTerminal window yarn add @radix-ui/react-avatar tailwind-variants tailwind-variants type-festTerminal window pnpm add @radix-ui/react-avatar tailwind-variants tailwind-variants type-festTerminal window bun add @radix-ui/react-avatar tailwind-variants tailwind-variants type-fest -
Copy and paste the following code into your project
'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<typeof avatar>const AvatarContext = createContext<AvatarVariantProps>(avatar.defaultVariants)const AvatarRoot = forwardRef<React.ElementRef<typeof AvatarPrimitive.Root>,Merge<React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>, AvatarVariantProps>>(({ size, ...props }, ref) => {const variantProps = { size }const { root } = avatar(variantProps)return (<AvatarContext.Provider value={variantProps}><AvatarPrimitive.Root {...props} ref={ref} className={root({ className: props.className })} /></AvatarContext.Provider>)})AvatarRoot.displayName = AvatarPrimitive.Root.displayNameconst AvatarImage = forwardRef<React.ElementRef<typeof AvatarPrimitive.Image>,React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>>((props, ref) => {const variantProps = useContext(AvatarContext)const { image } = avatar(variantProps)return (<AvatarPrimitive.Image {...props} ref={ref} className={image({ className: props.className })} />)})AvatarImage.displayName = AvatarPrimitive.Image.displayNameconst AvatarFallback = forwardRef<React.ElementRef<typeof AvatarPrimitive.Fallback>,React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>>((props, ref) => {const variantProps = useContext(AvatarContext)const { fallback } = avatar(variantProps)return (<AvatarPrimitive.Fallback{...props}ref={ref}className={fallback({ className: props.className })}/>)})AvatarFallback.displayName = AvatarPrimitive.Fallback.displayNameconst Avatar = Object.assign(AvatarRoot, {Image: AvatarImage,Fallback: AvatarFallback,})export default Avatarexport { avatar, AvatarPrimitive } -
Update the import paths to match your project setup
-
All done
You now can start using this component in your project.