diff --git a/app/components/clipboard.js b/app/components/clipboard.js new file mode 100644 index 0000000..bda14ca --- /dev/null +++ b/app/components/clipboard.js @@ -0,0 +1,50 @@ +// @flow +import React, { PureComponent } from 'react'; + +import { Button } from './button'; + +type Props = { + text: string, + className?: string, +}; + +type State = { copied: boolean }; + +export class Clipboard extends PureComponent { + static defaultProps = { + className: '', + }; + + state = { + copied: false, + }; + + handleClick = () => { + const { text } = this.props; + + const el = document.createElement('textarea'); + el.value = text; + + if (document.body) document.body.appendChild(el); + + el.select(); + document.execCommand('copy'); + if (document.body) document.body.removeChild(el); + + this.setState({ copied: true }); + }; + + render() { + const { className } = this.props; + const { copied } = this.state; + + return ( +