react-zeroclipboard
This is a wrapper around ZeroClipboard for use with React. ZeroClipboard has a difficult to work with api which is being abstracted from you. This library...
- asynchronusly loads zeroclipboard from cdnjs
- handles mounting/unmounting of components
- figures out which element was clicked
- allows you to declare text/html/rtf, or pass a function which returns it dynamically
Here's a simple example:
render: function(){
return (
<div>
<p>Click the button to copy some text</p>
<ReactZeroClipboard text="Hello, world!"><button>Copy</button></ReactZeroClipboard>
</div>
)
}
The full api offers more flexibility. If you provide e.g. html and text, they'll both be set and the application you're pasting into decides which one to use. Methods have higher priority than the literal strings, if for some reason you pass both.
The events can be used to show a success message by changing state, focusing an input, or
anything you want to happen. Generally you should use onAfterCopy
. The docs don't specify
when the error event occurs, or what can cause it.
<ReactZeroClipboard
text="text to copy"
html="<b>html to copy</b>"
richText="{\\rtf1\\ansi\n{\\b rich text to copy}}"
// optional
swfPath="http://user_defined_cdn_path/ZeroClipboard.swf"
getText={(Void -> String)}
getHtml={(Void -> String)}
getRichText={(Void -> String)}
onCopy={(Event -> Void)}
onAfterCopy={(Event -> Void)}
onErrorCopy={(Error -> Void)}
onReady={(Error -> Void)}
/>
Here's an example where we copy the current url to the clipboard, both in plain text and a html anchor
If the user pastes this in their address bar they get the url, and if they paste it in gmail they get a nice link.
render: function(){
return (
<div>
<p>Copy a link to this page</p>
<ReactZeroClipboard
getText={function(){ return location.href; }}
getHtml={function(){ return '<a href="' + location.href + '">My Page</a>'; }}>
<button>Copy</button>
</ReactZeroClipboard>
</div>
)
}
Here's a demo which shows how multiple types work. Press the copy button, and then try pasting it into each of the boxes below. one is a textarea, and the other is a contentEditable.
This is simply a button which logs the events as they occur.