UNPKG

bbo

Version:

bbo is a utility library of zero dependencies for javascript.

23 lines (19 loc) 641 B
import attr from './attr.js'; import setStyle from './set_style.js'; function copyToClipboard(str) { var el = document.createElement('textarea'); el.value = str; attr(el, 'readonly', ''); setStyle(el, 'position', 'absolute'); setStyle(el, 'left', '-9999px'); document.body.appendChild(el); var selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false; el.select(); document.execCommand('copy'); document.body.removeChild(el); if (selected) { document.getSelection().removeAllRanges(); document.getSelection().addRange(selected); } } export default copyToClipboard;