calcite-avatar.cjs.entry.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-5c65e149.js');
  9. const utils = require('./utils-51c079f6.js');
  10. const dom = require('./dom-9ac0341c.js');
  11. require('./guid-8b6d6cb4.js');
  12. /**
  13. * Convert a string to a valid hex by hashing its contents
  14. * and using the hash as a seed for three distinct color values
  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. function rgbToHue(rgb) {
  32. let { r, g, b } = rgb;
  33. r /= 255;
  34. g /= 255;
  35. b /= 255;
  36. const max = Math.max(r, g, b);
  37. const min = Math.min(r, g, b);
  38. const delta = max - min;
  39. if (max === min) {
  40. return 0;
  41. }
  42. let hue = (max + min) / 2;
  43. switch (max) {
  44. case r:
  45. hue = (g - b) / delta + (g < b ? 6 : 0);
  46. break;
  47. case g:
  48. hue = (b - r) / delta + 2;
  49. break;
  50. case b:
  51. hue = (r - g) / delta + 4;
  52. break;
  53. }
  54. return Math.round(hue * 60);
  55. }
  56. /**
  57. * For a hex color, find the hue
  58. * @param hex {string} - form of "#------"
  59. */
  60. function hexToHue(hex) {
  61. return rgbToHue(utils.hexToRGB(hex));
  62. }
  63. 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%}";
  64. const Avatar = class {
  65. constructor(hostRef) {
  66. index.registerInstance(this, hostRef);
  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 (index.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 (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 }))));
  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 = dom.getThemeName(el);
  109. const id = userId && `#${userId.substr(userId.length - 6)}`;
  110. const name = username || fullName || "";
  111. const hex = id && utils.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) || !utils.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 index.getElement(this); }
  138. };
  139. Avatar.style = avatarCss;
  140. exports.calcite_avatar = Avatar;