ClipX.js – Lightweight Copy, Cut & Paste Library
ClipX.js is a small JavaScript library for copying, cutting, pasting and formatting text. It suits blogs, documentation, and apps that need a copy button without heavy dependencies. You add a few HTML attributes and it handles the rest, including feedback for the user. It works in modern browsers.
Source: github.com/mraxays/clipxjs (MIT licensed)
Install: npm install clipxjs
Documentation: clipx.js.org
Background: read the case study on why I built it
What is ClipX.js?
ClipX.js handles the common clipboard actions (copy, cut and paste) and shows built-in toast notifications so users know the action worked. You can customize messages and styles, format the text before it is copied, and turn off any feature you don't need.
Key Features
- Copy, cut and paste: from elements or static text.
- Text formatting: trim, change case, collapse whitespace, strip HTML, or add your own formatter.
- Built-in notifications: shows "Copied!" messages without extra code.
- Easy setup: add the script and call
attach(). - Customizable: change styles, messages, position and duration with attributes.
- Works with dynamic content: elements added later are picked up automatically.
- No dependencies: plain HTML and JavaScript.
How to Install ClipX.js
- Via NPM:
npm install clipxjs
- Via CDN:
<script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>
Basic Usage Example
<button data-clipx-text="Hello World">Copy Text</button>
<script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>
<script>
var clipx = new ClipX();
clipx.attach(); // matches data-clipx-text, data-clipx-target and data-clipx-paste
</script>
ClipX is an alias of the clipx constructor, so var clipx = new ClipX() doesn't overwrite it. new clipx() still works too.
Copy from Another Element
<p id="copy-me">This text will be copied.</p>
<button data-clipx-target="#copy-me">Copy Paragraph</button>
Inputs, textareas and selects use their value. Other elements use their text content.
Format the Text Before Copying
Use data-clipx-format to clean up the text. Formatters run left to right.
<input id="email" value=" Ada@Example.COM " />
<button data-clipx-target="#email" data-clipx-format="trim,lowercase">
Copy Email
</button>
Built-in formatters are trim, uppercase, lowercase, capitalize, titlecase, collapse, singleline and striphtml. You can also add your own:
clipx.addFormatter("reverse", function (text) {
return text.split("").reverse().join("");
});
<button data-clipx-text="abc" data-clipx-format="reverse">Copy Reversed</button>
Custom Messages and Toast Position
You can customize the toast message, position, and display duration directly with data attributes.
<button
data-clipx-text="Custom text"
data-clipx-msg="Copied successfully!"
data-clipx-toast-position="bottom"
data-clipx-duration="4000"
>
Copy with Custom Toast
</button>
data-clipx-msg sets the message, data-clipx-toast-position places the toast at the top or bottom, and data-clipx-duration sets how long it stays visible, in milliseconds.
Toast Customization
Custom Toast Message
<button data-clipx-text="Hello World" data-clipx-msg="Copied to clipboard!">
Copy Text
</button>
Toast Position
<button data-clipx-text="Hello World" data-clipx-toast data-clipx-toast-position="top">
Copy with Top Toast
</button>
<button data-clipx-text="Hello World" data-clipx-toast data-clipx-toast-position="bottom">
Copy with Bottom Toast
</button>
Toast Duration
<button data-clipx-text="Hello World" data-clipx-toast data-clipx-duration="3000">
Show Toast for 3 Seconds
</button>
Toast Only
Show the toast without changing the button's text:
<button data-clipx-text="Hello World" data-clipx-toast data-clipx-msg="Text copied!">
Copy Text
</button>
Disable Toast Notifications
For one element:
<button data-clipx-text="Hello World" data-clipx-disable-toast>
Copy Without Toast
</button>
Or globally:
clipx.disable.toast = true;
Disable Text Changes
To stop ClipX.js from changing the element's text after the action:
<button data-clipx-text="Hello World" data-clipx-disable-msg>Copy Text</button>
Or globally:
clipx.disable.msg = true;
Custom Toast Styling
clipx.setCustomStyles({
toast: "background-color: #333; color: #fff; padding: 12px; border-radius: 5px;",
error: "background-color: #7a1f1f;", // optional, extra styles for error toasts
});
To hide the built-in SVG toast icon:
.clipx-js-svg-icon {
display: none;
}
Complete Cut & Paste Example
ClipX.js can cut text from an input and paste it into another element.
<input id="source" type="text" value="This text can be cut and pasted." />
<button type="button" data-clipx-target="#source" data-clipx-cut data-clipx-msg="Text cut!">
Cut Text
</button>
<input id="destination" type="text" placeholder="Paste the text here" />
<button type="button" data-clipx-paste="#destination" data-clipx-msg="Text pasted!">
Paste Text
</button>
<script src="https://cdn.jsdelivr.net/npm/clipxjs/dist/clipx.min.js"></script>
<script>
var clipx = new ClipX();
clipx.attach();
</script>
How It Works
- Cut Text:
data-clipx-target="#source"selects the source input.data-clipx-cutcopies its contents to the clipboard and then clears it. - Paste Text:
data-clipx-paste="#destination"tells ClipX.js which element should receive the clipboard text. - Custom Feedback:
data-clipx-msgshows a custom message after each action.
Paste uses the browser's navigator.clipboard.readText(), which needs HTTPS (or localhost), a user click, and usually a permission prompt. If the browser doesn't allow it, ClipX.js shows an error toast. You can add data-clipx-format="trim" to the paste button to clean the text as it is inserted.
Using the JavaScript API
You can also skip the attributes and call ClipX.js from your own code:
clipx.copyText("Hello!").then(function () {
console.log("Copied");
});
clipx.pasteText().then(function (text) {
console.log("Clipboard:", text);
});
And listen for results:
document.addEventListener("clipx:success", function (e) {
console.log(e.detail.type); // "copy", "cut" or "paste"
});
document.addEventListener("clipx:error", function (e) {
console.error(e.detail.error);
});
When to Use ClipX.js
Many sites add "Copy" buttons for code snippets, referral links, or form fields. ClipX.js fits these cases:
- Technical blogs with code examples
- Web apps that let users copy IDs or tokens
- Forms that need a quick copy, cut or paste action
It is not a full clipboard framework. If you need rich content such as images or HTML, use the browser's Clipboard API directly.
For all attributes and configuration options, see the full ClipX.js documentation.