avatar.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 { h } from "@stencil/core";
  7. import { isValidHex } from "../color-picker/utils";
  8. import { hexToHue, stringToHex } from "./utils";
  9. import { getThemeName } from "../../utils/dom";
  10. export class Avatar {
  11. constructor() {
  12. //--------------------------------------------------------------------------
  13. //
  14. // Properties
  15. //
  16. //--------------------------------------------------------------------------
  17. /** Specifies the size of the component. */
  18. this.scale = "m";
  19. //--------------------------------------------------------------------------
  20. //
  21. // Private State/Props
  22. //
  23. //--------------------------------------------------------------------------
  24. this.thumbnailFailedToLoad = false;
  25. }
  26. //--------------------------------------------------------------------------
  27. //
  28. // Lifecycle
  29. //
  30. //--------------------------------------------------------------------------
  31. render() {
  32. return this.determineContent();
  33. }
  34. //--------------------------------------------------------------------------
  35. //
  36. // Private Methods
  37. //
  38. //--------------------------------------------------------------------------
  39. determineContent() {
  40. if (this.thumbnail && !this.thumbnailFailedToLoad) {
  41. return (h("img", { alt: "", class: "thumbnail", onError: () => (this.thumbnailFailedToLoad = true), src: this.thumbnail }));
  42. }
  43. const initials = this.generateInitials();
  44. const backgroundColor = this.generateFillColor();
  45. 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 }))));
  46. }
  47. /**
  48. * Generate a valid background color that is consistent and unique to this user
  49. */
  50. generateFillColor() {
  51. const { userId, username, fullName, el } = this;
  52. const theme = getThemeName(el);
  53. const id = userId && `#${userId.substr(userId.length - 6)}`;
  54. const name = username || fullName || "";
  55. const hex = id && isValidHex(id) ? id : stringToHex(name);
  56. // if there is not unique information, or an invalid hex is produced, return a default
  57. if ((!userId && !name) || !isValidHex(hex)) {
  58. return `var(--calcite-ui-foreground-2)`;
  59. }
  60. const hue = hexToHue(hex);
  61. const l = theme === "dark" ? 20 : 90;
  62. return `hsl(${hue}, 60%, ${l}%)`;
  63. }
  64. /**
  65. * Use fullname or username to generate initials
  66. */
  67. generateInitials() {
  68. const { fullName, username } = this;
  69. if (fullName) {
  70. return fullName
  71. .trim()
  72. .split(" ")
  73. .map((name) => name.substring(0, 1))
  74. .join("");
  75. }
  76. else if (username) {
  77. return username.substring(0, 2);
  78. }
  79. return false;
  80. }
  81. static get is() { return "calcite-avatar"; }
  82. static get encapsulation() { return "shadow"; }
  83. static get originalStyleUrls() {
  84. return {
  85. "$": ["avatar.scss"]
  86. };
  87. }
  88. static get styleUrls() {
  89. return {
  90. "$": ["avatar.css"]
  91. };
  92. }
  93. static get properties() {
  94. return {
  95. "scale": {
  96. "type": "string",
  97. "mutable": false,
  98. "complexType": {
  99. "original": "Scale",
  100. "resolved": "\"l\" | \"m\" | \"s\"",
  101. "references": {
  102. "Scale": {
  103. "location": "import",
  104. "path": "../interfaces"
  105. }
  106. }
  107. },
  108. "required": false,
  109. "optional": false,
  110. "docs": {
  111. "tags": [],
  112. "text": "Specifies the size of the component."
  113. },
  114. "attribute": "scale",
  115. "reflect": true,
  116. "defaultValue": "\"m\""
  117. },
  118. "thumbnail": {
  119. "type": "string",
  120. "mutable": false,
  121. "complexType": {
  122. "original": "string",
  123. "resolved": "string",
  124. "references": {}
  125. },
  126. "required": false,
  127. "optional": false,
  128. "docs": {
  129. "tags": [],
  130. "text": "Specifies the `src` to an image (remember to add a token if the user is private)."
  131. },
  132. "attribute": "thumbnail",
  133. "reflect": true
  134. },
  135. "fullName": {
  136. "type": "string",
  137. "mutable": false,
  138. "complexType": {
  139. "original": "string",
  140. "resolved": "string",
  141. "references": {}
  142. },
  143. "required": false,
  144. "optional": false,
  145. "docs": {
  146. "tags": [],
  147. "text": "Specifies the full name of the user."
  148. },
  149. "attribute": "full-name",
  150. "reflect": true
  151. },
  152. "username": {
  153. "type": "string",
  154. "mutable": false,
  155. "complexType": {
  156. "original": "string",
  157. "resolved": "string",
  158. "references": {}
  159. },
  160. "required": false,
  161. "optional": false,
  162. "docs": {
  163. "tags": [],
  164. "text": "Specifies the username of the user."
  165. },
  166. "attribute": "username",
  167. "reflect": true
  168. },
  169. "userId": {
  170. "type": "string",
  171. "mutable": false,
  172. "complexType": {
  173. "original": "string",
  174. "resolved": "string",
  175. "references": {}
  176. },
  177. "required": false,
  178. "optional": false,
  179. "docs": {
  180. "tags": [],
  181. "text": "Specifies the unique id of the user."
  182. },
  183. "attribute": "user-id",
  184. "reflect": true
  185. }
  186. };
  187. }
  188. static get states() {
  189. return {
  190. "thumbnailFailedToLoad": {}
  191. };
  192. }
  193. static get elementRef() { return "el"; }
  194. }