Skip to content

Feature Request: Add "Copy" Button to Code Blocks in Documentation #22

@shibadityadeb

Description

@shibadityadeb

Is your feature request related to a problem? Please describe.
Yes. While reading the Theatre.js documentation, copying code snippets requires manually selecting text. This is time-consuming, especially for multi-line code examples, and can lead to copying errors.

Solution Description
Add a small "Copy" button to the top-right corner of every <pre><code> block in the documentation.
When clicked, it should copy the entire code snippet to the clipboard with a confirmation (e.g., tooltip: “Copied!”).

Proposed Implementation (Example):

document.querySelectorAll("pre code").forEach((block) => {
  const btn = document.createElement("button");
  btn.className = "copy-btn";
  btn.innerText = "Copy";
  block.parentElement.style.position = "relative";
  block.parentElement.appendChild(btn);

  btn.addEventListener("click", async () => {
    await navigator.clipboard.writeText(block.innerText);
    btn.innerText = "Copied!";
    setTimeout(() => (btn.innerText = "Copy"), 1500);
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions