Files
coder/site/src/components/CopyButton/CopyButton.tsx
T
Michael Smith 156f985fb0 fix(site): update useClipboard to work better with effect logic (#20183)
## Changes made
- Updated `useClipboard` API to require passing the text in via the
`copyToClipboard` function, rather than requiring that the text gets
specified in render logic
- Ensured that the `copyToClipboard` function always stays stable across
all React lifecycles
- Updated all existing uses to use the new function signatures
- Updated all tests and added new cases
2025-10-06 15:41:57 -04:00

43 lines
970 B
TypeScript

import { Button, type ButtonProps } from "components/Button/Button";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "components/Tooltip/Tooltip";
import { useClipboard } from "hooks/useClipboard";
import { CheckIcon, CopyIcon } from "lucide-react";
import type { FC } from "react";
type CopyButtonProps = ButtonProps & {
text: string;
label: string;
};
export const CopyButton: FC<CopyButtonProps> = ({
text,
label,
...buttonProps
}) => {
const { showCopiedSuccess, copyToClipboard } = useClipboard();
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Button
size="icon"
variant="subtle"
onClick={() => copyToClipboard(text)}
{...buttonProps}
>
{showCopiedSuccess ? <CheckIcon /> : <CopyIcon />}
<span className="sr-only">{label}</span>
</Button>
</TooltipTrigger>
<TooltipContent>{label}</TooltipContent>
</Tooltip>
</TooltipProvider>
);
};