UNPKG

react-copy-to-clipboard-ts

Version:

React 18, 19 compatible copy-to-clipboard component with TypeScript

2 lines (1 loc) 3.04 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const T=require("react");function E(e){const a=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const o in e)if(o!=="default"){const n=Object.getOwnPropertyDescriptor(e,o);Object.defineProperty(a,o,n.get?n:{enumerable:!0,get:()=>e[o]})}}return a.default=e,Object.freeze(a)}const y=E(T);function R(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var b,v;function S(){return v||(v=1,b=function(){var e=document.getSelection();if(!e.rangeCount)return function(){};for(var a=document.activeElement,o=[],n=0;n<e.rangeCount;n++)o.push(e.getRangeAt(n));switch(a.tagName.toUpperCase()){case"INPUT":case"TEXTAREA":a.blur();break;default:a=null;break}return e.removeAllRanges(),function(){e.type==="Caret"&&e.removeAllRanges(),e.rangeCount||o.forEach(function(d){e.addRange(d)}),a&&a.focus()}}),b}var C,w;function h(){if(w)return C;w=1;var e=S(),a={"text/plain":"Text","text/html":"Url",default:"Text"},o="Copy to clipboard: #{key}, Enter";function n(c){var t=(/mac os x/i.test(navigator.userAgent)?"⌘":"Ctrl")+"+C";return c.replace(/#{\s*key\s*}/g,t)}function d(c,t){var l,s,f,p,u,r,g=!1;t||(t={}),l=t.debug||!1;try{f=e(),p=document.createRange(),u=document.getSelection(),r=document.createElement("span"),r.textContent=c,r.ariaHidden="true",r.style.all="unset",r.style.position="fixed",r.style.top=0,r.style.clip="rect(0, 0, 0, 0)",r.style.whiteSpace="pre",r.style.webkitUserSelect="text",r.style.MozUserSelect="text",r.style.msUserSelect="text",r.style.userSelect="text",r.addEventListener("copy",function(i){if(i.stopPropagation(),t.format)if(i.preventDefault(),typeof i.clipboardData>"u"){l&&console.warn("unable to use e.clipboardData"),l&&console.warn("trying IE specific stuff"),window.clipboardData.clearData();var m=a[t.format]||a.default;window.clipboardData.setData(m,c)}else i.clipboardData.clearData(),i.clipboardData.setData(t.format,c);t.onCopy&&(i.preventDefault(),t.onCopy(i.clipboardData))}),document.body.appendChild(r),p.selectNodeContents(r),u.addRange(p);var D=document.execCommand("copy");if(!D)throw new Error("copy command was unsuccessful");g=!0}catch(i){l&&console.error("unable to copy using execCommand: ",i),l&&console.warn("trying IE specific stuff");try{window.clipboardData.setData(t.format||"text",c),t.onCopy&&t.onCopy(window.clipboardData),g=!0}catch(m){l&&console.error("unable to copy using clipboardData: ",m),l&&console.error("falling back to prompt"),s=n("message"in t?t.message:o),window.prompt(s,c)}}finally{u&&(typeof u.removeRange=="function"?u.removeRange(p):u.removeAllRanges()),r&&document.body.removeChild(r),f()}return g}return C=d,C}var k=h();const O=R(k),j=({text:e,onCopy:a,options:o,children:n,...d})=>{const c=y.useCallback(l=>{const s=y.Children.only(n),f=O(e,o);a&&a(e,f),s.props.onClick&&typeof s.props.onClick=="function"&&s.props.onClick(l)},[e,a,o,n]),t=y.Children.only(n);return y.cloneElement(t,{onClick:c,...d})};exports.CopyToClipboard=j;