import { LitElement, html, nothing } from "lit"; import { customElement } from "../../utils/decorators/custom-element"; import { property, state } from "lit/decorators.js"; import { getInitials } from "../../utils/strings"; import { type MemberType } from "../../types/members.types"; import { type UserType } from "../../types/users.types"; import { Presence, type PresenceType } from "../../types/presence.types"; import { consume } from "@lit/context"; import { UserContext } from "../../contexts/user-context"; import { ShadowPartsController } from "../../controllers/shadow-parts-controller"; import { partMap } from "../../utils/directives/shadow-part-map"; import { S4 } from "../../utils/data"; import { checkImageLoad, imageLoaded } from "../../utils/images"; import { ref } from "lit/directives/ref.js"; import rebootCss from "../../scss/reboot.scss"; import avatarCss from "../../scss/components/avatar.scss"; import presenceCss from "../../scss/components/presence.scss"; import hostContentsCss from "../../scss/host-contents.scss"; import "./wy-presence"; import "./wy-icon"; declare global { interface HTMLElementTagNameMap { "wy-avatar": WyAvatar; "wy-avatar-group": WyAvatarGroup; "wy-avatar-header": WyAvatarHeader; } } /** * Base component to render an avatar. If no `src` is provided, it uses `name` to construct an avatar from initials. * * **Used sub components** * * - [``](./wy-presence.ts) * - [``](./wy-icon.ts) * * @csspart wy-avatar - The avatar component itself. * @csspart wy-avatar-shape - The general styling shape for the avatar. * @csspart wy-avatar-img - Any provided avatar image. * @csspart wy-avatar-initials - Avatar initials when no avatar image is provided. * @csspart wy-avatar-initials-text - The text for avatar initials. * @csspart wy-avatar-type-icon - Type overlay icon. * @csspart wy-avatar-presence - Presence indicator. * @csspart wy-presence-mask - Masking for presence indicator. */ @customElement("wy-avatar") export class WyAvatar extends LitElement { static override styles = [rebootCss, avatarCss, presenceCss]; protected exportParts = new ShadowPartsController(this); /** * The size of the avatar in pixels. The pixels are translated to rem size with a base of 16px/rem. */ @property({ type: Number }) size: number = 32; /** * Source URL string for the avatar image. */ @property() src?: string = ""; /** * Name for the avatar. Used as title and for generating initials when `src` is not provided. */ @property() name?: string = ""; /** * Description for the avatar. This is appended to the title. */ @property() description?: string = ""; /** * Whether the avatar represents an agent. */ @property({ type: Boolean, reflect: true }) isAgent?: boolean = false; /** * Presence status for the avatar. */ @property() presence?: PresenceType; override render() { const remSize = this.size / 16; let initials; if (this.name) { initials = getInitials(this.name); } const avatarParts = { "wy-avatar-shape": true, "wy-presence-mask": this.presence === Presence.Active, }; return html` ${this.src ? html` { imageLoaded(e); }} /> ` : html` `}
${initials}
${this.isAgent ? html`` : nothing} ${this.presence && !this.isAgent ? html`` : nothing} `; } } /** * Group of two or more avatars displayed together. * * ## Used sub components * * - [``](./wy-avatar.ts) * * @csspart wy-avatar-back - The avatar in the back * @csspart wy-avatar-front - The avatar in the front * @csspart wy-avatar-back-mask-bg - Background for the back avatar mask. * @csspart wy-avatar-back-mask - Cutout mask for the back avatar. */ @customElement("wy-avatar-group") export class WyAvatarGroup extends LitElement { static override styles = [rebootCss, avatarCss, hostContentsCss]; protected exportParts = new ShadowPartsController(this); /** * The overall size of the avatar group */ @property({ type: Number }) size: number = 32; /** * Array of members that can be shown. */ @property({ type: Array, attribute: false, }) members?: MemberType[]; /** * The current user. * @internal */ @consume({ context: UserContext, subscribe: true }) @state() user: UserType | undefined; protected uniqueId = `wy-avatar-${S4()}`; override render() { if (!this.user) { return nothing; } const remSize = this.size / 16; const otherMembers = (this.members || []) .filter((member) => member.id !== this.user?.id) .slice(0, 2) .reverse(); const frontMember: MemberType | undefined = otherMembers?.shift() || this.user; const backMember: MemberType | undefined = otherMembers?.shift() || (frontMember !== this.user ? this.user : undefined); return [ html` `, html` `, html` `, ]; } } /** * Wrapper for displaying a description under an avatar. * * @slot Standard slot for any avatar to display. * @csspart wy-avatar-header - The avatar header component itself * @csspart wy-avatar-description */ @customElement("wy-avatar-header") export class WyAvatarHeader extends LitElement { static override styles = [rebootCss, avatarCss]; protected exportParts = new ShadowPartsController(this); /** * Any description to display. */ @property() description?: string; protected override render() { return html` ${this.description ? html`
${this.description}
` : nothing} `; } }