From 3cf4d058f74a605109cdc8403bf683ac869ade37 Mon Sep 17 00:00:00 2001 From: George Lima Date: Sat, 12 Jan 2019 16:29:50 -0300 Subject: [PATCH] feature(clipboard): add Clipboard button component --- app/components/clipboard.js | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 app/components/clipboard.js 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 ( +