Coder Social home page Coder Social logo

Comments (2)

alex-shpak avatar alex-shpak commented on July 20, 2024

Hi!
I looked into adding copy button before, but as I try to keep everything simple I decided against it, it was too much of support for icons, code and light-dark theme,
But I can reconsider is simple (code wise) solution with arise.

from hugo-book.

eskopp avatar eskopp commented on July 20, 2024

Hello @alex-shpak and @djsamperi,
I had the same problem. I made the following solution myself. The implementation is quite simple and so is the layout. Maybe you can add it to the original repo.
Greetings
@eskopp

Solution

Dark

image

Light

image

Source Code

JavaScript Code

<script>
  document.addEventListener('DOMContentLoaded', (event) => {
    document.querySelectorAll('pre code').forEach((block) => {
      const button = document.createElement('button');
      button.className = 'copy-button';
      button.textContent = 'Copy';
      button.addEventListener('click', () => {
        const range = document.createRange();
        range.selectNodeContents(block);
        const selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
        document.execCommand('copy');
        button.textContent = 'Copied!';
        setTimeout(() => { button.textContent = 'Copy'; }, 2000);
      });
      block.parentNode.insertBefore(button, block);
    });
  });
  </script>

CSS Code

.copy-button {
  position: absolute;
  right: 10px;
  top: 10px;
  padding: 5px 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background-color: #6c757d;
  color: white;
  font-family: Arial, sans-serif;
  font-size: 0.875rem;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
  transition: background-color 0.3s, box-shadow 0.3s;
}

.copy-button:hover {
  background-color: #5a6268;
  box-shadow: 0 2px 5px rgba(0,0,0,0.25);
}

pre {
  position: relative;
  background-color: #f8f9fa;
  border: 1px solid #e1e1e1;
  padding: 16px;
  border-radius: 4px;
  overflow: auto;
}

from hugo-book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.