闭社主体 forked from https://github.com/tootsuite/mastodon
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.

70 lines
1.4 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. class Avatar extends React.PureComponent {
  4. static propTypes = {
  5. src: PropTypes.string.isRequired,
  6. staticSrc: PropTypes.string,
  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: true
  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 { src, size, staticSrc, animate, inline } = this.props;
  30. const { hovering } = this.state;
  31. let className = 'account__avatar';
  32. if (inline) {
  33. className = className + ' account__avatar-inline';
  34. }
  35. const style = {
  36. ...this.props.style,
  37. width: `${size}px`,
  38. height: `${size}px`,
  39. backgroundSize: `${size}px ${size}px`
  40. };
  41. if (hovering || animate) {
  42. style.backgroundImage = `url(${src})`;
  43. } else {
  44. style.backgroundImage = `url(${staticSrc})`;
  45. }
  46. return (
  47. <div
  48. className={className}
  49. onMouseEnter={this.handleMouseEnter}
  50. onMouseLeave={this.handleMouseLeave}
  51. style={style}
  52. />
  53. );
  54. }
  55. }
  56. export default Avatar;