@stimulus-library/controllers
Version:
A library of useful controllers for Stimulus
36 lines (35 loc) • 1.06 kB
JavaScript
import { BaseController } from "@stimulus-library/utilities";
export class ClipboardController extends BaseController {
constructor() {
super(...arguments);
this._supported = false;
}
connect() {
this._supported = document.queryCommandSupported("copy");
if (this.hasRemoveUnusedValue && this.removeUnusedValue) {
if (this._supported && this.hasFallbackTarget) {
this.fallbackTarget.remove();
}
else if (this.hasCopyTarget) {
this.copyTarget.remove();
}
}
}
select(event) {
if (event) {
event.preventDefault();
}
this.sourceTarget.select();
}
copy(event) {
if (event) {
event.preventDefault();
}
this.sourceTarget.select();
if (this._supported) {
document.execCommand("copy");
}
}
}
ClipboardController.targets = ["source", "button", "copy", "fallback"];
ClipboardController.values = { removeUnused: Boolean };