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.7 KiB

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