copynpm i kopio.js --save
copyimport Kopio from 'kopio.js' // using default selector '.kopio' new Kopio() // or a custom selector new Kopio('.custom')
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>
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.
copyconst kopio = new Kopio() kopio.on('success', (trigger) => { }) kopio.on('error', (trigger) => { })
The code used for the example:
copy<button class="btn kopio" data-kopio-text="You did it!">copy</button>
copyconst 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) })