avatar.js 1.4 KB

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