aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/components/ui/copy_icon.tsx
blob: f8abaa59ec354ffca5e1ce29a48dcde2a2396b63 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as _ from 'lodash';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as CopyToClipboard from 'react-copy-to-clipboard';
import {colors} from 'material-ui/styles';
import ReactTooltip = require('react-tooltip');

interface CopyIconProps {
    data: string;
    callToAction?: string;
}

interface CopyIconState {
    isHovering: boolean;
}

export class CopyIcon extends React.Component<CopyIconProps, CopyIconState> {
    private copyTooltipTimeoutId: number;
    private copyable: HTMLInputElement;
    constructor(props: CopyIconProps) {
        super(props);
        this.state = {
            isHovering: false,
        };
    }
    public componentDidUpdate() {
        // Remove tooltip if hover away
        if (!this.state.isHovering && this.copyTooltipTimeoutId) {
            clearInterval(this.copyTooltipTimeoutId);
            this.hideTooltip();
        }
    }
    public render() {
        return (
            <div className="inline-block">
                    <CopyToClipboard text={this.props.data} onCopy={this.onCopy.bind(this)}>
                        <div
                            className="inline flex"
                            style={{cursor: 'pointer', color: colors.amber600}}
                            ref={this.setRefToProperty.bind(this)}
                            data-tip={true}
                            data-for="copy"
                            data-event="click"
                            data-iscapture={true} // This let's the click event continue to propogate
                            onMouseOver={this.setHoverState.bind(this, true)}
                            onMouseOut={this.setHoverState.bind(this, false)}
                        >
                            <div>
                                <i style={{fontSize: 15}} className="zmdi zmdi-copy" />
                            </div>
                            {this.props.callToAction &&
                                <div className="pl1">{this.props.callToAction}</div>
                            }
                        </div>
                    </CopyToClipboard>
                <ReactTooltip id="copy">Copied!</ReactTooltip>
            </div>
        );
    }
    private setRefToProperty(el: HTMLInputElement) {
        this.copyable = el;
    }
    private setHoverState(isHovering: boolean) {
        this.setState({
            isHovering,
        });
    }
    private onCopy() {
        if (this.copyTooltipTimeoutId) {
            clearInterval(this.copyTooltipTimeoutId);
        }

        const tooltipLifespanMs = 1000;
        this.copyTooltipTimeoutId = window.setTimeout(() => {
            this.hideTooltip();
        }, tooltipLifespanMs);
    }
    private hideTooltip() {
        ReactTooltip.hide(ReactDOM.findDOMNode(this.copyable));
    }
}