This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
copy to clipboard feature added #2551
Merged
eddiejaoude
merged 3 commits into
EddieHubCommunity:main
from
ChinmayMhatre:chinmay-issue-2521
Dec 31, 2022
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useState } from "react"; | ||
|
||
const ClipboardCopy = ({ copyText, children }) => { | ||
const [isCopied, setIsCopied] = useState(false); | ||
|
||
async function copyTextToClipboard(text) { | ||
if ("clipboard" in navigator) { | ||
return await navigator.clipboard.writeText(text); | ||
} else { | ||
return document.execCommand("copy", true, text); | ||
} | ||
} | ||
|
||
const handleCopyClick = () => { | ||
copyTextToClipboard(copyText) | ||
.then(() => { | ||
setIsCopied(true); | ||
setTimeout(() => { | ||
setIsCopied(false); | ||
}, 1500); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are using async/await and try/catch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh okay, I'll fix that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you 👍 |
||
}); | ||
}; | ||
|
||
return ( | ||
<div className="relative"> | ||
<div className="absolute flex items-center space-x-2 top-0 right-0 m-2"> | ||
<button | ||
type="button" | ||
aria-label="Copy to Clipboard" | ||
onClick={handleCopyClick} | ||
className="transition rounded-md flex items-center justify-center text-center px-2 focus:outline-none fade-in group-hover:flex" | ||
> | ||
{isCopied ? ( | ||
<span className="text-green-500 text-sm">Copied!</span> | ||
) : ( | ||
<span className="text-gray-500 text-sm">Copy</span> | ||
) | ||
} | ||
</button> | ||
</div> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
|
||
|
||
export default ClipboardCopy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need
copyText
andchildren
? They look the sameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
children
have the code blocks commas in the start and end. Should I use slice to remove the firsts 5 and the last three characters? Then We could probably use thechildren
prop itselfThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point 🤔 I think yes, because I am keen to try and use the same code, so there is not duplicate and not the risk of the code being different and causing bugs for people.
It would probably be safer to separate on the line breaks and remove the first line and last line - because it might not always be 5 characters at the beginning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, That is a way better option. Could you tell me how would I convert the
children
prop to string. I can't seem to find the method to get the text from the objectThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't tried before, but after a Google I found this https://github.com/fernandopasik/react-children-utilities
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it a go, thanks!