UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

5 lines (4 loc) 3.27 kB
/*! All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://github.com/Esri/calcite-design-system/blob/dev/LICENSE.md for details. v3.2.1 */ import{n as m,v as z}from"./TGDV7BPX.js";import"./YSI7ZYDZ.js";import{d as k}from"./3ADX47DD.js";import"./NNVH7JUI.js";import{E as g,F as c,G as p,H as h,R as v,c as b,d as o}from"./BJZTU5BQ.js";var u={thumbnail:"thumbnail",background:"background",initials:"initials",icon:"icon"};function x(r){r=$(r);let e=0;for(let i=0;i<r.length;i++)e=r.charCodeAt(i)+((e<<5)-e);let t="#";for(let i=0;i<3;i++){let a=e>>i*8&255;t+=("00"+a.toString(16)).substr(-2)}return t}function $(r){let e=Math.floor(r.length/2),t=r.split("").reverse().join("");return t.substring(e)+t.slice(0,e)}function y(r){let{r:e,g:t,b:i}=r;e/=255,t/=255,i/=255;let a=Math.max(e,t,i),l=Math.min(e,t,i),s=a-l;if(a===l)return 0;let n=(a+l)/2;switch(a){case e:n=(t-i)/s+(t<i?6:0);break;case t:n=(i-e)/s+2;break;case i:n=(e-t)/s+4;break}return Math.round(n*60)}function C(r){return y(z(r))}var T=b`:host{display:inline-block;overflow:hidden;border-radius:var(--calcite-avatar-corner-radius, 50%);color:var(--calcite-avatar-color, var(--calcite-color-text-2))}:host([scale=s]){block-size:1.5rem;inline-size:1.5rem;font-size:var(--calcite-font-size--3)}:host([scale=m]){block-size:2rem;inline-size:2rem;font-size:var(--calcite-font-size--2)}:host([scale=l]){block-size:2.75rem;inline-size:2.75rem;font-size:var(--calcite-font-size-0)}.icon{display:flex}.background{display:flex;block-size:100%;inline-size:100%;align-items:center;justify-content:center;border-radius:var(--calcite-avatar-corner-radius, 50%)}.initials{font-weight:var(--calcite-font-weight-bold);text-transform:uppercase}.thumbnail{block-size:100%;inline-size:100%;border-radius:var(--calcite-avatar-corner-radius, 50%)}:host([hidden]){display:none}[hidden]{display:none}`,d=class extends g{constructor(){super(...arguments),this.thumbnailFailedToLoad=!1,this.scale="m"}static{this.properties={thumbnailFailedToLoad:[16,{},{state:!0}],fullName:[3,{},{reflect:!0}],label:1,scale:[3,{},{reflect:!0}],thumbnail:[3,{},{reflect:!0}],userId:[3,{},{reflect:!0}],username:[3,{},{reflect:!0}]}}static{this.styles=T}determineContent(){if(this.thumbnail&&!this.thumbnailFailedToLoad)return o`<img alt=${(this.label||"")??h} class=${c(u.thumbnail)} @error=${()=>this.thumbnailFailedToLoad=!0} src=${this.thumbnail??h}>`;let e=this.generateInitials(),t=this.generateFillColor();return o`<span .ariaLabel=${this.label||this.fullName} class=${c(u.background)} role=figure style=${p({backgroundColor:t})}>${e?o`<span aria-hidden=true class=${c(u.initials)}>${e}</span>`:o`<calcite-icon class=${c(u.icon)} icon=user .scale=${this.scale}></calcite-icon>`}</span>`}generateFillColor(){let{userId:e,username:t,fullName:i,el:a}=this,l=k(a),s=e&&`#${e.substr(e.length-6)}`,n=t||i||"",f=s&&m(s)?s:x(n);return!e&&!n||!m(f)?"var(--calcite-avatar-background-color, var(--calcite-color-foreground-2))":`var(--calcite-avatar-background-color, hsl(${C(f)}, 60%, ${l==="dark"?20:90}%))`}generateInitials(){let{fullName:e,username:t}=this;return e?e.trim().split(" ").map(i=>i.substring(0,1)).join(""):t?t.substring(0,2):!1}render(){return this.determineContent()}};v("calcite-avatar",d);export{d as Avatar};