@react-hook/copy
Version:
A React hook for copying text to the clipboard
3 lines (2 loc) • 1.06 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):(e=e||self).useCopy=t(e.React)}(this,(function(e){"use strict";function t(e){return e}return function(n){function r(){return i(1)}function o(){return i(t)}var c=e.useState(0),u=c[0],i=c[1],a=e.useRef((function(){return i(0)}));return e.useEffect((function(){return a.current}),[n]),{copied:u,copy:e.useCallback((function(){return function(e){if(navigator.clipboard)return navigator.clipboard.writeText(e);var t=document.createElement("span");t.textContent=e,t.style.whiteSpace="pre",document.body.appendChild(t);var n=window.getSelection();if(!n)return Promise.reject();var r=window.document.createRange();n.removeAllRanges(),r.selectNode(t),n.addRange(r);try{window.document.execCommand("copy")}catch(e){return Promise.reject()}return n.removeAllRanges(),window.document.body.removeChild(t),Promise.resolve()}(n).then(r).catch(o)}),[n]),reset:a.current}}}));
//# sourceMappingURL=use-copy.js.map