calcite-avatar.entry.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 { r as registerInstance, h, g as getElement } from './index-8ece2564.js';
  7. import { h as hexToRGB, i as isValidHex } from './utils-040a2a22.js';
  8. import { b as getThemeName } from './dom-da697a3f.js';
  9. import './guid-b4461004.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 = class {
  63. constructor(hostRef) {
  64. registerInstance(this, hostRef);
  65. //--------------------------------------------------------------------------
  66. //
  67. // Properties
  68. //
  69. //--------------------------------------------------------------------------
  70. /** specify the scale of the avatar, defaults to m */
  71. this.scale = "m";
  72. //--------------------------------------------------------------------------
  73. //
  74. // Private State/Props
  75. //
  76. //--------------------------------------------------------------------------
  77. /** True if thumnail fails to load */
  78. this.error = false;
  79. }
  80. //--------------------------------------------------------------------------
  81. //
  82. // Lifecycle
  83. //
  84. //--------------------------------------------------------------------------
  85. render() {
  86. return this.determineContent();
  87. }
  88. //--------------------------------------------------------------------------
  89. //
  90. // Private Methods
  91. //
  92. //--------------------------------------------------------------------------
  93. determineContent() {
  94. if (this.thumbnail && !this.error) {
  95. return (h("img", { alt: "", class: "thumbnail", onError: () => (this.error = true), src: this.thumbnail }));
  96. }
  97. const initials = this.generateInitials();
  98. const backgroundColor = this.generateFillColor();
  99. 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 }))));
  100. }
  101. /**
  102. * Generate a valid background color that is consistent and unique to this user
  103. */
  104. generateFillColor() {
  105. const { userId, username, fullName, el } = this;
  106. const theme = getThemeName(el);
  107. const id = userId && `#${userId.substr(userId.length - 6)}`;
  108. const name = username || fullName || "";
  109. const hex = id && isValidHex(id) ? id : stringToHex(name);
  110. // if there is not unique information, or an invalid hex is produced, return a default
  111. if ((!userId && !name) || !isValidHex(hex)) {
  112. return `var(--calcite-ui-foreground-2)`;
  113. }
  114. const hue = hexToHue(hex);
  115. const l = theme === "dark" ? 20 : 90;
  116. return `hsl(${hue}, 60%, ${l}%)`;
  117. }
  118. /**
  119. * Use fullname or username to generate initials
  120. */
  121. generateInitials() {
  122. const { fullName, username } = this;
  123. if (fullName) {
  124. return fullName
  125. .trim()
  126. .split(" ")
  127. .map((name) => name.substring(0, 1))
  128. .join("");
  129. }
  130. else if (username) {
  131. return username.substring(0, 2);
  132. }
  133. return false;
  134. }
  135. get el() { return getElement(this); }
  136. };
  137. Avatar.style = avatarCss;
  138. export { Avatar as calcite_avatar };