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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. <text
  71. dominantBaseline = 'central'
  72. fill = 'rgba(255,255,255,.6)'
  73. fontSize = '40pt'
  74. textAnchor = 'middle'
  75. x = '50'
  76. y = '50'>
  77. { initials }
  78. </text>
  79. </svg>
  80. </div>
  81. );
  82. }
  83. // default avatar
  84. return (
  85. <div className = { this._getBadgeClassName() }>
  86. <img
  87. className = { this._getAvatarClassName('defaultAvatar') }
  88. id = { this.props.id }
  89. src = { this.props.defaultAvatar || 'images/avatar.png' }
  90. style = { this._getAvatarStyle() } />
  91. </div>
  92. );
  93. }
  94. /**
  95. * Constructs a style object to be used on the avatars.
  96. *
  97. * @param {string?} color - The desired background color.
  98. * @returns {Object}
  99. */
  100. _getAvatarStyle(color) {
  101. const { size } = this.props;
  102. return {
  103. backgroundColor: color || undefined,
  104. fontSize: size ? size * 0.5 : '180%',
  105. height: size || '100%',
  106. width: size || '100%'
  107. };
  108. }
  109. /**
  110. * Constructs a list of class names required for the avatar component.
  111. *
  112. * @param {string} additional - Any additional class to add.
  113. * @returns {string}
  114. */
  115. _getAvatarClassName(additional) {
  116. return `avatar ${additional || ''} ${this.props.className || ''}`;
  117. }
  118. /**
  119. * Generates a class name to render a badge on the avatar, if necessary.
  120. *
  121. * @returns {string}
  122. */
  123. _getBadgeClassName() {
  124. const { status } = this.props;
  125. if (status) {
  126. return `avatar-badge avatar-badge-${status}`;
  127. }
  128. return '';
  129. }
  130. _isIcon: (?string | ?Object) => boolean
  131. }