Created
July 15, 2020 00:52
-
-
Save swanson/968fb8351d4c0c3b815760d9809d3933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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