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

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

type State = { hovered: boolean };

export default class Img extends Component<Props, 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>
    );
  }
}