From 5fdbcfb1b3161c1101e3d4b08faa2d9b799c552d Mon Sep 17 00:00:00 2001 From: Chinmay Mhatre Date: Sun, 1 Jan 2023 00:44:08 +0530 Subject: [PATCH] feat: copy to clipboard (#2551) * copy to clipboard feature added * updated handleCopyClick function to use async await * refactored the copy to clipboard function added react-children-utilities --- components/ClipboardCopy.js | 48 +++++++++++++++++++++++++++++++++++++ package-lock.json | 18 ++++++++++++-- package.json | 3 ++- pages/docs/quickstart.mdx | 7 ++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 components/ClipboardCopy.js diff --git a/components/ClipboardCopy.js b/components/ClipboardCopy.js new file mode 100644 index 00000000000..793c604f70b --- /dev/null +++ b/components/ClipboardCopy.js @@ -0,0 +1,48 @@ +import React, { useState } from "react"; +import { onlyText } from "react-children-utilities"; + +const ClipboardCopy = ({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 = async () => { + try { + await copyTextToClipboard(onlyText(children)); + setIsCopied(true); + setTimeout(() => { + setIsCopied(false); + }, 1500); + } catch (error) { + console.log(err); + } + }; + + return ( +
+
+ +
+ {children} +
+ ); +}; + +export default ClipboardCopy; diff --git a/package-lock.json b/package-lock.json index a5467870e01..eb461b3f805 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "linkfree", - "version": "1.24.10", + "version": "1.29.0", "hasInstallScript": true, "license": "MIT", "dependencies": { @@ -24,6 +24,7 @@ "next": "^13", "qrcode.react": "^3.1.0", "react": "^18.2.0", + "react-children-utilities": "^2.8.0", "react-dom": "^18.2.0", "react-icons": "^4.6.0", "react-markdown": "^8.0.3", @@ -7890,6 +7891,14 @@ "node": ">=0.10.0" } }, + "node_modules/react-children-utilities": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/react-children-utilities/-/react-children-utilities-2.8.0.tgz", + "integrity": "sha512-g42oRsZLrFJgCcIdK1lad1CWujNH4gh1Cp1lsMQpHWdDjWQ8gUlaBebgy2iXofyPEpfJ4T/xt4qWrvDkgVCCNg==", + "peerDependencies": { + "react": "18 || 17 || 16 || 15" + } + }, "node_modules/react-dom": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz", @@ -15059,6 +15068,11 @@ "loose-envify": "^1.1.0" } }, + "react-children-utilities": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/react-children-utilities/-/react-children-utilities-2.8.0.tgz", + "integrity": "sha512-g42oRsZLrFJgCcIdK1lad1CWujNH4gh1Cp1lsMQpHWdDjWQ8gUlaBebgy2iXofyPEpfJ4T/xt4qWrvDkgVCCNg==" + }, "react-dom": { "version": "18.2.0", "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz", @@ -16205,4 +16219,4 @@ "integrity": "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==" } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index a76ffbe1216..1b75934ff33 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "next": "^13", "qrcode.react": "^3.1.0", "react": "^18.2.0", + "react-children-utilities": "^2.8.0", "react-dom": "^18.2.0", "react-icons": "^4.6.0", "react-markdown": "^8.0.3", @@ -89,4 +90,4 @@ "bugs": { "url": "https://github.com/EddieHubCommunity/LinkFree/issues" } -} \ No newline at end of file +} diff --git a/pages/docs/quickstart.mdx b/pages/docs/quickstart.mdx index d2b429ba940..4e5dff6dd72 100644 --- a/pages/docs/quickstart.mdx +++ b/pages/docs/quickstart.mdx @@ -1,4 +1,5 @@ import DocsLayout from "../../components/layouts/DocsLayout.js"; +import ClipboardCopy from "../../components/ClipboardCopy.js"; ## Quickstart @@ -29,6 +30,7 @@ To do this, you need a GitHub account. If you do not have one yet, you can creat 8. For the file contents use the json structure below: + ```js { "name": "Sara Jaoude", @@ -38,6 +40,7 @@ To do this, you need a GitHub account. If you do not have one yet, you can creat "avatar": "https://github.com/SaraJaoude.png", } ``` + | field | type | description | | :----------------- | :------------------- | :------------------------------------------------------------------------------------------------------------ | @@ -49,6 +52,7 @@ To do this, you need a GitHub account. If you do not have one yet, you can creat 9. Now add links to your social media and other content (for example: website) so they appear on your Profile. + ```js "links": [ { @@ -58,6 +62,7 @@ To do this, you need a GitHub account. If you do not have one yet, you can creat } ] ``` + | field | type | description | | :---- | :----- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -69,6 +74,7 @@ To do this, you need a GitHub account. If you do not have one yet, you can creat To add more links add another object inside the links collection. For example: + ```js "links": [ { @@ -83,6 +89,7 @@ To add more links add another object inside the links collection. For example: } ] ``` + 10. Scroll to the bottom and commit new file by adding the commit messsage that starts `data: ` followed by your GitHub username (for example `data: SaraJaoude`)