You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

30 lines
870 B

import { Controller } from '@hotwired/stimulus';
/**
* Copies data-copy-text-text-value to the clipboard (e.g. full payment URI for wallets).
*/
export default class extends Controller {
static values = { text: String };
static targets = ['button'];
async copy() {
const t = this.textValue ?? '';
if (t === '') {
return;
}
try {
await navigator.clipboard.writeText(t);
const btn = this.hasButtonTarget ? this.buttonTarget : this.element.querySelector('button');
if (btn) {
const prev = btn.textContent;
btn.textContent = 'Copied';
window.setTimeout(() => {
btn.textContent = prev;
}, 2000);
}
} catch (e) {
console.warn('Copy failed', e);
}
}
}