1.0.1 • Published 1 year ago

@istvzsig/clipboard v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Clipboard

This repository contains a JavaScript function that allows you to copy text from various HTML elements to the clipboard. It provides user feedback and handles errors gracefully.

Function Overview

The copyTextToClipboard function takes a source HTML element as an argument and returns an asynchronous callback function. When invoked, this callback will copy the text from the specified element to the clipboard.

Function Signature

function copyTextToClipboard(sourceElement);
function blockClipboardEvents();

User Manual

Installation

npm install @istvzsig/clipboard

How to Use

  1. Set Up Your HTML: Create an input field or any other element containing the text you want to copy.
  2. Create a Button: Add a button that will trigger the copy action.
  3. Attach the Function: Use the copyTextToClipboard function to attach the copy functionality to the button click event.

Example HTML

<input type="text" id="textInput" value="Copy this text!" />
<button id="copyButton">Copy Text</button>

Example JavaScript

const copyButton = document.getElementById('copyButton');
const textInput = document.getElementById('textInput');

copyButton.addEventListener(
    'pointerdown',
    copyTextToClipboard(textInput)
);

Additional Notes

  1. Ensure that your web page is served over HTTPS, as the Clipboard API requires a secure context.
  2. The blockClipboardEvents function can be used to prevent default clipboard actions if needed.

Feel free to customize the content further based on your specific requirements!

1.0.1

1 year ago

1.0.0

1 year ago