kopio.js
GitHub mark
Zero-dependency solution for copying text. Light as a croissant 🥐

Install

      
copy
npm i kopio.js --save

Import & instantiate

      
copy
import Kopio from 'kopio.js' // using default selector '.kopio' new Kopio() // or a custom selector new Kopio('.custom')

Two ways to get text

Text from data attribute:

      
copy
<button class="kopio" data-kopio-text="my text to copy">copy</button>

Specify a target:

      
copy
<button class="kopio" data-kopio-target="#target">copy</button> <p id="target">Here is some text I want to be able to be copied.</p>

Events

Saving the object enables subscribing to the copy events. There are only two events success and error. Both events pass the event triggerer as a parameter for the callback.

      
copy
const kopio = new Kopio() kopio.on('success', (trigger) => { }) kopio.on('error', (trigger) => { })

Example

copy

The code used for the example:

      
copy
<button class="btn kopio" data-kopio-text="You did it!">copy</button>
      
copy
const kopio = new Kopio() kopio.on('success', (trigger) => { const el = document.createElement('div') const text = document.createTextNode('Copied') el.classList.add('notify'); el.appendChild(text); trigger.appendChild(el); setTimeout(() => { el.remove() }, 500) })