All files / modules/10-common/components/CopyToClipBoard CopyToClipBoard.tsx

72.22% Statements 13/18
22.22% Branches 4/18
66.67% Functions 2/3
72.22% Lines 13/18

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61              510x 510x 510x 510x 510x   510x               510x 81x 81x 81x 81x           81x                                                       510x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import { Position, PopoverInteractionKind, Popover } from '@blueprintjs/core'
import { Icon } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import { useToaster } from '@common/exports'
 
import css from './CopyToClipBoard.module.scss'
 
interface CopyToClipboardProps {
  content: string
  showFeedback?: boolean
  iconSize?: number
}
 
const CopyToClipboard: React.FC<CopyToClipboardProps> = props => {
  const { showSuccess } = useToaster()
  const { getString } = useStrings()
  const getPopoverContent = (): JSX.Element => {
    return (
      <div className={css.popoverContent}>
        <span className={css.tooltipLabel}>{getString('snippets.copyToClipboard')} </span>
      </div>
    )
  }
  return (
    <>
      <Popover
        minimal
        position={Position.BOTTOM}
        interactionKind={PopoverInteractionKind.HOVER}
        content={getPopoverContent()}
      >
        <div>
          <Icon
            name="copy-alt"
            size={props.iconSize ?? 20}
            onClick={async (event: React.MouseEvent<HTMLHeadingElement, globalThis.MouseEvent>) => {
              event.preventDefault()
              event.stopPropagation()
              navigator?.clipboard?.writeText(props?.content)
              if (props.showFeedback) {
                showSuccess(getString('clipboardCopySuccess'))
              }
            }}
            className={css.copyIcon}
          />
        </div>
      </Popover>
    </>
  )
}
 
export default CopyToClipboard