Gathering detailed insights and metrics for react-clipboard-polyfill
Gathering detailed insights and metrics for react-clipboard-polyfill
Gathering detailed insights and metrics for react-clipboard-polyfill
Gathering detailed insights and metrics for react-clipboard-polyfill
npm install react-clipboard-polyfill
Typescript
Module System
43.5
Supply Chain
93.5
Quality
74
Maintenance
50
Vulnerability
99.6
License
HTML (42.04%)
JavaScript (42.01%)
CSS (15.95%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
29,222
Last Day
1
Last Week
1
Last Month
41
Last Year
1,310
17 Commits
2 Watching
9 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
react-clipboard-polyfill@1.1.0
Unpacked Size
125.27 kB
Size
26.17 kB
File Count
9
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-83.3%
1
Compared to previous week
Last month
57.7%
41
Compared to previous month
Last year
57.1%
1,310
Compared to previous year
2
18
1import Clipboard from 'react-clipboard-polyfill'; 2 3const MyComponent = () => ( 4 <Clipboard 5 render={({ copyText }) => ( 6 <div onClick={() => copyText('Your text here').then(() => anythingYouWant())} /> 7 )} 8 /> 9)
A versatile clipboard component for copying text or other data to the user's clipboard on the web. It is a wrapper around the excellent clipboard-polyfill and supports a variety of usage methods so that you can use the patterns your prefer.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
This module is distributed with npm which comes with node. It is a React component that is meant to be included in an existing project. You should add it to your project's dependencies like so:
npm i --save react-clipboard-polyfill
Once installed, import or require it wherever you need it!
1import Clipboard from 'react-clipboard-polyfill' 2... 3const Clipboard = require('Clipboard');
I'm only distributing CommonJS and ESM bundles at the moment. If you need a different format, leave an issue and let me know!
A note on Promises
The package clipboard-polyfill
provides an asynchronous clipboard API using promises. As such, the author chose to bundle es6-promise polyfill
with it so that it would be usable in older browsers. Since this package bundles clipboard-polyfill
, you in turn also receive the polyfill.
Clipboard
is a standalone component that comes bundled with clipboard-polyfill. It wraps its methods in a way that is convenient to use within React. The component supports three methods for accessing the copy functions and I'll discuss each next, with examples:
Use a render prop to receive the copyText
and copyData
methods, as well as the clipboard itself.
1import Clipboard from 'react-clipboard-polyfill'; 2 3const MyComponent = () => ( 4 <Clipboard 5 render={({ clipboard, copyData, copyText }) => ( 6 <div onClick={() => copyText('test').then(() => anythingYouWant())} /> 7 )} 8 /> 9)
1import Clipboard from 'react-clipboard-polyfill'; 2 3<Clipboard 4 render={({ clipboard, copyData }) => ( 5 <div 6 style={{ cursor: 'pointer' }} 7 className="App-intro" 8 onClick={() => { 9 clipboard.setData('text/plain', 'whoooooooa'); 10 clipboard.setData('text/html', '<i>whoooooooa</i>'); 11 copyData(); 12 }} 13 > 14 To get started, edit <code>src/App.js</code> and save to reload. 15 </div> 16 )} 17/>
Works just like the render prop, but you can provide a component which will be rendered with the copyText
and copyData
functions, the clipboard
object, and with any other props that were added to Clipboard
.
1import Clipboard from 'react-clipboard-polyfill'; 2 3const Button = ({ copyText, copyData }) => ( 4 <button onClick={() => copyText('test').then(() => done())}>button</button> 5); 6 7const MyComponent = () => ( 8 <Clipboard component={Button} /> 9)
You can also use Clipboard
to automatically copy the text or data you specify when an area is clicked by composing it around your other components. If Clipboard
is not given a render or component prop, it automatically adds an onClick
handler and wraps its children in a div tag. Speficy the data or text to copy by passing it as props to Clipboard
. You can add multiple type to the clipboard using the multidata
prop.
1import Clipboard from 'react-clipboard-polyfill'; 2 3const MyComponent = () => ( 4 <Clipboard text="test"> 5 <SomeOtherComponent /> 6 </Clipboard> 7)
or
1import Clipboard from 'react-clipboard-polyfill'; 2 3const MyComponent = () => ( 4 <Clipboard data="<i>Copy HTML!</i>" dataType="text/html"> 5 <SomeOtherComponent /> 6 </Clipboard> 7)
If you want to respond to the copy event, you can pass a callback as an onCopy
prop, and it will be called after the text or data is copied.
1import Clipboard from 'react-clipboard-polyfill'; 2 3const onCopy = () => alert('You got data!'); 4 5const MyComponent = () => ( 6 <Clipboard data="<i>Copy HTML!</i>" dataType="text/html" onCopy={onCopy}> 7 <SomeOtherComponent /> 8 </Clipboard> 9) 10
Example of setting multiple items:
1<Clipboard 2 multidata={[{ dataType: 'text/html', data: '<b>test</b>' }, { dataType: 'text/plain', data: 'test' }]} 3> 4 Copies both types 5</Clipboard>
To run the tests locally, first make sure you've setup the local project with the following:
git clone git@github.com:jongrim/react-clipboard-polyfill.git
cd ./react-clipboard-polyfill
npm i
After the project is installed run npm run test
from the root directory of the project. The project uses Jest and automatically produces a code coverage report!
To produce a build, run npm run build
and the project will be built to the dist
directory. The rollup config produces a CommonJS and ESM module bundle along with source maps. These bundles include the clipboard-polyfill dependency, but do not include React or ReactDOM to avoid duplicate code with consumers.
Please read CODE OF CONDUCT and then feel free to submit an issue or a pull request! I cannot promise I will accept your suggestions or changes, but I'm willing to consider it. My goal is to keep this hyper-focused on copying text / data in React in a way that is easy to use with modern patterns. Any changes to that end or that help reduce the size of the library are greatly appreciated.
This project is licensed under the MIT License - see the LICENSE file for details
Thanks to the following people / groups:
clipboard
A reference to the inner clipboard object. You can call setData
on it such as defined here.
copyText(text: string) => Promise(<void>)
Copy the text passed. Returns a promise that resolves on success and rejects on failure.
copyData(dataType: string, data: string) => Promise(<void>)
Copy the data
as dataType
. Returns a promise that resolve on success and rejects on failure.
When Clipboard
is given a render prop, it will call that render prop with the an object containing the copyText
and copyData
functions, and the clipboard
object.
When Clipboard
is given a component prop, it will render that component and pass the copyText
and copyData
functions, and the clipboard
object as props under their respective name.
When rendering children, you may specify the following props:
text: string
The text you wish to have copied on click. If text is specified it will be copied and any values for data
and dataType
are ignored.
data: string
The data you wish copied, as a string. For instance, you can supply HTML, or base64 of an image. To copy data, you must also specify a dataType
.
dataType: string
The type that corresponds with the data
you would like to copy.
multidata: [{ data, dataType }]
You may pass an array of objects if you want to set more than one type of data for copying. Each object must have a data
and dataType
property. Clipboard
will call setData
on each before then calling write
. This is equivalent to this functionality.
onCopy: function
You may supply a function to be called after the copy succeeds. The function will be called with no arguments.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
72 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Morereact-share-on-social
Native sharing using the Web Share API if supported, a beautiful fallback if not.
@mmbt/react-share-on-social
Native sharing using the Web Share API if supported, a beautiful fallback if not.
-adisagar2003-react-share-on-social
Native sharing using the Web Share API if supported, a beautiful fallback if not.