mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
## 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
43 lines
970 B
TypeScript
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>
|
|
);
|
|
};
|