The Clipboard component provides declarative copy/cut-to-clipboard behavior (native Clipboard API + fallback) using `data-kt-clipboard*` attributes.
These data attributes configure Clipboard triggers during auto-initialization.
| Name | Type | Default | Description |
|---|---|---|---|
data-kt-clipboard | boolean | false | Presence enables auto-initialization on the trigger element. |
data-kt-clipboard-text | string |
| Name | Description |
|---|---|
data-kt-clipboard | Used to auto-initialize KTClipboard on the trigger element. |
data-kt-clipboard-target | Used to resolve the element whose content should be copied/cut. |
Use the KTClipboard API methods to control clipboard instances.
| Method | Description |
|---|---|
KTClipboard.init() | Initializes all elements with data-kt-clipboard. |
KTClipboard.createInstances() | Initializes Clipboard triggers that were added to the DOM after load. |
KTClipboard.getInstance(element) | Returns the instance for a trigger element, or null. |
import {
KTClipboard,
type KTClipboardConfigInterface,
type KTClipboardInterface,
} from '@keenthemes/ktui';
const el = document.querySelector<HTMLElement>('[data-kt-clipboard]');
if (!el) throw new Error('Missing clipboard trigger');
// Get existing instance
const instance: KTClipboardInterface | null = KTClipboard.getInstance(el);
// Or create one with typed config
const options: KTClipboardConfigInterface = {
target: '#my_input',
text: 'Fallback (if configured via attributes)'
Clipboard dispatches DOM events on the trigger element.
| Event | Description |
|---|---|
kt.clipboard.success | Fired when a copy/cut succeeds. event.detail.payload includes action, text, and target. |
kt.clipboard.error | Fired when an error occurs (missing selector, invalid cut target, empty text, or clipboard write failure). event.detail.payload includes error. |
const el = document.querySelector('[data-kt-clipboard]');
const clipboard = KTClipboard.getInstance(el);
el?.addEventListener('kt.clipboard.success', (e) => {
console.log('Clipboard success:', e.detail.payload);
});
el?.addEventListener('kt.clipboard.error', (e) => {
console.error('Clipboard error:', e.detail.payload);
});Import KTClipboard and its types from @keenthemes/ktui:
import { KTClipboard, type KTClipboardInterface } from '@keenthemes/ktui';
const el = document.querySelector<HTMLElement>('[data-kt-clipboard]');
if (!el) return;
const instance: KTClipboardInterface | null = KTClipboard.getInstance(el);
if (instance) {
// Usually you interact via the UI trigger click.
instance.on('kt.clipboard.success', (payload) => {
console.log('Copied!', payload);
});
}"" |
Static text to copy. If the attribute is present, it takes precedence over data-kt-clipboard-target. |
data-kt-clipboard-target | string | "" | CSS selector for the source element. For input/textarea it copies .value; otherwise it copies .textContent. |
data-kt-clipboard-action | "copy" | "cut" | "copy" | The clipboard action. cut is only supported when the target resolves to an input/textarea. |
data-kt-clipboard-copied-class | string | "" | Optional class toggled on the trigger after success. |
data-kt-clipboard-success-event | string | "kt.clipboard.success" | Custom success event name (dispatched on the trigger). |
data-kt-clipboard-error-event | string | "kt.clipboard.error" | Custom error event name (dispatched on the trigger). |
KTClipboard.getOrCreateInstance(element, config) | Returns an existing instance or creates one with config. |
dispose() | Removes the instance from the trigger and cleans up listeners. |
on(eventName, handler) / off(eventName, eventId) | Registers/unregisters custom events. |
getOption(name) | Returns the instance option value. |
getElement() | Returns the trigger element. |