Created
May 21, 2018 14:39
-
-
Save rista404/d763b9ce128b643343d624037271e204 to your computer and use it in GitHub Desktop.
React wrapper for easy integration with clipboard.js (with Flow annotations)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import * as React from 'react' | |
import Clipboard, { type ClipboardType } from 'clipboard' | |
type Props = { | |
text: string, | |
onSuccess?: () => any, | |
children: React.Node, | |
} | |
export default class Copy extends React.PureComponent<Props> { | |
el: Element | |
clipboard: ClipboardType | |
onRef = (node: Element) => this.el = node | |
componentDidMount() { | |
this.clipboard = new Clipboard(this.el) | |
if(this.props.onSuccess) { | |
this.clipboard.on('success', this.props.onSuccess) | |
} | |
} | |
componentWillUnmount() { | |
this.clipboard.destroy() | |
} | |
render() { | |
const el = React.Children.only(this.props.children) | |
return React.cloneElement(el, { | |
ref: this.onRef, | |
'data-clipboard-text': this.props.text, | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment