calcite-avatar.entry.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 { r as registerInstance, h, g as getElement } from './index-1f9b54dc.js';
  7. import { h as hexToRGB, i as isValidHex } from './utils-85948aeb.js';
  8. import { d as getThemeName } from './dom-8f0a9ff2.js';
  9. import './resources-9c476cb6.js';
  10. import './guid-9f15e57a.js';
  11. /**
  12. * Convert a string to a valid hex by hashing its contents
  13. * and using the hash as a seed for three distinct color values
  14. *
  15. * @param str
  16. */
  17. function stringToHex(str) {
  18. let hash = 0;
  19. for (let i = 0; i < str.length; i++) {
  20. hash = str.charCodeAt(i) + ((hash << 5) - hash);
  21. }
  22. let hex = "#";
  23. for (let j = 0; j < 3; j++) {
  24. const value = (hash >> (j * 8)) & 0xff;
  25. hex += ("00" + value.toString(16)).substr(-2);
  26. }
  27. return hex;
  28. }
  29. /**
  30. * Find the hue of a color given the separate RGB color channels
  31. *
  32. * @param rgb
  33. */
  34. function rgbToHue(rgb) {
  35. let { r, g, b } = rgb;
  36. r /= 255;
  37. g /= 255;
  38. b /= 255;
  39. const max = Math.max(r, g, b);
  40. const min = Math.min(r, g, b);
  41. const delta = max - min;
  42. if (max === min) {
  43. return 0;
  44. }
  45. let hue = (max + min) / 2;
  46. switch (max) {
  47. case r:
  48. hue = (g - b) / delta + (g < b ? 6 : 0);
  49. break;
  50. case g:
  51. hue = (b - r) / delta + 2;
  52. break;
  53. case b:
  54. hue = (r - g) / delta + 4;
  55. break;
  56. }
  57. return Math.round(hue * 60);
  58. }
  59. /**
  60. * For a hex color, find the hue
  61. *
  62. * @param hex {string} - form of "#------"
  63. */
  64. function hexToHue(hex) {
  65. return rgbToHue(hexToRGB(hex));
  66. }
  67. 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%}";
  68. const Avatar = class {
  69. constructor(hostRef) {
  70. registerInstance(this, hostRef);
  71. //--------------------------------------------------------------------------
  72. //
  73. // Properties
  74. //
  75. //--------------------------------------------------------------------------
  76. /** Specifies the size of the component. */
  77. this.scale = "m";
  78. //--------------------------------------------------------------------------
  79. //
  80. // Private State/Props
  81. //
  82. //--------------------------------------------------------------------------
  83. this.thumbnailFailedToLoad = false;
  84. }
  85. //--------------------------------------------------------------------------
  86. //
  87. // Lifecycle
  88. //
  89. //--------------------------------------------------------------------------
  90. render() {
  91. return this.determineContent();
  92. }
  93. //--------------------------------------------------------------------------
  94. //
  95. // Private Methods
  96. //
  97. //--------------------------------------------------------------------------
  98. determineContent() {
  99. if (this.thumbnail && !this.thumbnailFailedToLoad) {
  100. return (h("img", { alt: "", class: "thumbnail", onError: () => (this.thumbnailFailedToLoad = true), src: this.thumbnail }));
  101. }
  102. const initials = this.generateInitials();
  103. const backgroundColor = this.generateFillColor();
  104. 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 }))));
  105. }
  106. /**
  107. * Generate a valid background color that is consistent and unique to this user
  108. */
  109. generateFillColor() {
  110. const { userId, username, fullName, el } = this;
  111. const theme = getThemeName(el);
  112. const id = userId && `#${userId.substr(userId.length - 6)}`;
  113. const name = username || fullName || "";
  114. const hex = id && isValidHex(id) ? id : stringToHex(name);
  115. // if there is not unique information, or an invalid hex is produced, return a default
  116. if ((!userId && !name) || !isValidHex(hex)) {
  117. return `var(--calcite-ui-foreground-2)`;
  118. }
  119. const hue = hexToHue(hex);
  120. const l = theme === "dark" ? 20 : 90;
  121. return `hsl(${hue}, 60%, ${l}%)`;
  122. }
  123. /**
  124. * Use fullname or username to generate initials
  125. */
  126. generateInitials() {
  127. const { fullName, username } = this;
  128. if (fullName) {
  129. return fullName
  130. .trim()
  131. .split(" ")
  132. .map((name) => name.substring(0, 1))
  133. .join("");
  134. }
  135. else if (username) {
  136. return username.substring(0, 2);
  137. }
  138. return false;
  139. }
  140. get el() { return getElement(this); }
  141. };
  142. Avatar.style = avatarCss;
  143. export { Avatar as calcite_avatar };