The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

69 lines
1.4 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 } = this.props;
  22. const { hovering } = this.state;
  23. const style = {
  24. ...this.props.style,
  25. width: `${size}px`,
  26. height: `${size}px`,
  27. backgroundSize: `${size}px ${size}px`
  28. };
  29. if (hovering || animate) {
  30. style.backgroundImage = `url(${src})`;
  31. } else {
  32. style.backgroundImage = `url(${staticSrc})`;
  33. }
  34. return (
  35. <div
  36. className='account__avatar'
  37. onMouseEnter={this.handleMouseEnter}
  38. onMouseLeave={this.handleMouseLeave}
  39. style={style}
  40. />
  41. );
  42. }
  43. }
  44. Avatar.propTypes = {
  45. src: PropTypes.string.isRequired,
  46. staticSrc: PropTypes.string,
  47. size: PropTypes.number.isRequired,
  48. style: PropTypes.object,
  49. animate: PropTypes.bool
  50. };
  51. Avatar.defaultProps = {
  52. animate: false
  53. };
  54. export default Avatar;