peppa/apps/web/components/CopyCode.tsx
Асель 5bc11bc320 Merge main into task/pep-98: PEP-126 texts through the dictionary
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 18:02:24 +05:00

31 lines
1.2 KiB
TypeScript

"use client";
import { useState } from "react";
import { Check, Copy } from "lucide-react";
import { useT } from "@/components/I18n";
/** PEP-126: a one-time code, large, with «Скопировать» — the person pastes it on another site. */
export function CopyCode({ code }: { code: string }) {
const t = useT();
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 ? t("Скопировано") : t("Скопировать код")}
</button>
</span>
);
}