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
69 lines
1.4 KiB
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
|
||
8 years ago
|
class Avatar extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
constructor (props, context) {
|
||
|
super(props, context);
|
||
8 years ago
|
|
||
8 years ago
|
this.state = {
|
||
8 years ago
|
hovering: false
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
this.handleMouseEnter = this.handleMouseEnter.bind(this);
|
||
|
this.handleMouseLeave = this.handleMouseLeave.bind(this);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleMouseEnter () {
|
||
8 years ago
|
if (this.props.animate) return;
|
||
8 years ago
|
this.setState({ hovering: true });
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
handleMouseLeave () {
|
||
8 years ago
|
if (this.props.animate) return;
|
||
8 years ago
|
this.setState({ hovering: false });
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
render () {
|
||
8 years ago
|
const { src, size, staticSrc, animate } = this.props;
|
||
8 years ago
|
const { hovering } = this.state;
|
||
|
|
||
8 years ago
|
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})`;
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
return (
|
||
8 years ago
|
<div
|
||
8 years ago
|
className='account__avatar'
|
||
8 years ago
|
onMouseEnter={this.handleMouseEnter}
|
||
|
onMouseLeave={this.handleMouseLeave}
|
||
|
style={style}
|
||
|
/>
|
||
8 years ago
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
}
|
||
|
|
||
|
Avatar.propTypes = {
|
||
|
src: PropTypes.string.isRequired,
|
||
|
staticSrc: PropTypes.string,
|
||
|
size: PropTypes.number.isRequired,
|
||
|
style: PropTypes.object,
|
||
|
animate: PropTypes.bool
|
||
|
};
|
||
|
|
||
|
Avatar.defaultProps = {
|
||
|
animate: false
|
||
|
};
|
||
8 years ago
|
|
||
|
export default Avatar;
|