import { Button } from '@/components/button';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/tooltip';
import { CheckIcon, CopyIcon } from '@/icons';
import { cn } from '@/lib/utils';
import { useMessages } from '~/i18n';
import { useCopy } from '../hooks/use-copy';
/**
* Copy-to-clipboard, in the three shapes the showcase needs: a labelled button
* you can put anywhere, the code line with a button on its right that the
* component page shows for its import statement, and the multi-line block that
* shows a generated snippet.
*
* All three are the kit's own `Button`, and the hint is the kit's `Tooltip` —
* chrome uses kit components. The one thing that is not the kit's is the
* clipboard call itself, which is `./use-copy` — shared with `chrome/ui/share.tsx`
* and where the doubled feedback is explained.
*/
export function CopyButton({
text,
label,
title,
iconOnly = false,
className,
}: {
/* A thunk defers the work to the click. A 35-cell matrix would otherwise
serialise every cell on every render just to fill a button nobody pressed. */
text: string | (() => string);
/** Defaults to the catalogue's plain "Copy" — most callers want exactly that. */
label?: string;
title?: string;
/**
* Drop the word and keep the glyph, for the square button a matrix cell has
* room for. The label still reaches assistive technology as the accessible
* name, so this is a visual choice rather than a semantic one.
*/
iconOnly?: boolean;
className?: string;
}) {
const [copied, copy] = useCopy();
const m = useMessages();
const word = label ?? m.common.copy;
const button = (
);
if (!title) return button;
return (
{code}
{text}