Skip to content

Instantly share code, notes, and snippets.

@swanson
Created July 15, 2020 00:52
Show Gist options
  • Save swanson/968fb8351d4c0c3b815760d9809d3933 to your computer and use it in GitHub Desktop.
Save swanson/968fb8351d4c0c3b815760d9809d3933 to your computer and use it in GitHub Desktop.
<div data-controller="clipboard" class="mt-4 bg-gray-50 p-6">
<input type="text" class="form-input bg-grey-50" value="SeCrEtKeY-42!" data-clipboard-target="source" readonly />
<button class="btn" data-clipboard-target="button" data-action="clipboard#copy">
Copy
</button>
</div>
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["source", "button"];
connect() {
if (!document.queryCommandSupported("copy")) {
this.buttonTarget.classList.add("hidden");
}
}
copy() {
this.sourceTarget.select();
document.execCommand("copy");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment