The clipboard-service
package is a lightweight, easy-to-use library that simplifies interaction with the browser’s Clipboard API. With ClipboardService
, you can easily read from and write to the clipboard with just a few lines of code. It provides a straightforward, promise-based API for copying and pasting text or other content, allowing you to focus on building your application without worrying about clipboard intricacies.
Install the package:
npm install -S clipboard-service
import { ClipboardService } from 'clipboard-service';
// check if the Clipboard API is supported by the browser
if (ClipboardService.isCompatible) {
// write text to the system clipboard
try {
await ClipboardService.writeText(code);
} catch (e) {
console.error(e);
}
// request text from the system clipboard
try {
const otpToken = await ClipboardService.readText();
} catch (e) {
console.error(e);
}
}
IClipboardItem
Represents an item that can be copied to or read from the clipboard. For more information, visit: ClipboardItem.
type IClipboardItem = ClipboardItem;
IClipboardService
Object in charge of interacting with the Clipboard API.
type IClipboardService = {
// properties
isSupported: boolean;
// actions
write: (data: IClipboardItem[]) => Promise<void>;
writeText: (data: string) => Promise<void>;
read: () => Promise<IClipboardItem[]>;
readText: () => Promise<string>;
};
- TypeScript
# integration tests
npm run test:integration
# unit tests
npm run test:unit
Install dependencies:
npm install
Build the library:
npm start
Publish to npm
:
npm publish