calcite-avatar.cjs.entry.js 6.3 KB

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