Avatar
A circular user avatar with image support and initials fallback in five sizes.
An image, or the initials that stand in for one. Five sizes, all circles: xs for a reply line, sm for a compact row, md for a member row, default for a message, and lg for a profile.
It is built on Radix Avatar in React and Reka UI Avatar in Vue. The primitive tracks the image. AvatarFallback shows while the image loads and when the load fails.
newt-avatarimport { Avatar } from "@/registry/bases/newt/ui/avatar"
export default function AvatarDemo() {
return (
<div className="flex items-center gap-4">
<Avatar size="xs">N</Avatar>
<Avatar size="sm">N</Avatar>
<Avatar size="md">N</Avatar>
<Avatar>N</Avatar>
<Avatar size="lg">N</Avatar>
</div>
)
}<script setup lang="ts">
import { Avatar } from "~~/registry/bases/newt/ui/avatar"
</script>
<template>
<div class="flex items-center gap-4">
<Avatar size="xs">N</Avatar>
<Avatar size="sm">N</Avatar>
<Avatar size="md">N</Avatar>
<Avatar>N</Avatar>
<Avatar size="lg">N</Avatar>
</div>
</template>Installation
npx newtui@latest add avatarnpx shadcn@latest add @newtui/avatarnpx shadcn-vue@latest add @newtui/avatarpnpm dlx newtui@latest add avatarpnpm dlx shadcn@latest add @newtui/avatarpnpm dlx shadcn-vue@latest add @newtui/avataryarn dlx newtui@latest add avataryarn dlx shadcn@latest add @newtui/avataryarn dlx shadcn-vue@latest add @newtui/avatarbunx newtui@latest add avatarbunx shadcn@latest add @newtui/avatarbunx shadcn-vue@latest add @newtui/avatardeno run -A npm:newtui@latest add avatardeno run -A npm:shadcn@latest add @newtui/avatardeno run -A npm:shadcn-vue@latest add @newtui/avatarvlx newtui@latest add avatarvlx shadcn@latest add @newtui/avatarvlx shadcn-vue@latest add @newtui/avatarvp dlx newtui@latest add avatarvp dlx shadcn@latest add @newtui/avatarvp dlx shadcn-vue@latest add @newtui/avatarnubx newtui@latest add avatarnubx shadcn@latest add @newtui/avatarnubx shadcn-vue@latest add @newtui/avatarnlx newtui@latest add avatarnlx shadcn@latest add @newtui/avatarnlx shadcn-vue@latest add @newtui/avatarupx newtui@latest add avatarupx shadcn@latest add @newtui/avatarupx shadcn-vue@latest add @newtui/avatar- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install class-variance-authority radix-uipnpm add class-variance-authority radix-uiyarn add class-variance-authority radix-uibun add class-variance-authority radix-uideno add npm:class-variance-authority npm:radix-uivlt install class-variance-authority radix-uivp add class-variance-authority radix-uinub add class-variance-authority radix-uini class-variance-authority radix-uiupm add class-variance-authority radix-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
npm install class-variance-authority reka-uipnpm add class-variance-authority reka-uiyarn add class-variance-authority reka-uibun add class-variance-authority reka-uideno add npm:class-variance-authority npm:reka-uivlt install class-variance-authority reka-uivp add class-variance-authority reka-uinub add class-variance-authority reka-uini class-variance-authority reka-uiupm add class-variance-authority reka-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Copy the source into your project
"use client"
import { cva, type VariantProps } from "class-variance-authority"
import { Avatar as AvatarPrimitive } from "radix-ui"
import * as React from "react"
import { cn } from "@/lib/utils"
const avatarVariants = cva(
"relative flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-newt-brand font-semibold text-newt-on-accent",
{
variants: {
size: {
xs: "h-4 w-4 text-[9px]",
sm: "h-6 w-6 text-[11px]",
md: "h-8 w-8 text-[13px]",
default: "h-10 w-10 text-base",
lg: "h-20 w-20 text-[28px]",
},
},
defaultVariants: {
size: "default",
},
}
)
export interface AvatarProps
extends
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>,
VariantProps<typeof avatarVariants> {}
/*
* Radix owns the image state: `AvatarImage` renders once its source has
* loaded, and `AvatarFallback` shows until then and when the load fails.
* Only the look is newt/ui's.
*/
const Avatar = React.forwardRef<
React.ComponentRef<typeof AvatarPrimitive.Root>,
AvatarProps
>(({ className, size, ...props }, ref) => (
<AvatarPrimitive.Root
ref={ref}
className={cn(avatarVariants({ size }), className)}
{...props}
/>
))
Avatar.displayName = "Avatar"
const AvatarImage = React.forwardRef<
React.ComponentRef<typeof AvatarPrimitive.Image>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>
>(({ className, alt = "", ...props }, ref) => (
<AvatarPrimitive.Image
ref={ref}
alt={alt}
className={cn("h-full w-full object-cover", className)}
{...props}
/>
))
AvatarImage.displayName = "AvatarImage"
const AvatarFallback = React.forwardRef<
React.ComponentRef<typeof AvatarPrimitive.Fallback>,
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
<AvatarPrimitive.Fallback
ref={ref}
className={cn("flex h-full w-full items-center justify-center", className)}
{...props}
/>
))
AvatarFallback.displayName = "AvatarFallback"
export { Avatar, AvatarImage, AvatarFallback, avatarVariants }<script setup lang="ts">
import { AvatarRoot, type AvatarRootProps } from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import { avatarVariants, type AvatarVariants } from "."
/*
* Reka owns the image state: `AvatarImage` shows once its source has loaded,
* and `AvatarFallback` shows until then and when the load fails. Only the
* look is newt/ui's.
*/
const props = defineProps<
AvatarRootProps & {
class?: HTMLAttributes["class"]
size?: AvatarVariants["size"]
}
>()
const delegated = computed(() => {
const { class: _class, size: _size, ...rest } = props
return rest
})
</script>
<template>
<AvatarRoot
v-bind="delegated"
:class="cn(avatarVariants({ size: props.size }), props.class)"
>
<slot />
</AvatarRoot>
</template><script setup lang="ts">
import {
AvatarImage,
useForwardPropsEmits,
type AvatarImageEmits,
type AvatarImageProps,
} from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = withDefaults(
defineProps<
AvatarImageProps & {
class?: HTMLAttributes["class"]
/** Accessible name of the image. Leave it empty beside a visible name. */
alt?: string
}
>(),
{ alt: "" }
)
const emits = defineEmits<AvatarImageEmits>()
const delegated = computed(() => {
const { class: _class, alt: _alt, ...rest } = props
return rest
})
const forwarded = useForwardPropsEmits(delegated, emits)
</script>
<template>
<!--
Reka puts `role="img"` on the image. An image with no name is decoration,
so it takes `presentation` and a screen reader skips it.
-->
<AvatarImage
v-bind="forwarded"
:alt="props.alt"
:role="props.alt ? 'img' : 'presentation'"
:class="cn('h-full w-full object-cover', props.class)"
/>
</template><script setup lang="ts">
import { AvatarFallback, type AvatarFallbackProps } from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<
AvatarFallbackProps & { class?: HTMLAttributes["class"] }
>()
const delegated = computed(() => {
const { class: _class, ...rest } = props
return rest
})
</script>
<template>
<AvatarFallback
v-bind="delegated"
:class="cn('flex h-full w-full items-center justify-center', props.class)"
>
<slot />
</AvatarFallback>
</template>import { cva, type VariantProps } from "class-variance-authority"
export { default as Avatar } from "./Avatar.vue"
export { default as AvatarImage } from "./AvatarImage.vue"
export { default as AvatarFallback } from "./AvatarFallback.vue"
export const avatarVariants = cva(
"relative flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-newt-brand font-semibold text-newt-on-accent",
{
variants: {
size: {
xs: "h-4 w-4 text-[9px]",
sm: "h-6 w-6 text-[11px]",
md: "h-8 w-8 text-[13px]",
default: "h-10 w-10 text-base",
lg: "h-20 w-20 text-[28px]",
},
},
defaultVariants: {
size: "default",
},
}
)
export type AvatarVariants = VariantProps<typeof avatarVariants>Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Avatar to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Avatar to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { Avatar } from "@/components/ui/avatar"
export function Example() {
return <Avatar />
}<script setup lang="ts">
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"
</script>
<template>
<Avatar />
</template>Examples
With an image
AvatarImage fills the circle at every size and is cropped to it. Give it an alt when the avatar stands on its own.
newt-avatarimport { Avatar, AvatarImage } from "@/registry/bases/newt/ui/avatar"
/* Inline so the demo never reaches out to the network for a placeholder. */
const PHOTO =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 40 40'%3E%3Crect width='40' height='40' fill='%233ba55d'/%3E%3Ccircle cx='20' cy='16' r='7' fill='%23ffffff'/%3E%3Cpath d='M6 40a14 14 0 0 1 28 0Z' fill='%23ffffff'/%3E%3C/svg%3E"
export default function AvatarImageDemo() {
return (
<div className="flex items-center gap-4">
<Avatar size="sm">
<AvatarImage src={PHOTO} alt="Mira Okafor" />
</Avatar>
<Avatar>
<AvatarImage src={PHOTO} alt="Mira Okafor" />
</Avatar>
<Avatar size="lg">
<AvatarImage src={PHOTO} alt="Mira Okafor" />
</Avatar>
</div>
)
}<script setup lang="ts">
import { Avatar, AvatarImage } from "~~/registry/bases/newt/ui/avatar"
/* Inline so the demo never reaches out to the network for a placeholder. */
const PHOTO =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 40 40'%3E%3Crect width='40' height='40' fill='%233ba55d'/%3E%3Ccircle cx='20' cy='16' r='7' fill='%23ffffff'/%3E%3Cpath d='M6 40a14 14 0 0 1 28 0Z' fill='%23ffffff'/%3E%3C/svg%3E"
</script>
<template>
<div class="flex items-center gap-4">
<Avatar size="sm">
<AvatarImage :src="PHOTO" alt="Mira Okafor" />
</Avatar>
<Avatar>
<AvatarImage :src="PHOTO" alt="Mira Okafor" />
</Avatar>
<Avatar size="lg">
<AvatarImage :src="PHOTO" alt="Mira Okafor" />
</Avatar>
</div>
</template>Initials
AvatarFallback centres initials when there is no image. The fill is the brand colour unless a token utility on the avatar says otherwise; a lone avatar takes role="img" and an aria-label, because initials are not a name.
newt-avatarimport { Avatar, AvatarFallback } from "@/registry/bases/newt/ui/avatar"
/*
* Initials on their own are not a name, so a lone avatar is an image with a
* label. The fill is a token utility passed through `className`.
*/
export default function AvatarFallbackDemo() {
return (
<div className="flex items-center gap-4">
<Avatar role="img" aria-label="Mira Okafor">
<AvatarFallback>MO</AvatarFallback>
</Avatar>
<Avatar role="img" aria-label="Jun Park" className="bg-newt-online">
<AvatarFallback>JP</AvatarFallback>
</Avatar>
<Avatar role="img" aria-label="Sam Reyes" className="bg-newt-danger">
<AvatarFallback>SR</AvatarFallback>
</Avatar>
<Avatar
role="img"
aria-label="Deleted user"
className="bg-newt-bg-elevated text-newt-text-muted"
>
<AvatarFallback>?</AvatarFallback>
</Avatar>
</div>
)
}<script setup lang="ts">
import { Avatar, AvatarFallback } from "~~/registry/bases/newt/ui/avatar"
</script>
<template>
<!--
Initials on their own are not a name, so a lone avatar is an image with a
label. The fill is a token utility passed through `class`.
-->
<div class="flex items-center gap-4">
<Avatar role="img" aria-label="Mira Okafor">
<AvatarFallback>MO</AvatarFallback>
</Avatar>
<Avatar role="img" aria-label="Jun Park" class="bg-newt-online">
<AvatarFallback>JP</AvatarFallback>
</Avatar>
<Avatar role="img" aria-label="Sam Reyes" class="bg-newt-danger">
<AvatarFallback>SR</AvatarFallback>
</Avatar>
<Avatar
role="img"
aria-label="Deleted user"
class="bg-newt-bg-elevated text-newt-text-muted"
>
<AvatarFallback>?</AvatarFallback>
</Avatar>
</div>
</template>Beside a name
Next to a visible name the avatar is decoration: the image takes alt="" and the initials are hidden from screen readers, so the name is read once.
newt-avatarimport {
Avatar,
AvatarFallback,
AvatarImage,
} from "@/registry/bases/newt/ui/avatar"
/* Inline so the demo never reaches out to the network for a placeholder. */
const PHOTO =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 40 40'%3E%3Crect width='40' height='40' fill='%233ba55d'/%3E%3Ccircle cx='20' cy='16' r='7' fill='%23ffffff'/%3E%3Cpath d='M6 40a14 14 0 0 1 28 0Z' fill='%23ffffff'/%3E%3C/svg%3E"
/* The name beside it is the accessible name, so the avatar stays silent. */
export default function AvatarWithNameDemo() {
return (
<div className="flex w-full max-w-xs flex-col gap-3">
<div className="flex items-center gap-3">
<Avatar>
<AvatarImage src={PHOTO} alt="" />
</Avatar>
<div className="flex min-w-0 flex-col">
<span className="truncate text-sm font-semibold text-newt-text-primary">
Mira Okafor
</span>
<span className="truncate text-xs text-newt-text-muted">
Moderator
</span>
</div>
</div>
<div className="flex items-center gap-3">
<Avatar aria-hidden="true">
<AvatarFallback>JP</AvatarFallback>
</Avatar>
<div className="flex min-w-0 flex-col">
<span className="truncate text-sm font-semibold text-newt-text-primary">
Jun Park
</span>
<span className="truncate text-xs text-newt-text-muted">
Joined yesterday
</span>
</div>
</div>
</div>
)
}<script setup lang="ts">
import {
Avatar,
AvatarFallback,
AvatarImage,
} from "~~/registry/bases/newt/ui/avatar"
/* Inline so the demo never reaches out to the network for a placeholder. */
const PHOTO =
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 40 40'%3E%3Crect width='40' height='40' fill='%233ba55d'/%3E%3Ccircle cx='20' cy='16' r='7' fill='%23ffffff'/%3E%3Cpath d='M6 40a14 14 0 0 1 28 0Z' fill='%23ffffff'/%3E%3C/svg%3E"
</script>
<template>
<!-- The name beside it is the accessible name, so the avatar stays silent. -->
<div class="flex w-full max-w-xs flex-col gap-3">
<div class="flex items-center gap-3">
<Avatar>
<AvatarImage :src="PHOTO" alt="" />
</Avatar>
<div class="flex min-w-0 flex-col">
<span class="truncate text-sm font-semibold text-newt-text-primary">
Mira Okafor
</span>
<span class="truncate text-xs text-newt-text-muted">Moderator</span>
</div>
</div>
<div class="flex items-center gap-3">
<Avatar aria-hidden="true">
<AvatarFallback>JP</AvatarFallback>
</Avatar>
<div class="flex min-w-0 flex-col">
<span class="truncate text-sm font-semibold text-newt-text-primary">
Jun Park
</span>
<span class="truncate text-xs text-newt-text-muted">
Joined yesterday
</span>
</div>
</div>
</div>
</template>API reference
Avatar
size?"xs" | "sm" | "md" | "default" | "lg"
16px, 24px, 32px, 40px or 80px.
"xs" | "sm" | "md" | "default" | "lg""default"asChild / as-child?boolean
Puts the avatar classes on the child element. Use it to make the avatar a link or a button.
booleanfalseAvatarImage
src?string
Image URL. The image appears after it loads. If the load fails, the fallback stays.
stringalt?string
Accessible name for the image. Leave it empty beside a visible name.
string""onLoadingStatusChange?(status: "idle" | "loading" | "loaded" | "error") => void
React only. Called when the load state changes. Vue emits `loadingStatusChange`.
(status: "idle" | "loading" | "loaded" | "error") => voidAvatarFallback
delayMs?number
Milliseconds to wait before the fallback appears. Use it to prevent a flash on fast connections.
numberAvatar and AvatarFallback render a <span>. AvatarImage renders an <img>. Every part also accepts the native attributes of the element it renders, and a className that is merged rather than replaced. avatarVariants gives the same classes to an element of your own.
Accessibility
An avatar next to a name is decorative: the name is the accessible name, so alt="" is correct and repeating it is noise. An avatar that stands alone (a stack of participants, an author with no byline) needs a real alt. An avatar that shows only initials needs role="img" and an aria-label on Avatar. If the image can fail, give Avatar the name, because the fallback initials are not a name.
Tokens
Avatar reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-avatar.
--newt-brand--newt-on-accent