calcite-avatar.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client';
  7. import { h as hexToRGB, i as isValidHex } from './utils.js';
  8. import { d as getThemeName } from './dom.js';
  9. import { d as defineCustomElement$2 } from './icon.js';
  10. /**
  11. * Convert a string to a valid hex by hashing its contents
  12. * and using the hash as a seed for three distinct color values
  13. */
  14. function stringToHex(str) {
  15. let hash = 0;
  16. for (let i = 0; i < str.length; i++) {
  17. hash = str.charCodeAt(i) + ((hash << 5) - hash);
  18. }
  19. let hex = "#";
  20. for (let j = 0; j < 3; j++) {
  21. const value = (hash >> (j * 8)) & 0xff;
  22. hex += ("00" + value.toString(16)).substr(-2);
  23. }
  24. return hex;
  25. }
  26. /**
  27. * Find the hue of a color given the separate RGB color channels
  28. */
  29. function rgbToHue(rgb) {
  30. let { r, g, b } = rgb;
  31. r /= 255;
  32. g /= 255;
  33. b /= 255;
  34. const max = Math.max(r, g, b);
  35. const min = Math.min(r, g, b);
  36. const delta = max - min;
  37. if (max === min) {
  38. return 0;
  39. }
  40. let hue = (max + min) / 2;
  41. switch (max) {
  42. case r:
  43. hue = (g - b) / delta + (g < b ? 6 : 0);
  44. break;
  45. case g:
  46. hue = (b - r) / delta + 2;
  47. break;
  48. case b:
  49. hue = (r - g) / delta + 4;
  50. break;
  51. }
  52. return Math.round(hue * 60);
  53. }
  54. /**
  55. * For a hex color, find the hue
  56. * @param hex {string} - form of "#------"
  57. */
  58. function hexToHue(hex) {
  59. return rgbToHue(hexToRGB(hex));
  60. }
  61. const avatarCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:inline-block;overflow:hidden;border-radius:50%}:host([scale=s]){height:1.5rem;width:1.5rem;font-size:var(--calcite-font-size--3)}:host([scale=m]){height:2rem;width:2rem;font-size:var(--calcite-font-size--2)}:host([scale=l]){height:2.75rem;width:2.75rem;font-size:var(--calcite-font-size-0)}.icon{display:-ms-flexbox;display:flex}.background{display:-ms-flexbox;display:flex;height:100%;width:100%;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;border-radius:50%}.initials{font-weight:var(--calcite-font-weight-bold);text-transform:uppercase;color:var(--calcite-ui-text-3)}.thumbnail{height:100%;width:100%;border-radius:50%}";
  62. const Avatar = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  63. constructor() {
  64. super();
  65. this.__registerHost();
  66. this.__attachShadow();
  67. //--------------------------------------------------------------------------
  68. //
  69. // Properties
  70. //
  71. //--------------------------------------------------------------------------
  72. /** specify the scale of the avatar, defaults to m */
  73. this.scale = "m";
  74. //--------------------------------------------------------------------------
  75. //
  76. // Private State/Props
  77. //
  78. //--------------------------------------------------------------------------
  79. /** True if thumnail fails to load */
  80. this.error = false;
  81. }
  82. //--------------------------------------------------------------------------
  83. //
  84. // Lifecycle
  85. //
  86. //--------------------------------------------------------------------------
  87. render() {
  88. return this.determineContent();
  89. }
  90. //--------------------------------------------------------------------------
  91. //
  92. // Private Methods
  93. //
  94. //--------------------------------------------------------------------------
  95. determineContent() {
  96. if (this.thumbnail && !this.error) {
  97. return (h("img", { alt: "", class: "thumbnail", onError: () => (this.error = true), src: this.thumbnail }));
  98. }
  99. const initials = this.generateInitials();
  100. const backgroundColor = this.generateFillColor();
  101. return (h("span", { class: "background", style: { backgroundColor } }, initials ? (h("span", { "aria-hidden": "true", class: "initials" }, initials)) : (h("calcite-icon", { class: "icon", icon: "user", scale: this.scale }))));
  102. }
  103. /**
  104. * Generate a valid background color that is consistent and unique to this user
  105. */
  106. generateFillColor() {
  107. const { userId, username, fullName, el } = this;
  108. const theme = getThemeName(el);
  109. const id = userId && `#${userId.substr(userId.length - 6)}`;
  110. const name = username || fullName || "";
  111. const hex = id && isValidHex(id) ? id : stringToHex(name);
  112. // if there is not unique information, or an invalid hex is produced, return a default
  113. if ((!userId && !name) || !isValidHex(hex)) {
  114. return `var(--calcite-ui-foreground-2)`;
  115. }
  116. const hue = hexToHue(hex);
  117. const l = theme === "dark" ? 20 : 90;
  118. return `hsl(${hue}, 60%, ${l}%)`;
  119. }
  120. /**
  121. * Use fullname or username to generate initials
  122. */
  123. generateInitials() {
  124. const { fullName, username } = this;
  125. if (fullName) {
  126. return fullName
  127. .trim()
  128. .split(" ")
  129. .map((name) => name.substring(0, 1))
  130. .join("");
  131. }
  132. else if (username) {
  133. return username.substring(0, 2);
  134. }
  135. return false;
  136. }
  137. get el() { return this; }
  138. static get style() { return avatarCss; }
  139. }, [1, "calcite-avatar", {
  140. "scale": [513],
  141. "thumbnail": [1],
  142. "fullName": [1, "full-name"],
  143. "username": [1],
  144. "userId": [1, "user-id"],
  145. "error": [32]
  146. }]);
  147. function defineCustomElement$1() {
  148. if (typeof customElements === "undefined") {
  149. return;
  150. }
  151. const components = ["calcite-avatar", "calcite-icon"];
  152. components.forEach(tagName => { switch (tagName) {
  153. case "calcite-avatar":
  154. if (!customElements.get(tagName)) {
  155. customElements.define(tagName, Avatar);
  156. }
  157. break;
  158. case "calcite-icon":
  159. if (!customElements.get(tagName)) {
  160. defineCustomElement$2();
  161. }
  162. break;
  163. } });
  164. }
  165. defineCustomElement$1();
  166. const CalciteAvatar = Avatar;
  167. const defineCustomElement = defineCustomElement$1;
  168. export { CalciteAvatar, defineCustomElement };