| 1 |
import { createRef } from 'react'; |
| 2 |
import RhUiCopyFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-copy-fill-icon'; |
| 3 |
import { Button } from '../Button'; |
| 4 |
import { Tooltip, TooltipPosition } from '../Tooltip'; |
| 5 |
|
| 6 |
export interface ClipboardCopyButtonProps extends Omit< |
| 7 |
React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, |
| 8 |
'ref' |
| 9 |
> { |
| 10 |
/** Callback for the copy when the button is clicked */ |
| 11 |
onClick: (event: React.MouseEvent) => void; |
| 12 |
/** Content of the copy button */ |
| 13 |
children: React.ReactNode; |
| 14 |
/** ID of the copy button */ |
| 15 |
id: string; |
| 16 |
/** @deprecated ID of the content that is being copied */ |
| 17 |
textId?: string; |
| 18 |
/** Additional classes added to the copy button */ |
| 19 |
className?: string; |
| 20 |
/** Exit delay on the copy button tooltip */ |
| 21 |
exitDelay?: number; |
| 22 |
/** Entry delay on the copy button tooltip */ |
| 23 |
entryDelay?: number; |
| 24 |
/** Max width of the copy button tooltip */ |
| 25 |
maxWidth?: string; |
| 26 |
/** Position of the copy button tooltip */ |
| 27 |
position?: |
| 28 |
| TooltipPosition |
| 29 |
| 'auto' |
| 30 |
| 'top' |
| 31 |
| 'bottom' |
| 32 |
| 'left' |
| 33 |
| 'right' |
| 34 |
| 'top-start' |
| 35 |
| 'top-end' |
| 36 |
| 'bottom-start' |
| 37 |
| 'bottom-end' |
| 38 |
| 'left-start' |
| 39 |
| 'left-end' |
| 40 |
| 'right-start' |
| 41 |
| 'right-end'; |
| 42 |
/** Aria-label for the copy button */ |
| 43 |
'aria-label'?: string; |
| 44 |
/** Variant of the copy button */ |
| 45 |
variant?: 'control' | 'plain'; |
| 46 |
/** Callback when tooltip's hide transition has finished executing */ |
| 47 |
onTooltipHidden?: () => void; |
| 48 |
/** Removes padding from clipboard copy button. This is used for inline-compact variant */ |
| 49 |
hasNoPadding?: boolean; |
| 50 |
} |
| 51 |
|
| 52 |
export const ClipboardCopyButton: React.FunctionComponent<ClipboardCopyButtonProps> = ({ |
| 53 |
onClick, |
| 54 |
exitDelay = 0, |
| 55 |
entryDelay = 300, |
| 56 |
maxWidth = '100px', |
| 57 |
position = 'top', |
| 58 |
'aria-label': ariaLabel = 'Copyable input', |
| 59 |
id, |
| 60 |
children, |
| 61 |
variant = 'control', |
| 62 |
onTooltipHidden = () => {}, |
| 63 |
className, |
| 64 |
hasNoPadding = false, |
| 65 |
...props |
| 66 |
}: ClipboardCopyButtonProps) => { |
| 67 |
const triggerRef = createRef<HTMLButtonElement>(); |
| 68 |
|
| 69 |
return ( |
| 70 |
<Tooltip |
| 71 |
trigger="mouseenter focus click" |
| 72 |
triggerRef={triggerRef} |
| 73 |
exitDelay={exitDelay} |
| 74 |
entryDelay={entryDelay} |
| 75 |
maxWidth={maxWidth} |
| 76 |
position={position} |
| 77 |
aria-live="polite" |
| 78 |
aria="none" |
| 79 |
content={<div>{children}</div>} |
| 80 |
onTooltipHidden={onTooltipHidden} |
| 81 |
> |
| 82 |
<Button |
| 83 |
type="button" |
| 84 |
variant={variant} |
| 85 |
hasNoPadding={hasNoPadding} |
| 86 |
onClick={onClick} |
| 87 |
aria-label={ariaLabel} |
| 88 |
className={className} |
| 89 |
id={id} |
| 90 |
icon={<RhUiCopyFillIcon />} |
| 91 |
{...props} |
| 92 |
ref={triggerRef} |
| 93 |
/> |
| 94 |
</Tooltip> |
| 95 |
); |
| 96 |
}; |
| 97 |
ClipboardCopyButton.displayName = 'ClipboardCopyButton'; |