Fix clipboard copy functionality for Docker Compose example

Added phx:copy event listener to handle clipboard operations for both
input/textarea elements and other content. The Docker Compose example
copy button now properly copies the configuration to clipboard.

The handler supports:
- INPUT and TEXTAREA elements (using .value)
- Other elements like PRE/CODE (using .innerText or .textContent)
This commit is contained in:
Graham McIntire 2026-01-14 09:30:09 -06:00
parent 1abdc549c7
commit 434ad14265
No known key found for this signature in database

View file

@ -272,6 +272,25 @@ topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
// Handle clipboard copy events
window.addEventListener("phx:copy", (event) => {
const el = event.target
let text = ""
if (el.tagName === "INPUT" || el.tagName === "TEXTAREA") {
text = el.value
} else {
text = el.innerText || el.textContent
}
navigator.clipboard.writeText(text).then(() => {
// Optional: Show a success message or visual feedback
console.log('Copied to clipboard')
}).catch(err => {
console.error('Failed to copy text: ', err)
})
})
// connect if there are any LiveViews on the page
liveSocket.connect()