avatar
neobrutalui/avatarFreeComponent
Display user profile images with automatic fallback text.
Live preview
live · rendered by the registry
Rendered by neobrutalui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.neobrutalui.live/r/avatar.jsonSource
1"use client"23import * as React from "react"4import { cn } from "@/lib/utils"56const AvatarContext = React.createContext<{7 status: "loading" | "loaded" | "error"8 setStatus: (status: "loading" | "loaded" | "error") => void9} | null>(null)1011const Avatar = React.forwardRef<12 HTMLDivElement,13 React.HTMLAttributes<HTMLDivElement>14>(({ className, ...props }, ref) => {15 const [status, setStatus] = React.useState<"loading" | "loaded" | "error">("loading")1617 return (18 <AvatarContext.Provider value={{ status, setStatus }}>19 <div20 ref={ref}21 className={cn(22 "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full border-2 border-black bg-white",23 className24 )}25 {...props}26 />27 </AvatarContext.Provider>28 )29})30Avatar.displayName = "Avatar"3132const AvatarImage = React.forwardRef<33 HTMLImageElement,34 React.ImgHTMLAttributes<HTMLImageElement>35>(({ className, src, ...props }, ref) => {36 const context = React.useContext(AvatarContext)37 const { setStatus, status } = context || {}3839 React.useEffect(() => {40 if (!src) {41 setStatus?.("error")42 return43 }4445 const img = new Image()46 img.src = src as string47 img.onload = () => setStatus?.("loaded")48 img.onerror = () => setStatus?.("error")4950 return () => {51 img.onload = null52 img.onerror = null53 }54 }, [src, setStatus])5556 if (status === "error" && context) return null5758 return (59 // eslint-disable-next-line @next/next/no-img-element60 <img61 ref={ref}62 src={src}63 alt={props.alt || "Avatar"}64 className={cn("aspect-square h-full w-full object-cover", className)}65 {...props}66 />67 )68})69AvatarImage.displayName = "AvatarImage"7071const AvatarFallback = React.forwardRef<72 HTMLDivElement,73 React.HTMLAttributes<HTMLDivElement>74>(({ className, ...props }, ref) => {75 const context = React.useContext(AvatarContext)76 const status = context?.status7778 if (status === "loaded") return null7980 return (81 <div82 ref={ref}83 className={cn(84 "flex h-full w-full items-center justify-center rounded-full bg-main text-sm font-bold text-black",85// … truncated
What it pulls in
Other registry items
More from neobrutalui
All 24 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | neobrutalui | Components | Free | 2 deps | |
| alertalert | neobrutalui | Components | Free | 1 dep | |
| badgebadge | neobrutalui | Components | Free | 1 dep | |
| buttonbutton | neobrutalui | Components | Free | 1 dep | |
| cardcard | neobrutalui | Components | Free | no deps | |
| checkboxcheckbox | neobrutalui | Components | Free | 2 deps | |
| date-pickerdate-picker | neobrutalui | Components | Free | 2 deps | |
| dialogdialog | neobrutalui | Components | Free | 2 deps |
