Files
blitzkit/packages/website/src/components/CopyableCode.tsx
T

40 lines
965 B
TypeScript
Raw Normal View History

import { Code, Tooltip, type CodeProps } from "@radix-ui/themes";
import { useState } from "react";
2026-03-25 11:18:53 +01:00
import { useLocale } from "../hooks/useLocale";
export interface CopyableCodeProps extends CodeProps {
2026-03-25 11:18:53 +01:00
copy?: string;
}
export function CopyableCode({
copy,
onClick,
onPointerLeave,
style,
children,
...props
}: CopyableCodeProps) {
2026-03-25 11:18:53 +01:00
const [copied, setCopied] = useState(false);
const { strings } = useLocale();
return (
<Tooltip open={copied} content={strings.website.common.copy_button.copied}>
<Code
{...props}
style={{ ...(copy ? { cursor: "copy" } : {}), ...style }}
onClick={(event) => {
onClick?.(event);
2026-03-25 11:18:53 +01:00
if (!copy) return;
navigator.clipboard.writeText(copy);
setCopied(true);
}}
onPointerLeave={(event) => {
onPointerLeave?.(event);
setCopied(false);
}}
2026-03-25 11:18:53 +01:00
>
{children}
</Code>
</Tooltip>
);
}