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.

71 lines
1.5 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. export default class Avatar extends React.PureComponent {
  5. static propTypes = {
  6. account: ImmutablePropTypes.map.isRequired,
  7. size: PropTypes.number.isRequired,
  8. style: PropTypes.object,
  9. animate: PropTypes.bool,
  10. inline: PropTypes.bool,
  11. };
  12. static defaultProps = {
  13. animate: false,
  14. size: 20,
  15. inline: false,
  16. };
  17. state = {
  18. hovering: false,
  19. };
  20. handleMouseEnter = () => {
  21. if (this.props.animate) return;
  22. this.setState({ hovering: true });
  23. }
  24. handleMouseLeave = () => {
  25. if (this.props.animate) return;
  26. this.setState({ hovering: false });
  27. }
  28. render () {
  29. const { account, size, animate, inline } = this.props;
  30. const { hovering } = this.state;
  31. const src = account.get('avatar');
  32. const staticSrc = account.get('avatar_static');
  33. let className = 'account__avatar';
  34. if (inline) {
  35. className = className + ' account__avatar-inline';
  36. }
  37. const style = {
  38. ...this.props.style,
  39. width: `${size}px`,
  40. height: `${size}px`,
  41. backgroundSize: `${size}px ${size}px`,
  42. };
  43. if (hovering || animate) {
  44. style.backgroundImage = `url(${src})`;
  45. } else {
  46. style.backgroundImage = `url(${staticSrc})`;
  47. }
  48. return (
  49. <div
  50. className={className}
  51. onMouseEnter={this.handleMouseEnter}
  52. onMouseLeave={this.handleMouseLeave}
  53. style={style}
  54. />
  55. );
  56. }
  57. }