mirror of
https://github.com/primer/css.git
synced 2024-11-28 04:43:05 +03:00
use our own clipboard copy logic /shrug
This commit is contained in:
parent
76fbfed54c
commit
821f92cdbd
@ -1,13 +1,45 @@
|
||||
import React from 'react'
|
||||
import dynamic from 'next/dynamic'
|
||||
import {findDOMNode} from 'react-dom'
|
||||
import {Button} from '@primer/components'
|
||||
import Octicon, {Clippy} from '@githubprimer/octicons-react'
|
||||
|
||||
const ClipboardCopy = React.createFactory('clipboard-copy')
|
||||
export default class ClipboardCopy extends React.Component {
|
||||
copy() {
|
||||
const {value = ''} = this.props
|
||||
const {clipboard} = window.navigator
|
||||
const done = () => {
|
||||
findDOMNode(this).dispatchEvent(new CustomEvent('copy', {bubbles: false}))
|
||||
}
|
||||
if (clipboard) {
|
||||
return clipboard.writeText(value).then(done)
|
||||
} else if (!document.body) {
|
||||
return
|
||||
} else {
|
||||
var node = document.createElement('pre')
|
||||
node.style.width = '1px'
|
||||
node.style.height = '1px'
|
||||
node.style.position = 'fixed'
|
||||
node.style.top = '5px'
|
||||
node.textContent = value
|
||||
|
||||
export default dynamic(
|
||||
() => {
|
||||
return import('clipboard-copy-element').then(() => ClipboardCopy)
|
||||
},
|
||||
{
|
||||
ssr: false
|
||||
document.body.appendChild(node)
|
||||
selection.removeAllRanges()
|
||||
const range = document.createRange()
|
||||
range.selectNodeContents(node)
|
||||
selection.addRange(range)
|
||||
document.execCommand('copy')
|
||||
selection.removeAllRanges()
|
||||
document.body.removeChild(node)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
render() {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const {value = '', ...rest} = this.props
|
||||
return (
|
||||
<Button onClick={() => this.copy()} type="button" {...rest}>
|
||||
<Octicon icon={Clippy} />
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user