Avatar
shadcn-lit-registry/avatarFreeComponent
An image element with a fallback for representing the user. Built with Lit and styled using Tailwind CSS.
Install it
npx shadcn@latest add https://lit-registry.lloydrichards.dev/r/avatar.jsonSource
1import { cva } from "class-variance-authority";2import { html, LitElement, type PropertyValues } from "lit";3import { customElement, property, state } from "lit/decorators.js";4import { TW } from "@/registry/lib/tailwindMixin";5import { cn } from "@/registry/lib/utils";67export const avatarVariants = cva(8 "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full",9);1011export interface AvatarProperties {12 src?: string;13 alt?: string;14 loading?: "eager" | "lazy";15}1617@customElement("ui-avatar")18export class Avatar extends TW(LitElement) implements AvatarProperties {19 @property({ type: String }) src?: string;20 @property({ type: String }) alt = "";21 @property({ type: String }) loading: "eager" | "lazy" = "lazy";2223 @state() private _imageLoaded = false;24 @state() private _imageError = false;2526 override updated(changedProperties: PropertyValues) {27 super.updated(changedProperties);2829 if (changedProperties.has("src")) {30 this._imageLoaded = false;31 this._imageError = false;32 }33 }3435 private _handleImageLoad() {36 this._imageLoaded = true;37 this._imageError = false;38 }3940 private _handleImageError() {41 this._imageLoaded = false;42 this._imageError = true;43 }4445 private get _shouldShowImage(): boolean {46 return Boolean(this.src && this._imageLoaded && !this._imageError);47 }4849 private get _shouldShowFallback(): boolean {50 return !this.src || this._imageError || !this._imageLoaded;51 }5253 override render() {54 const hasImageSlot = this.querySelector('[slot="image"]');5556 return html`57 <span class=${cn(avatarVariants(), this.className)}>58 <slot name="image"></slot>5960 ${61 this.src && !hasImageSlot62 ? html`63 <img64 class="aspect-square h-full w-full object-cover"65 src=${this.src}66 alt=${this.alt}67 loading=${this.loading}68 @load=${this._handleImageLoad}69 @error=${this._handleImageError}70 style=${this._shouldShowImage ? "" : "display: none;"}71 />72 `73 : ""74 }75 ${76 this._shouldShowFallback77 ? html`78 <span79 class="flex h-full w-full items-center justify-center rounded-full bg-muted"80 >81 <slot></slot>82 </span>83 `84 : ""85 }86 </span>87 `;88 }89}9091declare global {92// … truncated
Files it writes
More from shadcn-lit-registry
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Badgebadge | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Buttonbutton | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Cardcard | shadcn-lit-registry | Components | Free | no deps · 2 files | |
| Checkboxcheckbox | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Collapsiblecollapsible | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Commandcommand | shadcn-lit-registry | Components | Free | 1 dep · 2 files | |
| Context Menucontext-menu | shadcn-lit-registry | Components | Free | 2 deps · 2 files |
