react-copy-html-to-clipboard
Copy text/html to clipboard React component
Forked from react-copy-to-clipboard
Based on copy-html-to-clipboard
Would try to use execCommand with fallback to IE specific clipboardData interface and finally, fallback to simple prompt with proper text content & 'Copy to clipboard: Ctrl+C, Enter'
Installation
NPM
npm install --save react react-copy-html-to-clipboard
Don't forget to manually install peer dependencies (react
) if you use npm@3.
Bower:
bower install --save https://unpkg.com/react-copy-to-clipboard/bower.zip
or in bower.json
then include as
1998 Script Tag:
(Module exposed as `CopyToClipboard`)
Demo
http://nkbt.github.io/react-copy-to-clipboard
Codepen demo
http://codepen.io/nkbt/pen/eNPoQv
Usage
;;; Component state = value: '' copied: false { return <div> <input value=thisstatevalue onChange= this /> <CopyToClipboard text=thisstatevalue onCopy= this> <span>Copy to clipboard with span</span> </CopyToClipboard> <CopyToClipboard text= thisstatevalue onCopy= this> <button>Copy to clipboard with button</button> </CopyToClipboard> thisstatecopied ? <span style=color: 'red'>Copied</span> : null </div> ; } const appRoot = document;documentbody;ReactDOM;
Options
text
: PropTypes.any.isRequired
Text/html to be copied to clipboard or function returning text/html
onCopy
: PropTypes.func
Optional callback, will be called when text/html is copied
onCopy(text, result)
result (bool)
: Returns true
if copied successfully, else false
.
options
: PropTypes.shape({asHtml: bool, onlyHtml: bool, canUsePrompt: bool, debug: bool, message: string})
Optional copy-html-to-clipboard options:
asHtml
: PropTypes.bool
True - use param text
as html
onlyHtml
: PropTypes.bool
True - if can't copy html to clipboard, don't try to copy text with alternative ways
canUsePrompt
: PropTypes.bool
True - try alternative ugly prompt-way
See API docs for details
children
: PropTypes.node.isRequired
CopyToClipboard is a simple wrapping component, it does not render any tags, so it requires the only child element to be present, which will be used to capture clicks.
<CopyToClipboard text="Hello!"> <button>Copy text to clipboard</button></CopyToClipboard>
<CopyToClipboard text="Hello, <b>I am bold</b>!" options=asHtml: true > <button>Copy html to clipboard</button></CopyToClipboard>
Development and testing
Currently is being developed and tested with the latest stable Node 7
on OSX
and Windows
.
To run example covering all CopyToClipboard
features, use npm start dev
, which will compile src/example/Example.js
git clone git@github.com:nkbt/react-copy-to-clipboard.gitcd react-copy-to-clipboardnpm installnpm start dev # then open http://localhost:8080
Tests
# to run tests npm start test # to generate test coverage (./reports/coverage) npm start test.cov # to run end-to-end tests npm start test.e2e
License
MIT