Copy-to-clipboard React component wrapper for any child element
npx @tessl/cli install tessl/npm-react-copy-to-clipboard@5.1.0React Copy to Clipboard is a React component library that provides a simple wrapper component for adding copy-to-clipboard functionality to any child element. It enables developers to easily add copy-to-clipboard features to their React applications by wrapping existing elements (buttons, spans, etc.) with the CopyToClipboard component.
npm install react-copy-to-clipboardimport { CopyToClipboard } from "react-copy-to-clipboard";For CommonJS:
const { CopyToClipboard } = require("react-copy-to-clipboard");Alternative default import:
import CopyToClipboard from "react-copy-to-clipboard";import React from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';
class App extends React.Component {
state = {
value: 'Hello World!',
copied: false,
};
render() {
return (
<div>
<input
value={this.state.value}
onChange={({target: {value}}) => this.setState({value, copied: false})}
/>
<CopyToClipboard
text={this.state.value}
onCopy={() => this.setState({copied: true})}>
<button>Copy to clipboard</button>
</CopyToClipboard>
{this.state.copied ? <span style={{color: 'red'}}>Copied!</span> : null}
</div>
);
}
}React component that wraps any child element to provide copy-to-clipboard functionality when clicked.
/**
* Copy-to-clipboard React component wrapper
*/
class CopyToClipboard extends React.PureComponent {
static propTypes = {
text: PropTypes.string.isRequired,
children: PropTypes.element.isRequired,
onCopy: PropTypes.func,
options: PropTypes.shape({
debug: PropTypes.bool,
message: PropTypes.string,
format: PropTypes.string
})
};
}text: string (required)
children: React.Element (required)
onCopy: (text: string, result: boolean) => void (optional)
text: The text that was copiedresult: true if copy was successful, false otherwiseoptions: object (optional)
debug (boolean): Enable debug mode for copy-to-clipboardmessage (string): Custom message to show in fallback promptformat (string): MIME type for clipboard formatonClick handlers on child elements - child's onClick will be called after the copy operationonClick handlercopy-to-clipboard library internally for cross-browser compatibilityUsage Examples:
// Basic button wrapper
<CopyToClipboard text="Hello World!">
<button>Copy to clipboard</button>
</CopyToClipboard>
// With callback handling
<CopyToClipboard
text="Hello World!"
onCopy={(text, result) => {
if (result) {
console.log('Successfully copied:', text);
} else {
console.log('Copy failed');
}
}}>
<button>Copy to clipboard</button>
</CopyToClipboard>
// With options and existing onClick
<CopyToClipboard
text="Hello World!"
options={{message: 'Press Ctrl+C to copy'}}
onCopy={() => alert('Copied!')}>
<button onClick={() => console.log('Button clicked!')}>
Copy to clipboard
</button>
</CopyToClipboard>
// Forwarding props to child
<CopyToClipboard text="Hello World!">
<span className="copy-button" style={{cursor: 'pointer'}}>
Click to copy
</span>
</CopyToClipboard>// PropTypes definitions (JavaScript)
CopyToClipboard.propTypes = {
text: PropTypes.string.isRequired,
children: PropTypes.element.isRequired,
onCopy: PropTypes.func,
options: PropTypes.shape({
debug: PropTypes.bool,
message: PropTypes.string,
format: PropTypes.string
})
};
CopyToClipboard.defaultProps = {
onCopy: undefined,
options: undefined
};lib/index.jsbuild/react-copy-to-clipboard.js (global: CopyToClipboard)copy-to-clipboard ^3.3.1 and prop-types ^15.8.1The component handles copy operation failures gracefully:
onCopy callback receives result: falsecopy-to-clipboard library