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.tsx 7.3KB

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