What is clipboard?
The clipboard npm package is a simple interface for copying text to the clipboard. It does not rely on Flash and is typically used in web applications to enhance the user experience by providing copy-to-clipboard functionality with minimal effort.
What are clipboard's main functionalities?
Copy text from a trigger element
This code binds a new ClipboardJS instance to elements with the class 'btn'. When these elements are clicked, the text target they are associated with is copied to the clipboard.
new ClipboardJS('.btn');
Copy text programmatically
This code allows you to define a function that returns the text to be copied when the trigger element is activated.
const clipboard = new ClipboardJS('.btn', { text: function(trigger) { return 'Text to copy'; } });
Cut text from an input
This code sets up the clipboard instance to cut text from an input field when the trigger element is used.
new ClipboardJS('.btn', { action: function(trigger) { return 'cut'; } });
Customize via data attributes
This HTML markup shows how to use data attributes to specify the text to be copied when the button is clicked.
<button class='btn' data-clipboard-text='Text to copy'>Copy</button>
Event handling
This code adds an event listener for the 'success' event, which is fired when text is successfully copied to the clipboard. It logs information about the event and clears the selection.
clipboard.on('success', function(e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); e.clearSelection(); });
Other packages similar to clipboard
copy-to-clipboard
copy-to-clipboard is a module that provides a simple API to copy text to the clipboard. It does not have a dependency on Flash and works in modern browsers. It is similar to clipboard but offers a more straightforward, function-based approach.
clipboardy
clipboardy is a Node.js module for copying and pasting text to and from the clipboard. It works in both desktop and server environments, unlike clipboard which is primarily for the browser. clipboardy provides more comprehensive clipboard interactions, including reading from the clipboard.
clipboard-polyfill
clipboard-polyfill is a package that provides a polyfill for the Clipboard API. It aims to mimic the modern Clipboard API in browsers that do not support it fully. It is similar to clipboard in that it enables copy operations but also attempts to provide a consistent API across different browsers.
node-clipboard
Easy to use utility for reading and writing to the system clipboard.
usage
npm install clipboard
var clipboard = require('clipboard');
var fromClipboard = clipboard.read();
fromClipboard = clipboard.read('bitmap');
fromClipboard = clipboard.readAll();
clipboard.write('some text');
clipboard.write([
{ format: 'ascii', value: 'some text' },
{ format: 'unicode', value: '\u1059\u0000etc' },
{ format: 'bitmap', value: someBuffer }
]);
clipboard.clear();
var formats = clipboard.iterate(function(format, formatName, isCustom){
return formatName;
});
clipboard.write({ realJSObject: true });
var obj = clipboard.read('jsobject');