import React, { useState, ReactNode } from "react"; type CopyableProps = { text: string; children: ReactNode; bottom?: boolean; right?: boolean; }; type State = "hide" | "copy" | "copied"; function Popover({ state, bottom, right, }: { state: State; bottom?: boolean; right?: boolean; }) { if (state === "hide") return null; const text = state === "copy" ? "Copy" : "Copied!"; return (
{text}
); } function Copyable({ bottom, right, text, children }: CopyableProps) { const [state, setState] = useState("hide"); const copyToClipboard = () => navigator.clipboard.writeText(text); const handleClick = () => copyToClipboard().then(() => { setState("copied"); setTimeout(() => setState("hide"), 1000); }); return (
setState("copy")} onMouseOut={() => state === "copy" && setState("hide")} > {children}
); } export default Copyable;