import React from 'react'; import PropTypes from 'prop-types'; import { Tooltip, OverlayTrigger, MenuItem } from 'react-bootstrap'; const propTypes = { copyNode: PropTypes.node, getText: PropTypes.func, onCopyEnd: PropTypes.func, shouldShowText: PropTypes.bool, text: PropTypes.string, inMenu: PropTypes.bool, tooltipText: PropTypes.string, }; const defaultProps = { copyNode: Copy, onCopyEnd: () => {}, shouldShowText: true, inMenu: false, tooltipText: 'Copy to clipboard', }; export default class CopyToClipboard extends React.Component { constructor(props) { super(props); this.state = { hasCopied: false, }; // bindings this.copyToClipboard = this.copyToClipboard.bind(this); this.resetTooltipText = this.resetTooltipText.bind(this); this.onMouseOut = this.onMouseOut.bind(this); } onMouseOut() { // delay to avoid flash of text change on tooltip setTimeout(this.resetTooltipText, 200); } onClick() { if (this.props.getText) { this.props.getText((d) => { this.copyToClipboard(d); }); } else { this.copyToClipboard(this.props.text); } } resetTooltipText() { this.setState({ hasCopied: false }); } copyToClipboard(textToCopy) { const textArea = document.createElement('textarea'); textArea.style.position = 'fixed'; textArea.style.left = '-1000px'; textArea.value = textToCopy; document.body.appendChild(textArea); textArea.select(); try { if (!document.execCommand('copy')) { throw new Error('Not successful'); } } catch (err) { window.alert('Sorry, your browser does not support copying. Use Ctrl / Cmd + C!'); // eslint-disable-line } document.body.removeChild(textArea); this.setState({ hasCopied: true }); this.props.onCopyEnd(); } tooltipText() { if (this.state.hasCopied) { return 'Copied!'; } return this.props.tooltipText; } renderLink() { return ( {this.props.shouldShowText && {this.props.text}      } {this.props.copyNode} ); } renderInMenu() { return ( {this.props.copyNode} ); } renderTooltip() { return ( {this.tooltipText()} ); } render() { let html; if (this.props.inMenu) { html = this.renderInMenu(); } else { html = this.renderLink(); } return html; } } CopyToClipboard.propTypes = propTypes; CopyToClipboard.defaultProps = defaultProps;