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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /**
  20. * Implements a stateless avatar component that renders an avatar purely from what gets passed through
  21. * props.
  22. */
  23. export default class StatelessAvatar extends AbstractStatelessAvatar<Props> {
  24. /**
  25. * Implements {@code Component#render}.
  26. *
  27. * @inheritdoc
  28. */
  29. render() {
  30. const { initials, url } = this.props;
  31. if (this._isIcon(url)) {
  32. return (
  33. <div
  34. className = { this._getAvatarClassName() }
  35. id = { this.props.id }
  36. style = { this._getAvatarStyle(this.props.color) }>
  37. <Icon
  38. size = '50%'
  39. src = { url } />
  40. </div>
  41. );
  42. }
  43. if (url) {
  44. return (
  45. <img
  46. className = { this._getAvatarClassName() }
  47. id = { this.props.id }
  48. onError = { this.props.onAvatarLoadError }
  49. src = { url }
  50. style = { this._getAvatarStyle() } />
  51. );
  52. }
  53. if (initials) {
  54. return (
  55. <div
  56. className = { this._getAvatarClassName() }
  57. id = { this.props.id }
  58. style = { this._getAvatarStyle(this.props.color) }>
  59. <svg
  60. className = 'avatar-svg'
  61. viewBox = '0 0 100 100'
  62. xmlns = 'http://www.w3.org/2000/svg'
  63. xmlnsXlink = 'http://www.w3.org/1999/xlink'>
  64. <foreignObject
  65. height = '100%'
  66. width = '100%'>
  67. <span
  68. className = 'avatar-foreign'>
  69. { initials }
  70. </span>
  71. </foreignObject>
  72. </svg>
  73. </div>
  74. );
  75. }
  76. // default avatar
  77. return (
  78. <img
  79. className = { this._getAvatarClassName('defaultAvatar') }
  80. id = { this.props.id }
  81. src = { this.props.defaultAvatar || 'images/avatar.png' }
  82. style = { this._getAvatarStyle() } />
  83. );
  84. }
  85. /**
  86. * Constructs a style object to be used on the avatars.
  87. *
  88. * @param {string?} color - The desired background color.
  89. * @returns {Object}
  90. */
  91. _getAvatarStyle(color) {
  92. const { size } = this.props;
  93. return {
  94. backgroundColor: color || undefined,
  95. fontSize: size ? size * 0.5 : '180%',
  96. height: size || '100%',
  97. width: size || '100%'
  98. };
  99. }
  100. /**
  101. * Constructs a list of class names required for the avatar component.
  102. *
  103. * @param {string} additional - Any additional class to add.
  104. * @returns {string}
  105. */
  106. _getAvatarClassName(additional) {
  107. return `avatar ${additional || ''} ${this.props.className || ''}`;
  108. }
  109. _isIcon: (?string | ?Object) => boolean
  110. }