feat(fs.shared): Add copy-to-clipboard. Closes #1519
This commit is contained in:
parent
ab96362d97
commit
f5877d9abe
5 changed files with 49 additions and 7 deletions
27
packages/freesewing.shared/components/copy-to-clipboard.js
Normal file
27
packages/freesewing.shared/components/copy-to-clipboard.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
import ReactDOMServer from 'react-dom/server'
|
||||
import { useState, useEffect } from 'react'
|
||||
import CopyIcon from 'shared/components/icons/copy.js'
|
||||
import {CopyToClipboard} from 'react-copy-to-clipboard'
|
||||
|
||||
const strip = html => new DOMParser()
|
||||
.parseFromString(html, 'text/html')
|
||||
.body.textContent || ""
|
||||
|
||||
|
||||
const CopyToClipboardIcon = ({ content }) => {
|
||||
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
const text = (typeof content === 'string')
|
||||
? content
|
||||
: strip(ReactDOMServer.renderToStaticMarkup(content))
|
||||
|
||||
return (
|
||||
<CopyToClipboard text={text} onCopy={() => setCopied(true)}>
|
||||
<button className={copied ? 'text-success' : ''}><CopyIcon /></button>
|
||||
</CopyToClipboard>
|
||||
)
|
||||
}
|
||||
|
||||
export default CopyToClipboardIcon
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue