"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = require("react");

/**
 * `useClipboard` is a custom hook to handle copying content to clipboard
 */

/**
 *
 * @param {any} value - The content to add to clipboard
 */
var copyToClipboard = function copyToClipboard(value) {
  var el = document.createElement('textarea');
  el.value = value;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.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);
  }
};

function useClipboard(value) {
  var _useState = (0, _react.useState)(false),
      hasCopied = _useState[0],
      setHasCopied = _useState[1];

  var onCopy = function onCopy() {
    copyToClipboard(value);
    setHasCopied(true);
    setTimeout(function () {
      return setHasCopied(false);
    }, 1500);
  };

  return {
    value: value,
    onCopy: onCopy,
    hasCopied: hasCopied
  };
}

var _default = useClipboard;
exports["default"] = _default;
//# sourceMappingURL=useClipboard.js.map