Code Monkey home page Code Monkey logo

react-use-clipboard's Introduction

πŸ“‹ react-use-clipboard

NPM version Test build status Bundle size Bundle size

A React Hook that provides copy to clipboard functionality.

Install

You can install react-use-clipboard with npm, Yarn, or pnpm.

npm install react-use-clipboard
yarn add react-use-clipboard
pnpm install react-use-clipboard

Usage

Here's how to use react-use-clipboard:

import useClipboard from "react-use-clipboard";

function App() {
  const [isCopied, setCopied] = useClipboard("Text to copy");

  return (
    <button onClick={setCopied}>
      Was it copied? {isCopied ? "Yes! πŸ‘" : "Nope! πŸ‘Ž"}
    </button>
  );
}

You can reset the isCopied value after a certain amount of time with the successDuration option.

import useClipboard from "react-use-clipboard";

function App() {
  const [isCopied, setCopied] = useClipboard("Text to copy", {
    // `isCopied` will go back to `false` after 1000ms.
    successDuration: 1000,
  });

  return (
    <button onClick={setCopied}>
      Was it copied? {isCopied ? "Yes! πŸ‘" : "Nope! πŸ‘Ž"}
    </button>
  );
}

This package only works in versions of React that support Hooks.

react-use-clipboard's People

Contributors

aljadan avatar arthurdenner avatar danoc avatar dependabot[bot] avatar dimazuien avatar github-actions[bot] avatar jeongnaehyeok avatar tu4mo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-use-clipboard's Issues

Error on Firefox

Hello everyone,

I have tested the solution on Firefox but it rejects an error:

Unhandled Rejection (TypeError): 'clipboard-write' (value of 'name' member of PermissionDescriptor) is not a valid value for enumeration PermissionName.

Do you have a solution ?

Types not working

Hi and thanks for the lib! I noticed that types are referring to dist/react-use-clipboard.d.ts, which doesn't exist in the package.

How to copy mutable text?

For example, api like this:

const [isCopied, setCopied] = useClipboard({
  successDuration: 1000,
  defaultCopyText: 'copied text!'
});

// ...
<>
  <span onClick={() => setCopied()}>some text</span> // will be "copied text!"
  <span onClick={() => setCopied('new text!')}>some text</span> // will be "new text!"
</>

SyntaxError: Cannot use import statement outside a module

When working with nextjs, it is founded that the server returns error as this syntax error pops out

import{useState as t,useEffect as o}from"react";import r from"copy-to-clipboard";export default function(c,e){const[i,n]=t(!1),u=e&&e.successDuration;return o(()=>{if(i&&u){const t=setTimeout(()=>{n(!1)},u);return()=>{clearTimeout(t)}}},[i,u]),[i,()=>{const t=r(c);n(t)}]}
^^^^^^

SyntaxError: Cannot use import statement outside a module

isCopied is not reactive to other objects being copied.

I am rendering a list of colors

image

When I click one of these colors, the local isCopied state is true.

image

But when I click another color, the old color still maintains it's value of isCopied.

image

Is there any way to make sure that only one instance of isCopied is set to true?

Can't import the named export 'useState' from non EcmaScript module (only default export is available)

Got this error when I use this as mentioned in the example.

./node_modules/react-use-clipboard/dist/react-use-clipboard.mjs
Can't import the named export 'useState' from non EcmaScript module (only default export is available)

Code

import useClipboard from "react-use-clipboard";


export default function Home() {
    const [query, setQuery] = useState('');
    const [isCopied, setCopied] = useClipboard("Text to copy");
}

Version

"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-use-clipboard": "0.1.2",

Jest Cannot find module 'react-use-clipboard'

Test suite failed to run

    Cannot find module 'react-use-clipboard' from 'x.tsx'

    However, Jest was able to find:
        './x.tsx'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'jsx', 'ts', 'tsx', 'json'].

This issue arose in version 0.1.3
0.1.2 had no issue

I assume its to do the the file extension change in 0.1.3 but you may know better.

Should point out to get around I have mocked it but raised this encase anyone else runs into it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.