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
|
@ -28,6 +28,7 @@
|
||||||
"lodash.set": "^4.3.2",
|
"lodash.set": "^4.3.2",
|
||||||
"netlify-cli": "^8.4.2",
|
"netlify-cli": "^8.4.2",
|
||||||
"next": "latest",
|
"next": "latest",
|
||||||
|
"react-copy-to-clipboard": "^5.0.4",
|
||||||
"react-hotkeys-hook": "^3.4.4",
|
"react-hotkeys-hook": "^3.4.4",
|
||||||
"react-instantsearch-dom": "^6.18.0",
|
"react-instantsearch-dom": "^6.18.0",
|
||||||
"react-markdown": "^7.1.1",
|
"react-markdown": "^7.1.1",
|
||||||
|
|
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
|
||||||
|
|
7
packages/freesewing.shared/components/icons/copy.js
Normal file
7
packages/freesewing.shared/components/icons/copy.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
const Copy = props => (
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
|
||||||
|
</svg>
|
||||||
|
)
|
||||||
|
|
||||||
|
export default Copy
|
|
@ -1,3 +1,5 @@
|
||||||
|
import CopyToClipboard from 'shared/components/copy-to-clipboard'
|
||||||
|
|
||||||
const names = {
|
const names = {
|
||||||
js: 'javascript',
|
js: 'javascript',
|
||||||
bash: 'bash',
|
bash: 'bash',
|
||||||
|
@ -5,16 +7,21 @@ const names = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const Highlight = (props) => {
|
const Highlight = (props) => {
|
||||||
const {
|
|
||||||
children=[],
|
const { children=[], className='language-js' } = props
|
||||||
className='language-js',
|
|
||||||
} = props
|
|
||||||
const language = props.children.props.className.split('-').pop()
|
const language = props.children.props.className.split('-').pop()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="hljs my-4">
|
<div className="hljs my-4">
|
||||||
<div className={`text-xs uppercase font-bold text-neutral-content mt-1 text-center border-b border-neutral-content border-opacity-25 py-1 mb-2 lg:text-sm`}>
|
<div className={`
|
||||||
{names[language] ? names[language] : language}
|
flex flex-row justify-between
|
||||||
|
text-xs uppercase font-bold text-neutral-content
|
||||||
|
mt-1 border-b border-neutral-content border-opacity-25
|
||||||
|
py-1 mb-2 lg:text-sm
|
||||||
|
`}>
|
||||||
|
<span> </span>
|
||||||
|
<span>{names[language] ? names[language] : language}</span>
|
||||||
|
<CopyToClipboard content={children} />
|
||||||
</div>
|
</div>
|
||||||
<pre className={`language-${language} hljs text-base lg:text-lg whitespace-pre-wrap break-words`}>
|
<pre className={`language-${language} hljs text-base lg:text-lg whitespace-pre-wrap break-words`}>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
@ -23286,7 +23286,7 @@ react-app-polyfill@^2.0.0:
|
||||||
regenerator-runtime "^0.13.7"
|
regenerator-runtime "^0.13.7"
|
||||||
whatwg-fetch "^3.4.1"
|
whatwg-fetch "^3.4.1"
|
||||||
|
|
||||||
react-copy-to-clipboard@^5.0.3:
|
react-copy-to-clipboard@^5.0.3, react-copy-to-clipboard@^5.0.4:
|
||||||
version "5.0.4"
|
version "5.0.4"
|
||||||
resolved "https://registry.yarnpkg.com/react-copy-to-clipboard/-/react-copy-to-clipboard-5.0.4.tgz#42ec519b03eb9413b118af92d1780c403a5f19bf"
|
resolved "https://registry.yarnpkg.com/react-copy-to-clipboard/-/react-copy-to-clipboard-5.0.4.tgz#42ec519b03eb9413b118af92d1780c403a5f19bf"
|
||||||
integrity sha512-IeVAiNVKjSPeGax/Gmkqfa/+PuMTBhutEvFUaMQLwE2tS0EXrAdgOpWDX26bWTXF3HrioorR7lr08NqeYUWQCQ==
|
integrity sha512-IeVAiNVKjSPeGax/Gmkqfa/+PuMTBhutEvFUaMQLwE2tS0EXrAdgOpWDX26bWTXF3HrioorR7lr08NqeYUWQCQ==
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue