demarches-normaliennes/app/javascript/controllers/clipboard_controller.ts

35 lines
865 B
TypeScript
Raw Normal View History

2022-11-29 09:37:58 +01:00
import { Controller } from '@hotwired/stimulus';
const SUCCESS_MESSAGE_TIMEOUT = 1000;
export class ClipboardController extends Controller {
static values = { text: String };
static targets = ['success'];
declare readonly textValue: string;
declare readonly successTarget: HTMLElement;
declare readonly hasSuccessTarget: boolean;
#timer?: ReturnType<typeof setTimeout>;
disconnect(): void {
clearTimeout(this.#timer);
}
copy() {
navigator.clipboard
.writeText(this.textValue)
.then(() => this.displayCopyConfirmation());
}
private displayCopyConfirmation() {
if (this.hasSuccessTarget) {
this.successTarget.classList.remove('hidden');
clearTimeout(this.#timer);
this.#timer = setTimeout(() => {
this.successTarget.classList.add('hidden');
}, SUCCESS_MESSAGE_TIMEOUT);
}
}
}