Skip to content

Commit bfd900e

Browse files
committed
Added Functionality for cope the code to clipboard
1 parent a754eb2 commit bfd900e

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { useState } from "react"
2+
import { BsCopy } from "react-icons/bs"
3+
import { BsCheckLg } from "react-icons/bs"
4+
function CopyToClipboard({ textToCopy }) {
5+
const [copied, setCopied] = useState(false)
6+
7+
const handleCopy = () => {
8+
navigator.clipboard
9+
.writeText(textToCopy)
10+
.then(() => {
11+
setCopied(true)
12+
setTimeout(() => setCopied(false), 1500)
13+
})
14+
.catch(error => {
15+
console.error("Error copying text to clipboard:", error)
16+
})
17+
}
18+
19+
return (
20+
<>
21+
<div onClick={handleCopy} style={{ cursor: "pointer" }}>
22+
{copied ? <BsCheckLg /> : <BsCopy />}
23+
</div>
24+
</>
25+
)
26+
}
27+
28+
export default CopyToClipboard

src/components/common/Sheet.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react"
22
import styled from "@emotion/styled"
33
import { FiLink } from "react-icons/fi"
4+
import CopyToClipboard from "./CopyToClipboard"
45

56
const SheetContainer = styled.div`
67
& h3 {
@@ -108,16 +109,22 @@ export const Sheet = ({ title, items, onlyCode }) => (
108109
/>
109110
)}
110111
{code && (
111-
<pre className="code">
112-
<code>{code}</code>
113-
</pre>
112+
<>
113+
<pre className="code">
114+
<code>{code}</code>
115+
</pre>
116+
<CopyToClipboard textToCopy={code} />
117+
</>
114118
)}
115119
</li>
116120
))
117121
) : (
118-
<pre className="code">
119-
<code>{onlyCode}</code>
120-
</pre>
122+
<>
123+
<pre className="code">
124+
<code>{onlyCode}</code>
125+
</pre>
126+
<CopyToClipboard textToCopy={code} />
127+
</>
121128
)}
122129
</ul>
123130
</SheetContainer>

0 commit comments

Comments
 (0)