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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // @flow
  2. import React from 'react';
  3. import { Icon } from '../../../icons';
  4. import AbstractStatelessAvatar, { type Props as AbstractProps } from '../AbstractStatelessAvatar';
  5. type Props = AbstractProps & {
  6. /**
  7. * External class name passed through props.
  8. */
  9. className?: string,
  10. /**
  11. * The default avatar URL if we want to override the app bundled one (e.g. AlwaysOnTop)
  12. */
  13. defaultAvatar?: string,
  14. /**
  15. * ID of the component to be rendered.
  16. */
  17. id?: string,
  18. /**
  19. * One of the expected status strings (e.g. 'available') to render a badge on the avatar, if necessary.
  20. */
  21. status?: ?string
  22. };
  23. /**
  24. * Implements a stateless avatar component that renders an avatar purely from what gets passed through
  25. * props.
  26. */
  27. export default class StatelessAvatar extends AbstractStatelessAvatar<Props> {
  28. /**
  29. * Implements {@code Component#render}.
  30. *
  31. * @inheritdoc
  32. */
  33. render() {
  34. const { initials, url } = this.props;
  35. if (this._isIcon(url)) {
  36. return (
  37. <div
  38. className = { `${this._getAvatarClassName()} ${this._getBadgeClassName()}` }
  39. id = { this.props.id }
  40. style = { this._getAvatarStyle(this.props.color) }>
  41. <Icon
  42. size = '50%'
  43. src = { url } />
  44. </div>
  45. );
  46. }
  47. if (url) {
  48. return (
  49. <div className = { this._getBadgeClassName() }>
  50. <img
  51. className = { this._getAvatarClassName() }
  52. id = { this.props.id }
  53. onError = { this.props.onAvatarLoadError }
  54. src = { url }
  55. style = { this._getAvatarStyle() } />
  56. </div>
  57. );
  58. }
  59. if (initials) {
  60. return (
  61. <div
  62. className = { `${this._getAvatarClassName()} ${this._getBadgeClassName()}` }
  63. id = { this.props.id }
  64. style = { this._getAvatarStyle(this.props.color) }>
  65. <svg
  66. className = 'avatar-svg'
  67. viewBox = '0 0 100 100'
  68. xmlns = 'http://www.w3.org/2000/svg'
  69. xmlnsXlink = 'http://www.w3.org/1999/xlink'>
  70. <foreignObject
  71. height = '100%'
  72. width = '100%'>
  73. <span
  74. className = 'avatar-foreign'>
  75. { initials }
  76. </span>
  77. </foreignObject>
  78. </svg>
  79. </div>
  80. );
  81. }
  82. // default avatar
  83. return (
  84. <div className = { this._getBadgeClassName() }>
  85. <img
  86. className = { this._getAvatarClassName('defaultAvatar') }
  87. id = { this.props.id }
  88. src = { this.props.defaultAvatar || 'images/avatar.png' }
  89. style = { this._getAvatarStyle() } />
  90. </div>
  91. );
  92. }
  93. /**
  94. * Constructs a style object to be used on the avatars.
  95. *
  96. * @param {string?} color - The desired background color.
  97. * @returns {Object}
  98. */
  99. _getAvatarStyle(color) {
  100. const { size } = this.props;
  101. return {
  102. backgroundColor: color || undefined,
  103. fontSize: size ? size * 0.5 : '180%',
  104. height: size || '100%',
  105. width: size || '100%'
  106. };
  107. }
  108. /**
  109. * Constructs a list of class names required for the avatar component.
  110. *
  111. * @param {string} additional - Any additional class to add.
  112. * @returns {string}
  113. */
  114. _getAvatarClassName(additional) {
  115. return `avatar ${additional || ''} ${this.props.className || ''}`;
  116. }
  117. /**
  118. * Generates a class name to render a badge on the avatar, if necessary.
  119. *
  120. * @returns {string}
  121. */
  122. _getBadgeClassName() {
  123. const { status } = this.props;
  124. if (status) {
  125. return `avatar-badge avatar-badge-${status}`;
  126. }
  127. return '';
  128. }
  129. _isIcon: (?string | ?Object) => boolean
  130. }