({
textToCopy,
children,
leadingSpace = true,
style,
})
| 125 | * ``` |
| 126 | */ |
| 127 | export const CopyButton: React.FC<CopyButtonProps> = ({ |
| 128 | textToCopy, |
| 129 | children, |
| 130 | leadingSpace = true, |
| 131 | style, |
| 132 | }) => { |
| 133 | const [isCopied, setIsCopied] = useState(false) |
| 134 | const [isHovered, setIsHovered] = useState(false) |
| 135 | const { setTimeout } = useTimeout() |
| 136 | |
| 137 | const handleCopy = async () => { |
| 138 | try { |
| 139 | await copyTextToClipboard(textToCopy, { |
| 140 | suppressGlobalMessage: true, |
| 141 | }) |
| 142 | const newState = copyButtonHandlers.handleCopy() |
| 143 | setIsCopied(newState.isCopied) |
| 144 | setIsHovered(newState.isHovered) |
| 145 | setTimeout('reset-copied', () => setIsCopied(false), COPIED_RESET_DELAY_MS) |
| 146 | } catch (_error) { |
| 147 | // Error is already logged and displayed by copyTextToClipboard |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | const handleMouseOver = () => { |
| 152 | const shouldHover = copyButtonHandlers.handleMouseOver(isCopied) |
| 153 | if (shouldHover) { |
| 154 | setIsHovered(true) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | const handleMouseOut = () => { |
| 159 | setIsHovered(copyButtonHandlers.handleMouseOut()) |
| 160 | } |
| 161 | |
| 162 | return ( |
| 163 | <Clickable |
| 164 | as="text" |
| 165 | style={style} |
| 166 | onMouseDown={handleCopy} |
| 167 | onMouseOver={handleMouseOver} |
| 168 | onMouseOut={handleMouseOut} |
| 169 | > |
| 170 | {children} |
| 171 | <CopyIcon |
| 172 | isCopied={isCopied} |
| 173 | isHovered={isHovered} |
| 174 | leadingSpace={leadingSpace} |
| 175 | /> |
| 176 | </Clickable> |
| 177 | ) |
| 178 | } |
nothing calls this directly
no test coverage detected