Skip to content

Instantly share code, notes, and snippets.

@rista404
Created May 21, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rista404/d763b9ce128b643343d624037271e204 to your computer and use it in GitHub Desktop.
Save rista404/d763b9ce128b643343d624037271e204 to your computer and use it in GitHub Desktop.
React wrapper for easy integration with clipboard.js (with Flow annotations)
// @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