// // Copyright 2025 DXOS.org // import { LitElement, html, svg } from 'lit'; import { customElement, property, state } from 'lit/decorators.js'; import { styleMap } from 'lit/directives/style-map.js'; import { makeId } from '@dxos/react-hooks'; import { type Size } from '../defs.ts'; import { getFallbackGlyph } from './fallback.ts'; export type ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error'; export type AvatarVariant = 'square' | 'circle'; export type AvatarStatus = 'active' | 'inactive' | 'current' | 'internal' | 'error' | 'warning'; export type AvatarAnimation = 'pulse' | 'none'; const rx = '0.125rem'; export type DxAvatarProps = Partial< Pick< DxAvatar, | 'fallback' | 'imgSrc' | 'imgCrossOrigin' | 'imgReferrerPolicy' | 'variant' | 'status' | 'animation' | 'hue' | 'hueVariant' | 'size' | 'icon' | 'rootClassName' > >; @customElement('dx-avatar') export class DxAvatar extends LitElement { private maskId: string; constructor() { super(); this.maskId = makeId('avatar__mask'); } @property({ type: String }) fallback: string = '🫥'; @property({ type: String }) imgSrc: string | undefined = undefined; @property({ type: String }) imgCrossOrigin: NonNullable | undefined = undefined; @property({ type: String }) imgReferrerPolicy: HTMLImageElement['referrerPolicy'] | undefined = undefined; @property({ type: String }) variant: AvatarVariant = 'circle'; @property({ type: String }) status: AvatarStatus | undefined = undefined; @property({ type: String }) animation: AvatarAnimation = 'none'; @property({ type: String }) hue: string | undefined = undefined; @property({ type: String }) hueVariant: 'fill' | 'surface' | 'transparent' = 'fill'; @property({ type: String }) size: Size = 10; @property({ type: String }) icon: string | undefined = undefined; @property({ type: String }) rootClassName: string | undefined = undefined; @state() loadingStaus: ImageLoadingStatus = 'idle'; override connectedCallback(): void { super.connectedCallback(); this.role = 'img'; this.loadingStaus = this.imgSrc ? 'loading' : 'idle'; } override willUpdate(changedProperties: Map): void { if (changedProperties.has('imgSrc')) { this.loadingStaus = changedProperties.get('imgSrc') ? 'loading' : 'idle'; } } private handleLoad(): void { this.loadingStaus = 'loaded'; } private handleError(): void { this.loadingStaus = 'error'; } override render() { const numericSize = this.size === 'px' ? 1 : Number(this.size); const sizePx = numericSize * 4; const ringWidth = this.status ? (numericSize > 4 ? 2 : numericSize > 3 ? 1 : 1) : 0; const ringGap = this.status ? (numericSize > 12 ? 3 : numericSize > 4 ? 2 : numericSize > 3 ? 1 : 0) : 0; const r = sizePx / 2 - ringGap - ringWidth; const isTextOnly = Boolean(this.fallback && /[0-9a-zA-Z]+/.test(this.fallback)); const fontScale = (isTextOnly ? 3 : 3.6) * (1 / 1.612); const bg = this.hueVariant === 'transparent' ? 'transparent' : this.hue ? this.hueVariant === 'surface' ? `var(--color-${this.hue}-surface)` : `var(--color-${this.hue === 'neutral' ? 'input-surface' : `${this.hue}-bg`})` : 'var(--surface-bg)'; const fg = this.hue && this.hueVariant === 'surface' ? `var(--color-${this.hue}-fg)` : 'var(--color-accent-fg)'; return html`${svg` ${ this.variant === 'circle' ? svg`` : svg` ` } ${ this.variant === 'circle' ? svg` ` : svg` ` } ${ this.icon ? svg` ` : // NOTE: Firefox currently doesn't fully support alignment-baseline. svg` ${getFallbackGlyph(this.fallback)} ` } ${ this.imgSrc && svg` ` } `}`; } override createRenderRoot(): this { return this; } }