You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatelessAvatar.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // @flow
  2. import { withStyles } from '@material-ui/core/styles';
  3. import clsx from 'clsx';
  4. import React from 'react';
  5. import { Icon } from '../../../icons';
  6. import AbstractStatelessAvatar, { type Props as AbstractProps } from '../AbstractStatelessAvatar';
  7. import { PRESENCE_AVAILABLE_COLOR, PRESENCE_AWAY_COLOR, PRESENCE_BUSY_COLOR, PRESENCE_IDLE_COLOR } from '../styles';
  8. type Props = AbstractProps & {
  9. /**
  10. * An object containing the CSS classes.
  11. */
  12. classes: Object,
  13. /**
  14. * External class name passed through props.
  15. */
  16. className?: string,
  17. /**
  18. * The default avatar URL if we want to override the app bundled one (e.g. AlwaysOnTop).
  19. */
  20. defaultAvatar?: string,
  21. /**
  22. * ID of the component to be rendered.
  23. */
  24. id?: string,
  25. /**
  26. * One of the expected status strings (e.g. 'available') to render a badge on the avatar, if necessary.
  27. */
  28. status?: ?string,
  29. /**
  30. * TestId of the element, if any.
  31. */
  32. testId?: string,
  33. /**
  34. * Indicates whether to load the avatar using CORS or not.
  35. */
  36. useCORS?: ?boolean
  37. };
  38. /**
  39. * Creates the styles for the component.
  40. *
  41. * @returns {Object}
  42. */
  43. const styles = () => {
  44. return {
  45. avatar: {
  46. backgroundColor: '#AAA',
  47. borderRadius: '50%',
  48. color: 'rgba(255, 255, 255, 1)',
  49. fontWeight: '100',
  50. objectFit: 'cover',
  51. textAlign: 'center',
  52. '&.avatar-small': {
  53. height: '28px !important',
  54. width: '28px !important'
  55. },
  56. '&.avatar-xsmall': {
  57. height: '16px !important',
  58. width: '16px !important'
  59. },
  60. '& .jitsi-icon': {
  61. transform: 'translateY(50%)'
  62. },
  63. '& .avatar-svg': {
  64. height: '100%',
  65. width: '100%'
  66. }
  67. },
  68. badge: {
  69. position: 'relative',
  70. '&.avatar-badge:after': {
  71. borderRadius: '50%',
  72. content: '""',
  73. display: 'block',
  74. height: '35%',
  75. position: 'absolute',
  76. bottom: 0,
  77. width: '35%'
  78. },
  79. '&.avatar-badge-available:after': {
  80. backgroundColor: PRESENCE_AVAILABLE_COLOR
  81. },
  82. '&.avatar-badge-away:after': {
  83. backgroundColor: PRESENCE_AWAY_COLOR
  84. },
  85. '&.avatar-badge-busy:after': {
  86. backgroundColor: PRESENCE_BUSY_COLOR
  87. },
  88. '&.avatar-badge-idle:after': {
  89. backgroundColor: PRESENCE_IDLE_COLOR
  90. }
  91. }
  92. };
  93. };
  94. /**
  95. * Implements a stateless avatar component that renders an avatar purely from what gets passed through
  96. * props.
  97. */
  98. class StatelessAvatar extends AbstractStatelessAvatar<Props> {
  99. /**
  100. * Instantiates a new {@code Component}.
  101. *
  102. * @inheritdoc
  103. */
  104. constructor(props: Props) {
  105. super(props);
  106. this._onAvatarLoadError = this._onAvatarLoadError.bind(this);
  107. }
  108. /**
  109. * Implements {@code Component#render}.
  110. *
  111. * @inheritdoc
  112. */
  113. render() {
  114. const { initials, url, useCORS } = this.props;
  115. if (this._isIcon(url)) {
  116. return (
  117. <div
  118. className = { clsx(this._getAvatarClassName(), this._getBadgeClassName()) }
  119. data-testid = { this.props.testId }
  120. id = { this.props.id }
  121. style = { this._getAvatarStyle(this.props.color) }>
  122. <Icon
  123. size = '50%'
  124. src = { url } />
  125. </div>
  126. );
  127. }
  128. if (url) {
  129. return (
  130. <div className = { this._getBadgeClassName() }>
  131. <img
  132. alt = 'avatar'
  133. className = { this._getAvatarClassName() }
  134. crossOrigin = { useCORS ? '' : undefined }
  135. data-testid = { this.props.testId }
  136. id = { this.props.id }
  137. onError = { this._onAvatarLoadError }
  138. src = { url }
  139. style = { this._getAvatarStyle() } />
  140. </div>
  141. );
  142. }
  143. if (initials) {
  144. return (
  145. <div
  146. className = { clsx(this._getAvatarClassName(), this._getBadgeClassName()) }
  147. data-testid = { this.props.testId }
  148. id = { this.props.id }
  149. style = { this._getAvatarStyle(this.props.color) }>
  150. <svg
  151. className = 'avatar-svg'
  152. viewBox = '0 0 100 100'
  153. xmlns = 'http://www.w3.org/2000/svg'
  154. xmlnsXlink = 'http://www.w3.org/1999/xlink'>
  155. <text
  156. dominantBaseline = 'central'
  157. fill = 'rgba(255,255,255,1)'
  158. fontSize = '40pt'
  159. textAnchor = 'middle'
  160. x = '50'
  161. y = '50'>
  162. { initials }
  163. </text>
  164. </svg>
  165. </div>
  166. );
  167. }
  168. // default avatar
  169. return (
  170. <div className = { this._getBadgeClassName() }>
  171. <img
  172. alt = 'avatar'
  173. className = { this._getAvatarClassName('defaultAvatar') }
  174. data-testid = { this.props.testId }
  175. id = { this.props.id }
  176. src = { this.props.defaultAvatar || 'images/avatar.png' }
  177. style = { this._getAvatarStyle() } />
  178. </div>
  179. );
  180. }
  181. /**
  182. * Constructs a style object to be used on the avatars.
  183. *
  184. * @param {string?} color - The desired background color.
  185. * @returns {Object}
  186. */
  187. _getAvatarStyle(color) {
  188. const { size } = this.props;
  189. return {
  190. background: color || undefined,
  191. fontSize: size ? size * 0.5 : '180%',
  192. height: size || '100%',
  193. width: size || '100%'
  194. };
  195. }
  196. /**
  197. * Constructs a list of class names required for the avatar component.
  198. *
  199. * @param {string} additional - Any additional class to add.
  200. * @returns {string}
  201. */
  202. _getAvatarClassName(additional) {
  203. return clsx('avatar', additional, this.props.className, this.props.classes.avatar);
  204. }
  205. /**
  206. * Generates a class name to render a badge on the avatar, if necessary.
  207. *
  208. * @returns {string}
  209. */
  210. _getBadgeClassName() {
  211. const { status } = this.props;
  212. if (status) {
  213. return clsx('avatar-badge', `avatar-badge-${status}`, this.props.classes.badge);
  214. }
  215. return '';
  216. }
  217. _isIcon: (?string | ?Object) => boolean;
  218. _onAvatarLoadError: () => void;
  219. /**
  220. * Handles avatar load errors.
  221. *
  222. * @returns {void}
  223. */
  224. _onAvatarLoadError() {
  225. const { onAvatarLoadError, onAvatarLoadErrorParams } = this.props;
  226. if (typeof onAvatarLoadError === 'function') {
  227. onAvatarLoadError(onAvatarLoadErrorParams);
  228. }
  229. }
  230. }
  231. export default withStyles(styles)(StatelessAvatar);