All Projects → bence-toth → react-hook-clipboard

bence-toth / react-hook-clipboard

Licence: LGPL-3.0 License
A React hook for accessing clipboard

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-hook-clipboard

CopyPasteJS
This a small JS library to execute clipboard functions in a fast and easy way.
Stars: ✭ 20 (+25%)
Mutual labels:  clipboard, copy-to-clipboard
copy-image-clipboard
Lightweight library to copy PNG and JPG images to clipboard
Stars: ✭ 37 (+131.25%)
Mutual labels:  clipboard, copy-to-clipboard
clipper
📋 Cross Platform Desktop App to Save history of all information you copy and use them whenever with a solitary snap
Stars: ✭ 42 (+162.5%)
Mutual labels:  clipboard, copy-to-clipboard
react-hook-form-auto
Automatic form generation using ReactHookForm
Stars: ✭ 45 (+181.25%)
Mutual labels:  react-hooks
rxdb-hooks
React hooks for integrating with RxDB
Stars: ✭ 57 (+256.25%)
Mutual labels:  react-hooks
covid
COVID-19 updates webpage
Stars: ✭ 117 (+631.25%)
Mutual labels:  react-hooks
swr-examples
SWR is a React Hook library for remote data fetching. Here are some simple examples.
Stars: ✭ 14 (-12.5%)
Mutual labels:  react-hooks
frontegg-react
Frontegg-React is pre-built Component for faster and simpler integration with Frontegg services.
Stars: ✭ 15 (-6.25%)
Mutual labels:  react-hooks
vimclip
Never type outside vim again
Stars: ✭ 70 (+337.5%)
Mutual labels:  clipboard
use-custom-compare-effect
useEffect hook which takes a comparison function instead of compare using reference equality
Stars: ✭ 26 (+62.5%)
Mutual labels:  react-hooks
MyApp
A template to create awesome Apps easily ⚡️
Stars: ✭ 39 (+143.75%)
Mutual labels:  react-hooks
Desktop
A comprehensive solution for convenient and efficient work with notes, snippets, clipboard, files, and other information that requires quick access via any device.
Stars: ✭ 123 (+668.75%)
Mutual labels:  clipboard
ClipboardText
Universal clipboard text support for PowerShell, notably also in PowerShell Core (cross-platform) and Windows PowerShell v2-v4
Stars: ✭ 37 (+131.25%)
Mutual labels:  clipboard
clipboard-files
A nodejs addon, read or write file path for clipboard, support win32 and mac osx.
Stars: ✭ 26 (+62.5%)
Mutual labels:  clipboard
shop-app
A shopping mobile application made with react native for Android.
Stars: ✭ 100 (+525%)
Mutual labels:  react-hooks
how-react-hooks-work
Understand how React-hook really behaves, once and for all!
Stars: ✭ 73 (+356.25%)
Mutual labels:  react-hooks
react-recycled-scrolling
Simulate normal scrolling by using only fixed number of DOM elements for large lists of items with React Hooks
Stars: ✭ 26 (+62.5%)
Mutual labels:  react-hooks
react-captain
⚓ A collection of strongly typed React hooks and contexts.
Stars: ✭ 15 (-6.25%)
Mutual labels:  react-hooks
use-url-search-params
A React Hook to use URL query string as a state management
Stars: ✭ 51 (+218.75%)
Mutual labels:  react-hooks
react-contentful
📰 A React component library that makes it super simple to compose Contentful content into your sites and applications.
Stars: ✭ 58 (+262.5%)
Mutual labels:  react-hooks

react-hook-clipboard 📋

A React hook to access the clipboard.

Installation

Using npm:

npm install --save react-hook-clipboard

Using yarn:

yarn add react-hook-clipboard

Basic usage

The useClipboard() hook, similarly to the useState() hook, returns an array of two elements:

  • the first element contains the clipboard content which is getting updated at regular intervals
  • the second element is function which can be called with a value that will be copied to the clipboard
import React from 'react'
import useClipboard from 'react-hook-clipboard'

const ComponentWithClipboard = () => {
  const [clipboard, copyToClipboard] = useClipboard()
  const toClipboard = 'I want to go to the clipboard'

  return (
    <div className="App">
      <p>Clipboard content: {clipboard}</p>
      <button onClick={() => copyToClipboard(toClipboard)}>
        Copy to clipboard
      </button>
    </div>
  )
}

Tweaking update frequency

The default update frequency of the clipboard content is 1 second which can be overridden by calling useClipboard with an argument which is an options object, and has a member called updateFrequency that indicates the desired update frequency in milliseconds:

const [clipboard, copyToClipboard] = useClipboard({updateFrequency: 500}) 

Read more about this in Caveats.

Using callbacks

Reading from the clipboard

To be able to react to reading errors from the clipboard (for example due to the user blocking clipboard permission), you can call useClipboard with a second argument which is a function that will be called when these errors occur:

import React from 'react'
import useClipboard from 'react-hook-clipboard'

const ComponentWithClipboard = () => {
  const handleClipboardReadError = error => {
    console.log(
      'There was an error reading from the clipboard:',
      error
    )
  }

  const [clipboard, copyToClipboard] = useClipboard(
    {},
    handleClipboardReadError
  )

  const toClipboard = 'I want to go to the clipboard'

  return (
    <div className="App">
      <p>Clipboard content: {clipboard}</p>
      <button onClick={() => copyToClipboard(toClipboard)}>
        Copy to clipboard
      </button>
    </div>
  )
}

Writing to the clipboard

You may want to react to both successful and failed attempts to copy content to the clipboard.

To achieve this, you can call copyToClipboard with a second and a third argument which are both functions, you can use the former to react to a successful copying and the latter to handle errors:

import React from 'react'
import useClipboard from 'react-hook-clipboard'

const ComponentWithClipboard = () => {
  const [clipboard, copyToClipboard] = useClipboard({})

  const toClipboard = 'I want to go to the clipboard'

  const handleClipboardWriteSuccess = clipboardContent => {
    console.log(
      `'${clipboardContent}' was successfully copied to the clipboard.`
    )
  }

  const handleClipboardWriteError = error => {
    console.log(
      'There was an error writing to the clipboard:',
      error
    )
  }

  return (
    <div className="App">
      <p>Clipboard content: {clipboard}</p>
      <button
        onClick={() => copyToClipboard(
          toClipboard,
          handleClipboardWriteSuccess,
          handleClipboardWriteError
        )}
      >
        Copy to clipboard
      </button>
    </div>
  )
}

Notes

Access to the Clipboard API requires user permission. If permission to access the clipboard was previously granted by the user, clipboard data will be available. If permission was not granted previously, the user will be prompted to give permission when the component mounts. In permission was previously denied by the user, if the user agent does not support the Clipboard API or the Permissions API, you will not be able to gain clipboard access.

Caveats

Listening to clipboard content changes lacks decent browser support. Therefore this hook is accessing clipboard content at regular intervals, which can result in a delay in registering changes in clipboard content and lead to performance issues if the update frequency is low.

The Clipboard API is available only in secure contexts (a.k.a. only using HTTPS).

Due to security reasons the Clipboard API is only accessible while the browser tab is in focus. For as long as the browser tab is out of focus, the clipboard content provided by useClipboard might be outdated or blank, and attempts to write to the clipboard will fail.

Contributions

Contributions are welcome. File bug reports, create pull requests, feel free to reach out at [email protected].

Licence

LGPL-3.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].