UNPKG

xtreme-ui

Version:

Collection of reusable components that can be used in web projects

2 lines (1 loc) 1.3 kB
"use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react/jsx-runtime"),a=require("react"),E=require("clsx"),n=require("../../../utils/constants/iconCollection.cjs"),g=require("../../../utils/helper/imageHelper.cjs"),F=require("../Icon/Icon.cjs");;/* empty css */const j=require("./types.cjs"),p=a.forwardRef((h,S)=>{const{className:v,src:t,file:r,alt:y,placeholderIcon:I,size:i="default",onClick:A}=h,[u,d]=a.useState(),[C,q]=a.useState(n.FACE_ICONS.happy[0]),[x,s]=a.useState(!!t||!!r),[m,o]=a.useState(!1),f=typeof i=="number"?i:j.EAvatarSize[i],N=E("xtrAvatar",v,x&&"loading",m&&"error"),c=e=>{e&&setTimeout(()=>d(e),300),setTimeout(()=>s(!1),1e3)};return a.useEffect(()=>{q(n.FACE_ICONS.happy[Math.floor(Math.random()*n.FACE_ICONS.happy.length)]);try{r?(s(!0),o(!1),g.readImageFile(r).then(e=>c(e)).catch(e=>{console.log(e),s(!1),o(!0)})):t?(s(!0),o(!1),g.readImageSrc(t).then(e=>c(e)).catch(e=>{console.log(e,t),s(!1),o(!0)})):(d(void 0),c())}catch{c()}},[t,r]),l.jsx("div",{ref:S,className:N,style:{"--avatarSize":f+"px"},onClick:A,role:"img",children:u?l.jsx("img",{className:"image",src:u,alt:y}):l.jsx(F.Icon,{className:"placeholder",type:"solid",size:8+f/4,code:m?"e1b7":I??C})})});p.displayName="Avatar";exports.Avatar=p;