peppa/apps/web/components/CopyCode.tsx
2026-11-15 17:11:01 +05:00

29 lines
1.2 KiB
TypeScript

"use client";
import { useState } from "react";
import { Check, Copy } from "lucide-react";
/** PEP-126: a one-time code, large, with «Скопировать» — the person pastes it on another site. */
export function CopyCode({ code }: { code: string }) {
const [copied, setCopied] = useState(false);
async function copy() {
try {
await navigator.clipboard.writeText(code);
setCopied(true);
setTimeout(() => setCopied(false), 2500);
} catch {
/* no clipboard access: the code is selectable, the person copies it by hand */
}
}
return (
<span className="inline-flex flex-wrap items-center gap-2">
<span className="rounded-md border bg-muted px-3 py-1.5 font-mono text-xl font-semibold tracking-widest select-all" data-code>
{code}
</span>
<button type="button" onClick={() => void copy()} className="inline-flex items-center gap-1.5 rounded-md border bg-card px-2.5 py-1.5 text-sm font-medium hover:border-primary/60">
{copied ? <Check className="size-4 text-ok" /> : <Copy className="size-4" />} {copied ? "Скопировано" : "Скопировать код"}
</button>
</span>
);
}