闭社主体 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.

77 lines
1.6 KiB

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