about summary refs log tree commit diff
path: root/app/javascript/mastodon/components/avatar.js
blob: 47f2715c783f21799780b9e06bc00d465c956351 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import PropTypes from 'prop-types';

class Avatar extends React.PureComponent {

  constructor (props, context) {
    super(props, context);

    this.state = {
      hovering: false
    };

    this.handleMouseEnter = this.handleMouseEnter.bind(this);
    this.handleMouseLeave = this.handleMouseLeave.bind(this);
  }

  handleMouseEnter () {
    if (this.props.animate) return;
    this.setState({ hovering: true });
  }

  handleMouseLeave () {
    if (this.props.animate) return;
    this.setState({ hovering: false });
  }

  render () {
    const { src, size, staticSrc, animate } = this.props;
    const { hovering } = this.state;

    const style = {
      ...this.props.style,
      width: `${size}px`,
      height: `${size}px`,
      backgroundSize: `${size}px ${size}px`
    };

    if (hovering || animate) {
      style.backgroundImage = `url(${src})`;
    } else {
      style.backgroundImage = `url(${staticSrc})`;
    }

    return (
      <div
        className='account__avatar'
        onMouseEnter={this.handleMouseEnter}
        onMouseLeave={this.handleMouseLeave}
        style={style}
      />
    );
  }

}

Avatar.propTypes = {
  src: PropTypes.string.isRequired,
  staticSrc: PropTypes.string,
  size: PropTypes.number.isRequired,
  style: PropTypes.object,
  animate: PropTypes.bool
};

Avatar.defaultProps = {
  animate: false
};

export default Avatar;