8000 GitHub - Danikoko/copy-code-snippet: A zero-dependency JavaScript utility that adds copy-to-clipboard buttons to <pre><code> blocks — perfect for blogs, docs, and Markdown renderers.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

A zero-dependency JavaScript utility that adds copy-to-clipboard buttons to <pre><code> blocks — perfect for blogs, docs, and Markdown renderers.

License

Notifications You must be signed in to change notification settings

Danikoko/copy-code-snippet

Repository files navigation

✂️ copy-code-snippet

Zero-dependency JavaScript utility that automatically adds “Copy” buttons to your <pre><code> blocks — perfect for blogs, docs, and any site that displays code.


🚀 Features

  • ✅ Automatically detects and enhances <pre><code> blocks
  • ✅ Copies code to clipboard with one click
  • ✅ “Copied!” feedback with optional timeout
  • ✅ Works out of the box — no frameworks required
  • ✅ Fully customizable via options and CSS
  • Built-in default styling, or use your own styles
  • ✅ Tiny footprint & browser-compatible

🧪 Demo

Try it live: Demo Page


📦 Installation

🔗 CDN (Recommended for static sites)

<script src="https://cdn.jsdelivr.net/npm/copy-code-snippet@1.0.7"></script>
<script>
    copyCodeSnippet(); // Initializes and applies to all code blocks
</script>

📦 NPM

npm install copy-code-snippet
import copyCodeSnippet from 'copy-code-snippet';

copyCodeSnippet(); // Or pass options

⚙️ Usage Options

copyCodeSnippet({
    selector: 'pre code', // CSS selector for code blocks
    buttonText: 'Copy', // Button label before copying
    buttonClassName: 'copy-btn', // CSS class for the button
    copiedText: 'Copied!', // Label after click
    timeout: 2000, // Time in ms before reverting label
    position: 'top-right', // Button placement: 'top-right' or 'top-left'
    disableDefaultStyle: false, // Inject default styling automatically (true = disable)
});

Note: All options are optional — sensible defaults are applied.


🎨 Styling Options

1️⃣ Use built-in default style

  • The library will automatically inject a default style block unless you set disableDefaultStyle: true.
  • This allows quick use with no extra CSS required.

Default styling:

.copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 5px 10px;
    font-size: 12px;
    background: #333;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    z-index: 100;
}

.copy-btn:hover {
    background: #555;
}

2️⃣ Use your own custom CSS

  • If you want full control over styling:

    • Set disableDefaultStyle: true
    • Provide your own styles for .copy-btn

Example:

copyCodeSnippet({
    disableDefaultStyle: true,
    buttonClassName: 'my-custom-copy-btn',
});
.my-custom-copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 6px 12px;
    font-size: 14px;
    background: #007bff;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.my-custom-copy-btn:hover {
    background: #0056b3;
}

🧹 Example Markup

<pre>
    <code class="language-js">
        const hello = 'world';
        console.log(hello);
    </code>
</pre>

✅ Browser Support

Fully supported in all modern browsers that implement the Clipboard API:

  • Chrome
  • Firefox
  • Safari
  • Edge

🛠 Contributing

PRs welcome! Whether you're improving performance, adding features, or enhancing styling options — contributions are appreciated.

  1. Fork this repo
  2. Create your feature branch (git checkout -b feature/my-feature)
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

📄 License

MIT License


📬 Author

Made with ❤️ by Daniel Enamudu GitHub: @danikoko Twitter: @iam_danikoko

About

A zero-dependency JavaScript utility that adds copy-to-clipboard buttons to <pre><code> blocks — perfect for blogs, docs, and Markdown renderers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published
0