|
| 1 | +import type { ReactNode } from "react"; |
| 2 | + |
| 3 | +import { CheckIcon } from "@heroicons/react/16/solid"; |
| 4 | +import { ClipboardIcon } from "@heroicons/react/24/outline"; |
| 5 | +import { useState } from "react"; |
| 6 | +import toast from "react-hot-toast"; |
| 7 | + |
| 8 | +import { ToastContentSuccess } from "@/components/Toast"; |
| 9 | + |
| 10 | +type CopyablePanelProps = { |
| 11 | + children: ReactNode; |
| 12 | + /** |
| 13 | + * Additional class names to apply to the component. |
| 14 | + */ |
| 15 | + className?: string; |
| 16 | + /** |
| 17 | + * Raw text to be copied to clipboard. |
| 18 | + */ |
| 19 | + copyText: string; |
| 20 | + /** |
| 21 | + * The title to show in the copy confirmation toast. |
| 22 | + * @default "Text" |
| 23 | + */ |
| 24 | + copyTitle?: string; |
| 25 | +}; |
| 26 | + |
| 27 | +const styleConfig = { |
| 28 | + container: { |
| 29 | + base: "relative overflow-auto rounded-md bg-slate-50 dark:bg-slate-800", |
| 30 | + font: { fontFamily: "var(--font-family-monospace, monospace)" }, |
| 31 | + }, |
| 32 | + content: { |
| 33 | + base: "relative overflow-x-auto overscroll-y-auto p-1 pl-2 text-xs", |
| 34 | + }, |
| 35 | + header: { |
| 36 | + base: "flex items-center justify-end bg-slate-100 px-2 py-1 text-xs dark:bg-slate-700", |
| 37 | + textAlign: { textAlign: "right" as const }, |
| 38 | + }, |
| 39 | + icon: { |
| 40 | + base: "h-3 w-3", |
| 41 | + check: "text-green-500", |
| 42 | + clipboard: |
| 43 | + "text-slate-500 dark:text-slate-400 hover:text-brand-primary dark:hover:text-brand-primary", |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * A panel that wraps copyable content with a copy button. |
| 49 | + */ |
| 50 | +export default function CopyablePanel({ |
| 51 | + children, |
| 52 | + className, |
| 53 | + copyText, |
| 54 | + copyTitle = "Text", |
| 55 | +}: CopyablePanelProps) { |
| 56 | + const [isCopied, setIsCopied] = useState(false); |
| 57 | + |
| 58 | + const copyToClipboard = () => { |
| 59 | + navigator.clipboard.writeText(copyText).then( |
| 60 | + () => { |
| 61 | + setIsCopied(true); |
| 62 | + toast.custom((t) => ( |
| 63 | + <ToastContentSuccess |
| 64 | + message={`${copyTitle} copied to clipboard`} |
| 65 | + t={t} |
| 66 | + /> |
| 67 | + )); |
| 68 | + setTimeout(() => { |
| 69 | + setIsCopied(false); |
| 70 | + }, 2000); |
| 71 | + }, |
| 72 | + (err) => { |
| 73 | + console.error("Failed to copy text: ", err); |
| 74 | + }, |
| 75 | + ); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div |
| 80 | + className={`${styleConfig.container.base} ${className || ""}`} |
| 81 | + style={styleConfig.container.font} |
| 82 | + > |
| 83 | + <div |
| 84 | + className={styleConfig.header.base} |
| 85 | + style={styleConfig.header.textAlign} |
| 86 | + > |
| 87 | + <button |
| 88 | + className="inline-flex cursor-pointer items-center rounded p-1" |
| 89 | + data-testid="text-copy-button" |
| 90 | + onClick={copyToClipboard} |
| 91 | + tabIndex={0} |
| 92 | + title="Copy to clipboard" |
| 93 | + type="button" |
| 94 | + > |
| 95 | + {isCopied ? ( |
| 96 | + <CheckIcon |
| 97 | + aria-hidden="true" |
| 98 | + className={`${styleConfig.icon.base} ${styleConfig.icon.check}`} |
| 99 | + /> |
| 100 | + ) : ( |
| 101 | + <ClipboardIcon |
| 102 | + aria-hidden="true" |
| 103 | + className={`${styleConfig.icon.base} ${styleConfig.icon.clipboard}`} |
| 104 | + /> |
| 105 | + )} |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + <div className={styleConfig.content.base}>{children}</div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
0 commit comments