Table of contents
Adding copy and pasting the text feature to your website is very handy, it simplifies the process for the users, but are you finding difficulty in implementing it?
In this article, I’ve given step-by-step instructions on how to implement copy-paste text functionality.
I’ve mentioned the Github repo link at the end of the article.
Project Demo:
Import the required dependencies.
import { useRef } from 'react' import React from 'react' import ClipboardJS from 'clipboard'
The useRef hook is used to access the text area element, ClipboardJS is a library for working with clipboard.
Define the functional component
CopyPaste
that returns a form with a text area and two buttons.function CopyPaste() { const textAreaRef = useRef(null) ... return ( <div style={{ color: 'white' }}> <form> <label htmlFor="name">Name:</label> <input type="text" ref={textAreaRef} id="name" className="form-control mb-2" /> <button className="btn btn-outline-warning m-2 btn-copy" data-clipboard-target="#name" > Copy </button> <button className="btn btn-outline-info m-2" onClick={pasteFromClipboard} > Paste </button> </form> </div> ) }
The text area is created with the
input
element and theref
attribute is set to thetextAreaRef
to access it in the component.Implement the copy functionality.
React.useEffect(() => { new ClipboardJS('.btn-copy', { text: function(trigger) { return trigger.getAttribute('aria-label') } }) }, [])
Here, the
useEffect
hook is used to call theClipboardJS
constructor when the component is mounted. The.btn-copy
selector is used to select the copy button, and the text in the text area with theid
ofname
will be copied when the button is clicked.Implement the paste functionality.
const pasteFromClipboard = (e) => { e.preventDefault() navigator.clipboard .readText() .then((text) => { textAreaRef.current.value = text }) .catch((error) => { console.error(error) }) }
The
pasteFromClipboard
function reads the text from the clipboard usingnavigator.clipboard.readText
and updates the value of the text area with the returned text. Thee.preventDefault
is used to prevent the default behavior of the form submit.Export the component.
export default CopyPaste
The final code should look like this:
import { useRef } from 'react' import React from 'react' import ClipboardJS from 'clipboard' function CopyPaste() { const textAreaRef = useRef(null) React.useEffect(() => { new ClipboardJS('.btn-copy', { text: function(trigger) { return trigger.getAttribute('aria-label') } }) }, []) const pasteFromClipboard = (e) => { e.preventDefault() navigator.clipboard .readText() .then((text) => { textAreaRef.current.value = text }) .catch((error) => { console.error(error) }) } const copyToClipboard = (e) => { e.preventDefault() } return ( <div style={{ color: 'white' }}> <form> <label htmlFor="name">Name:</label> <input type="text" ref={textAreaRef} id="name" className="form-control mb-2" /> <button className="btn btn-outline-warning m-2 btn-copy" data-clipboard-target="#name" onClick={copyToClipboard} > Copy </button> <button className="btn btn-outline-info m-2" onClick={pasteFromClipboard} > Paste </button> </form> </div> ) } export default CopyPaste
GitHub Repository Link: https://github.com/vishwanath16/React-Copy-Paste-Example
How to run the project?
Getting Started
To run the project, follow these steps:
- Clone the repository
git clone https://github.com/vishwanath16/React-Copy-Paste-Example.git
2. Install dependencies
cd react-copy-paste npm install
3. Start the development server
npm start
The project will be running at http://localhost:3000/
.
Prerequisites
You will need the following tools to run this project:
Node.js
npm (Node Package Manager)
Built With
React — A JavaScript library for building user interfaces
ClipboardJS — A modern approach to copy text to clipboard