31 lines
1.2 KiB
TypeScript
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>
|
|
);
|
|
}
|