Skip to content

A React component and hook for copying text from an HTML element or directly. Includes a component for easy integration and a hook for custom copying functionality.

Notifications You must be signed in to change notification settings

vishal-gg/react-copy-snippet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-copy-snippet

screenshot

React component and hook for copying text from an HTML element.

Installation

If you prefer npm:

npm i react-copy-snippet

If you prefer yarn:

yarn add react-copy-snippet

usage

import CopySnippet from 'react-copy-snippet'
import { useRef } from 'react'

const Demo = () => {

  const snipRef = useRef(null)

  return (
    <div>
      <pre ref={snipRef}>
        this is an example text that is being copied.
      </pre>
      <CopySnippet ref={snipRef} />
    </div>
  )
}

export default Demo

if you just want to copy some text

import CopySnippet from 'react-copy-snippet'

const Demo = () => {

  return (
    <div>
      <CopySnippet text="hello world" />
    </div>
  )
}

export default Demo

we also have useCopyToClipboard hook

import { useCopyToClipboard } from 'react-copy-snippet'

const Demo = () => {

  const {handleCopy, isCopied} = useCopyToClipboard({textContent: "hello world"})

  return (
    <div>
      <button onClick={handleCopy}>{isCopied ? "copied!" : "copy"}</button>
    </div>
  )
}

export default Demo

Props

Name Type Description
text string The text content to be copied when using the component.
ref React.RefObject Reference to the HTML element from which to copy text.
Any HTML Attributes - Any additional HTML attributes to be passed to the underlying <button> element.

About

A React component and hook for copying text from an HTML element or directly. Includes a component for easy integration and a hook for custom copying functionality.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published