All files / src/components clipboard-copy.js

0% Statements 0/10
0% Branches 0/4
0% Functions 0/5
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                                                             
import {Button, StyledOcticon} from '@primer/components'
import {CheckIcon, ClippyIcon} from '@primer/octicons-react'
import copy from 'copy-to-clipboard'
import React from 'react'
 
function ClipboardCopy({value}) {
  const [copied, setCopied] = React.useState(false)
 
  React.useEffect(() => {
    const timeout = setTimeout(() => {
      if (copied) setCopied(false)
    }, 1000)
 
    return () => clearTimeout(timeout)
  }, [copied])
 
  return (
    <Button
      aria-label="Copy to clipboard"
      onClick={() => {
        copy(value)
        setCopied(true)
      }}
    >
      {copied ? <StyledOcticon icon={CheckIcon} color="green.5" /> : <StyledOcticon icon={ClippyIcon} color="gray.7" />}
    </Button>
  )
}
 
export default ClipboardCopy