peppa/apps/web/components/CopyText.tsx
2026-11-16 09:04:45 +05:00

29 lines
1.2 KiB
TypeScript

"use client";
import { useState } from "react";
import { Check, Copy } from "lucide-react";
/** PEP-135: a value the person pastes elsewhere (an address), in full, with a copy button. */
export function CopyText({ value, label, done }: { value: string; label: string; done: string }) {
const [copied, setCopied] = useState(false);
async function copy() {
try {
await navigator.clipboard.writeText(value);
setCopied(true);
setTimeout(() => setCopied(false), 2500);
} catch {
/* no clipboard access: the text is selectable, the person copies it by hand */
}
}
return (
<span className="flex flex-wrap items-center gap-2">
<code className="min-w-0 flex-[1_1_14rem] overflow-x-auto rounded-md border bg-muted px-3 py-2 font-mono text-sm select-all" data-copy-value>
{value}
</code>
<button type="button" onClick={() => void copy()} className="inline-flex shrink-0 items-center gap-1.5 rounded-md border bg-card px-2.5 py-2 text-sm font-medium hover:border-primary/60">
{copied ? <Check className="size-4 text-ok" /> : <Copy className="size-4" />} {copied ? done : label}
</button>
</span>
);
}