2026-11-19 16:59:43 -06:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useStrings } from "../hooks/useStrings";
|
|
|
|
|
import { Button, type ButtonProps } from "./Button";
|
|
|
|
|
import { Tooltip } from "./Tooltip";
|
|
|
|
|
|
|
|
|
|
const COPIED_TOOLTIP_DURATION = 2000;
|
2024-08-11 12:44:16 -05:00
|
|
|
|
|
|
|
|
interface CopyButtonProps extends ButtonProps {
|
|
|
|
|
copy: () => string | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 16:59:43 -06:00
|
|
|
export function CopyButton({ copy, style, ...props }: CopyButtonProps) {
|
2024-08-11 12:44:16 -05:00
|
|
|
const [tooltipOpen, setTooltipOpen] = useState(false);
|
2026-11-19 16:59:43 -06:00
|
|
|
const strings = useStrings();
|
2024-08-11 12:44:16 -05:00
|
|
|
|
|
|
|
|
return (
|
2025-01-07 13:09:05 -06:00
|
|
|
<Tooltip
|
|
|
|
|
open={tooltipOpen}
|
2026-11-19 16:59:43 -06:00
|
|
|
tooltip={strings.website.common.copy_button.copied}
|
|
|
|
|
style={style}
|
2025-01-07 13:09:05 -06:00
|
|
|
>
|
2024-08-11 12:44:16 -05:00
|
|
|
<Button
|
|
|
|
|
{...props}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const content = copy();
|
|
|
|
|
|
|
|
|
|
if (content === undefined) return;
|
|
|
|
|
|
|
|
|
|
navigator.clipboard.writeText(content);
|
|
|
|
|
setTooltipOpen(true);
|
2026-11-19 16:59:43 -06:00
|
|
|
setTimeout(() => setTooltipOpen(false), COPIED_TOOLTIP_DURATION);
|
2024-08-11 12:44:16 -05:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
}
|