calcite-avatar.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.97
  5. */
  6. import { proxyCustomElement, HTMLElement, h } from '@stencil/core/internal/client/index.js';
  7. import { h as hexToRGB, i as isValidHex } from './utils.js';
  8. import { e 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. * @param str
  15. */
  16. function stringToHex(str) {
  17. let hash = 0;
  18. for (let i = 0; i < str.length; i++) {
  19. hash = str.charCodeAt(i) + ((hash << 5) - hash);
  20. }
  21. let hex = "#";
  22. for (let j = 0; j < 3; j++) {
  23. const value = (hash >> (j * 8)) & 0xff;
  24. hex += ("00" + value.toString(16)).substr(-2);
  25. }
  26. return hex;
  27. }
  28. /**
  29. * Find the hue of a color given the separate RGB color channels
  30. *
  31. * @param rgb
  32. */
  33. function rgbToHue(rgb) {
  34. let { r, g, b } = rgb;
  35. r /= 255;
  36. g /= 255;
  37. b /= 255;
  38. const max = Math.max(r, g, b);
  39. const min = Math.min(r, g, b);
  40. const delta = max - min;
  41. if (max === min) {
  42. return 0;
  43. }
  44. let hue = (max + min) / 2;
  45. switch (max) {
  46. case r:
  47. hue = (g - b) / delta + (g < b ? 6 : 0);
  48. break;
  49. case g:
  50. hue = (b - r) / delta + 2;
  51. break;
  52. case b:
  53. hue = (r - g) / delta + 4;
  54. break;
  55. }
  56. return Math.round(hue * 60);
  57. }
  58. /**
  59. * For a hex color, find the hue
  60. *
  61. * @param hex {string} - form of "#------"
  62. */
  63. function hexToHue(hex) {
  64. return rgbToHue(hexToRGB(hex));
  65. }
  66. 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-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-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)}: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-3)}.thumbnail{block-size:100%;inline-size:100%;border-radius:50%}";
  67. const Avatar = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  68. constructor() {
  69. super();
  70. this.__registerHost();
  71. this.__attachShadow();
  72. //--------------------------------------------------------------------------
  73. //
  74. // Properties
  75. //
  76. //--------------------------------------------------------------------------
  77. /** Specifies the size of the component. */
  78. this.scale = "m";
  79. //--------------------------------------------------------------------------
  80. //
  81. // Private State/Props
  82. //
  83. //--------------------------------------------------------------------------
  84. this.thumbnailFailedToLoad = false;
  85. }
  86. //--------------------------------------------------------------------------
  87. //
  88. // Lifecycle
  89. //
  90. //--------------------------------------------------------------------------
  91. render() {
  92. return this.determineContent();
  93. }
  94. //--------------------------------------------------------------------------
  95. //
  96. // Private Methods
  97. //
  98. //--------------------------------------------------------------------------
  99. determineContent() {
  100. if (this.thumbnail && !this.thumbnailFailedToLoad) {
  101. return (h("img", { alt: "", class: "thumbnail", onError: () => (this.thumbnailFailedToLoad = true), src: this.thumbnail }));
  102. }
  103. const initials = this.generateInitials();
  104. const backgroundColor = this.generateFillColor();
  105. 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 }))));
  106. }
  107. /**
  108. * Generate a valid background color that is consistent and unique to this user
  109. */
  110. generateFillColor() {
  111. const { userId, username, fullName, el } = this;
  112. const theme = getThemeName(el);
  113. const id = userId && `#${userId.substr(userId.length - 6)}`;
  114. const name = username || fullName || "";
  115. const hex = id && isValidHex(id) ? id : stringToHex(name);
  116. // if there is not unique information, or an invalid hex is produced, return a default
  117. if ((!userId && !name) || !isValidHex(hex)) {
  118. return `var(--calcite-ui-foreground-2)`;
  119. }
  120. const hue = hexToHue(hex);
  121. const l = theme === "dark" ? 20 : 90;
  122. return `hsl(${hue}, 60%, ${l}%)`;
  123. }
  124. /**
  125. * Use fullname or username to generate initials
  126. */
  127. generateInitials() {
  128. const { fullName, username } = this;
  129. if (fullName) {
  130. return fullName
  131. .trim()
  132. .split(" ")
  133. .map((name) => name.substring(0, 1))
  134. .join("");
  135. }
  136. else if (username) {
  137. return username.substring(0, 2);
  138. }
  139. return false;
  140. }
  141. get el() { return this; }
  142. static get style() { return avatarCss; }
  143. }, [1, "calcite-avatar", {
  144. "scale": [513],
  145. "thumbnail": [513],
  146. "fullName": [513, "full-name"],
  147. "username": [513],
  148. "userId": [513, "user-id"],
  149. "thumbnailFailedToLoad": [32]
  150. }]);
  151. function defineCustomElement$1() {
  152. if (typeof customElements === "undefined") {
  153. return;
  154. }
  155. const components = ["calcite-avatar", "calcite-icon"];
  156. components.forEach(tagName => { switch (tagName) {
  157. case "calcite-avatar":
  158. if (!customElements.get(tagName)) {
  159. customElements.define(tagName, Avatar);
  160. }
  161. break;
  162. case "calcite-icon":
  163. if (!customElements.get(tagName)) {
  164. defineCustomElement$2();
  165. }
  166. break;
  167. } });
  168. }
  169. defineCustomElement$1();
  170. const CalciteAvatar = Avatar;
  171. const defineCustomElement = defineCustomElement$1;
  172. export { CalciteAvatar, defineCustomElement };