import * as _ from 'lodash'; import * as React from 'react'; import * as CopyToClipboard from 'react-copy-to-clipboard'; import * as ReactDOM from 'react-dom'; import ReactTooltip = require('react-tooltip'); import { colors } from 'ts/utils/colors'; interface CopyIconProps { data: string; callToAction?: string; } interface CopyIconState { isHovering: boolean; } export class CopyIcon extends React.Component { 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 (
{this.props.callToAction &&
{this.props.callToAction}
}
Copied!
); } 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)); } }