Copy-to-clipboard button with Stimulus

2770
0

Copy buttons are everywhere (invite links, API keys, CLI commands). With Stimulus, I keep it tiny and resilient: use navigator.clipboard.writeText when available, and fall back to selecting a hidden input for older browsers. I also provide immediate feedback by swapping the button label to “Copied” for a second. This is a perfect example of where Hotwire shines: the baseline is simple server-rendered HTML, and a small controller adds polish without turning the page into a JS app. When I’m copying sensitive content, I keep it out of the DOM when possible and fetch it just-in-time, but for normal URLs this approach is straightforward.