snort/src/Element/Copy.tsx

28 lines
936 B
TypeScript
Raw Normal View History

2023-01-09 11:00:23 +00:00
import "./Copy.css";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCopy, faCheck } from "@fortawesome/free-solid-svg-icons";
2023-01-20 11:11:50 +00:00
import { useCopy } from "useCopy";
2023-01-09 11:00:23 +00:00
2023-01-16 17:48:25 +00:00
export interface CopyProps {
text: string,
maxSize?: number
}
export default function Copy({ text, maxSize = 32 }: CopyProps) {
2023-01-09 11:00:23 +00:00
const { copy, copied, error } = useCopy();
const sliceLength = maxSize / 2
2023-01-25 18:08:53 +00:00
const trimmed = text.length > maxSize ? `${text.slice(0, sliceLength)}...${text.slice(-sliceLength)}` : text
2023-01-09 11:00:23 +00:00
return (
<div className="flex flex-row copy" onClick={() => copy(text)}>
<span className="body">
{trimmed}
</span>
2023-01-09 11:00:23 +00:00
<FontAwesomeIcon
icon={copied ? faCheck : faCopy}
size="xs"
2023-01-25 18:08:53 +00:00
style={{ color: copied ? 'var(--success)' : 'var(--highlight)', marginRight: '2px' }}
2023-01-09 11:00:23 +00:00
/>
</div>
)
}