Dev Helpers
← Back

Copy to clipboard with feedback

Import and render <CopyToClipboardWithFeedback /> in a React project.

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>
  );
}