Open this example in the interactive library
Live preview
Source code
// components/CopyToClipboardWithFeedback.tsx
import { useState } from "react";
export default function CopyToClipboardWithFeedback({
filename = "notes.txt",
text = "Studio notes",
}: { filename?: string; text?: string } = {}) {
const [status, set] = useState("");
return (
<div>
<button
onClick={async () => {
try {
await navigator.clipboard.writeText(text);
set("Copied");
} catch {
set("Copy unavailable");
}
}}
>
Copy {filename}
</button>
<output>{status}</output>
</div>
);
}
