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.

63 lines
1.3 KiB

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