A robust, easy-to-use utility for copying text to the clipboard with a simple function call. Perfect for developers looking to implement a copy-to-clipboard feature in their web applications with minimal effort.
- Secure Clipboard Access: Utilizes the modern asynchronous Clipboard API with a fallback for older browsers.
- Optional Callback Function: Allows the execution of a callback function upon successful copy.
- Easy to Implement: A simple function call enables text copying to the clipboard.
- Fallback for Non-Secure Contexts: Ensures functionality in non-secure contexts or older browsers by using a textarea based method.
Using npm:
npm install @stianlarsen/copy-to-clipboard
Or using yarn:
yarn add @stianlarsen/copy-to-clipboard
Import the copy
function and use it to copy text to the clipboard. You can also provide an optional callback function to execute upon successful copy:
import { copy } from "@stianlarsen/copy-to-clipboard";
const yourCallbackFunction = () => {
console.log("Text successfully copied to clipboard!");
};
function App() {
return (
<button onClick={() => copy("Text to copy", yourCallbackFunction)}>
Copy to Clipboard
</button>
);
}
import { copy } from "@stianlarsen/copy-to-clipboard";
// Example function to call the copy functionality
function copyText() {
const textToCopy = "Hello, clipboard!";
copy(textToCopy, () => {
console.log("Text successfully copied to clipboard!");
});
}
// Trigger the copy function
copyText();
Designed to work in modern browsers with full support for the asynchronous Clipboard API. Includes a fallback mechanism for older browsers.
There's no direct customization required for this utility, but it seamlessly integrates into any web application, allowing for customized button or trigger implementation for the copy action.
Contributions to @stianlarsen/copy-to-clipboard
are welcome!
@stianlarsen/copy-to-clipboard
is MIT licensed.
- GitHub: @stianlars1
- Website: https://stianlarsen.com
- Email: stian.larsen@mac.com