zenwalker
11/2/2015 - 7:16 AM

clibpoard.js

export function copyToClipboard(text) {
  var textArea = document.createElement('textarea');

  textArea.style.position = 'fixed';
  textArea.style.top = -100;
  textArea.style.left = -100;
  textArea.value = text;

  document.body.appendChild(textArea);
  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('[utils/clipboard] Copied to clipboard: ' + text);
  } catch (err) {
    console.log('[utils/clipboard] Oops, unable to copy');
  }

  document.body.removeChild(textArea);
}