@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
170 lines (165 loc) • 7.3 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
import { r as getModeName } from './dom.js';
import { g as hexToRGB, i as isValidHex } from './utils.js';
import { d as defineCustomElement$1 } from './icon.js';
/**
* Convert a string to a valid hex by hashing its contents
* and using the hash as a seed for three distinct color values
*
* @param str
*/
function stringToHex(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let hex = "#";
for (let j = 0; j < 3; j++) {
const value = (hash >> (j * 8)) & 0xff;
hex += ("00" + value.toString(16)).substr(-2);
}
return hex;
}
/**
* Find the hue of a color given the separate RGB color channels
*
* @param rgb
*/
function rgbToHue(rgb) {
let { r, g, b } = rgb;
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const delta = max - min;
if (max === min) {
return 0;
}
let hue = (max + min) / 2;
switch (max) {
case r:
hue = (g - b) / delta + (g < b ? 6 : 0);
break;
case g:
hue = (b - r) / delta + 2;
break;
case b:
hue = (r - g) / delta + 4;
break;
}
return Math.round(hue * 60);
}
/**
* For a hex color, find the hue
*
* @param hex {string} - form of "#------"
*/
function hexToHue(hex) {
return rgbToHue(hexToRGB(hex));
}
const avatarCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host{display:inline-block;overflow:hidden;border-radius:50%}: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:50%}.initials{font-weight:var(--calcite-font-weight-bold);text-transform:uppercase;color:var(--calcite-ui-text-2)}.thumbnail{block-size:100%;inline-size:100%;border-radius:50%}";
const Avatar = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.scale = "m";
this.thumbnail = undefined;
this.fullName = undefined;
this.username = undefined;
this.userId = undefined;
this.label = undefined;
this.thumbnailFailedToLoad = false;
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
render() {
return this.determineContent();
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
determineContent() {
if (this.thumbnail && !this.thumbnailFailedToLoad) {
return (h("img", { alt: this.label || "", class: "thumbnail", onError: () => (this.thumbnailFailedToLoad = true), src: this.thumbnail }));
}
const initials = this.generateInitials();
const backgroundColor = this.generateFillColor();
return (h("span", { "aria-label": this.label || this.fullName, class: "background", role: "figure", style: { backgroundColor } }, initials ? (h("span", { "aria-hidden": "true", class: "initials" }, initials)) : (h("calcite-icon", { class: "icon", icon: "user", scale: this.scale }))));
}
/**
* Generate a valid background color that is consistent and unique to this user
*/
generateFillColor() {
const { userId, username, fullName, el } = this;
const theme = getModeName(el);
const id = userId && `#${userId.substr(userId.length - 6)}`;
const name = username || fullName || "";
const hex = id && isValidHex(id) ? id : stringToHex(name);
// if there is not unique information, or an invalid hex is produced, return a default
if ((!userId && !name) || !isValidHex(hex)) {
return `var(--calcite-ui-foreground-2)`;
}
const hue = hexToHue(hex);
const l = theme === "dark" ? 20 : 90;
return `hsl(${hue}, 60%, ${l}%)`;
}
/**
* Use fullname or username to generate initials
*/
generateInitials() {
const { fullName, username } = this;
if (fullName) {
return fullName
.trim()
.split(" ")
.map((name) => name.substring(0, 1))
.join("");
}
else if (username) {
return username.substring(0, 2);
}
return false;
}
get el() { return this; }
static get style() { return avatarCss; }
}, [1, "calcite-avatar", {
"scale": [513],
"thumbnail": [513],
"fullName": [513, "full-name"],
"username": [513],
"userId": [513, "user-id"],
"label": [1],
"thumbnailFailedToLoad": [32]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-avatar", "calcite-icon"];
components.forEach(tagName => { switch (tagName) {
case "calcite-avatar":
if (!customElements.get(tagName)) {
customElements.define(tagName, Avatar);
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
defineCustomElement();
export { Avatar as A, defineCustomElement as d };