summaryrefslogtreecommitdiffhomepage
path: root/app/components/Img.js
blob: d124eb7f49203197acc97f3908c52925bed9e04b (plain)
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
// @flow
import * as React from 'react';
import { View, Component, Types } from 'reactxp';

type ImgProps = {
    source: string,
    tintColor?: string,
    hoverStyle?: Types.ViewStyle,
    disabled?: boolean,
  };

type State = { hovered: boolean };

export default class Img extends Component<ImgProps, State> {

  state = { hovered: false };

  onHoverStart = () => !this.props.disabled ? this.setState({ hovered: true }) : null;
  onHoverEnd = () => !this.props.disabled ? this.setState({ hovered: false }) : null;

  getHoverStyle = () => this.state.hovered ? this.props.hoverStyle || null : null;

  render() {
    const { source, style, onMouseEnter, onMouseLeave, ...otherProps } = this.props;
    const tintColor = this.props.tintColor;
    const url = './assets/images/' + source + '.svg';
    let image;

    if(tintColor) {
      image = (
        <div style={{
          WebkitMaskImage: `url('${url}')`,
          WebkitMaskRepeat: 'no-repeat',
          backgroundColor: tintColor,
          lineHeight: 0,
        }}>
          <img src={ url } style={{
            visibility: 'hidden',
          }} />
        </div>
      );
    } else {
      image = (
        <img src={ url } />
      );
    }

    return (
      <View { ...otherProps }
        onMouseEnter={onMouseEnter || this.onHoverStart}
        onMouseLeave={onMouseLeave || this.onHoverEnd}
        style={[style, this.getHoverStyle()]}>
        { image }
      </View>);
  }
}