All files / src/components/common CopyButton.tsx

31.81% Statements 28/88
100% Branches 0/0
0% Functions 0/1
31.81% Lines 28/88

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1421x 1x 1x                                           1x   1x     1x   1x 1x   1x   1x 1x   1x                       1x     1x   1x   1x   1x 1x                                                                             1x   1x 1x 1x                                                             1x 1x 1x 1x 1x 1x  
import { VSCodeButton } from "@vscode/webview-ui-toolkit/react"
import React, { forwardRef, useState } from "react"
import styled from "styled-components"
 
// ======== Interfaces ========
 
interface CopyButtonProps {
	textToCopy?: string
	onCopy?: () => string | undefined | null
	className?: string
	ariaLabel?: string
}
 
interface WithCopyButtonProps {
	children: React.ReactNode
	textToCopy?: string
	onCopy?: () => string | undefined | null
	position?: "top-right" | "bottom-right"
	style?: React.CSSProperties
	className?: string
	onMouseUp?: (event: React.MouseEvent<HTMLDivElement>) => void
	ariaLabel?: string
}
 
// ======== Styled Components ========
 
const StyledButton = styled(VSCodeButton)`
	z-index: 1;
	transform: scale(0.9);
`
 
// Unified container component
const ContentContainer = styled.div`
	position: relative;
`
 
// Unified button container with flexible positioning
const ButtonContainer = styled.div<{ $position?: "top-right" | "bottom-right" }>`
	position: absolute;
	${(props) => {
		switch (props.$position) {
			case "bottom-right":
				return "bottom: 2px; right: 2px;"
			case "top-right":
			default:
				return "top: 5px; right: 5px;"
		}
	}}
	z-index: 1;
	opacity: 0;
 
	${ContentContainer}:hover & {
		opacity: 0.5;
	}
`
 
// ======== Component Implementations ========
 
/**
 * Base copy button component with clipboard functionality
 */
export const CopyButton: React.FC<CopyButtonProps> = ({ textToCopy, onCopy, className = "", ariaLabel }) => {
	const [copied, setCopied] = useState(false)
 
	const handleCopy = () => {
		if (!textToCopy && !onCopy) {
			return
		}
 
		let textToCopyFinal = textToCopy
 
		if (onCopy) {
			const result = onCopy()
			if (typeof result === "string") {
				textToCopyFinal = result
			}
		}
 
		if (textToCopyFinal) {
			navigator.clipboard
				.writeText(textToCopyFinal)
				.then(() => {
					setCopied(true)
					setTimeout(() => setCopied(false), 1500)
				})
				.catch((err) => console.error("Copy failed", err))
		}
	}
 
	return (
		<StyledButton
			appearance="icon"
			aria-label={copied ? "Copied" : ariaLabel || "Copy"}
			className={className}
			onClick={handleCopy}>
			<span className={`codicon codicon-${copied ? "check" : "copy"}`}></span>
		</StyledButton>
	)
}
 
/**
 * Container component that wraps content with a copy button
 */
export const WithCopyButton = forwardRef<HTMLDivElement, WithCopyButtonProps>(
	(
		{
			children,
			textToCopy,
			onCopy,
			position = "top-right",
			style,
			className,
			onMouseUp,
			ariaLabel, // Destructure ariaLabel
			...props
		},
		ref,
	) => {
		return (
			<ContentContainer className={className} onMouseUp={onMouseUp} ref={ref} style={style} {...props}>
				{children}
				{(textToCopy || onCopy) && (
					<ButtonContainer $position={position}>
						<CopyButton
							ariaLabel={ariaLabel}
							onCopy={onCopy}
							textToCopy={textToCopy} // Pass through the ariaLabel prop directly
						/>
					</ButtonContainer>
				)}
			</ContentContainer>
		)
	},
)
 
// Default export for convenience if needed, though named exports are preferred for clarity
const CopyButtonComponents = {
	CopyButton,
	WithCopyButton,
}
export default CopyButtonComponents