tianji/src/client/components/CodeBlock.tsx

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-05-15 12:32:25 +00:00
import { useEvent } from '@/hooks/useEvent';
import React, { useState } from 'react';
import { Button } from './ui/button';
import { LuCopy, LuCopyCheck } from 'react-icons/lu';
import { toast } from 'sonner';
import { useTranslation } from '@i18next-toolkit/react';
2024-06-24 14:39:36 +00:00
import { ScrollBar } from './ui/scroll-area';
2024-05-15 12:32:25 +00:00
export const CodeBlock: React.FC<{
code: string;
}> = React.memo((props) => {
const [copied, setCopied] = useState(false);
const { t } = useTranslation();
const handleCopy = useEvent(() => {
window.navigator.clipboard.writeText(props.code);
toast(t('Copied into clipboard!'));
setCopied(true);
});
return (
2024-06-24 14:39:36 +00:00
<div className="group relative w-full overflow-auto">
2024-05-15 12:32:25 +00:00
<pre className="rounded-sm border border-zinc-800 bg-zinc-900 p-3 pr-12 text-sm">
<code>{props.code}</code>
</pre>
<Button
className="absolute right-1 top-1 bg-opacity-50 opacity-0 transition-opacity group-hover:opacity-100 dark:bg-opacity-50"
variant="outline"
size="icon"
Icon={copied ? LuCopyCheck : LuCopy}
onClick={handleCopy}
/>
</div>
);
});
CodeBlock.displayName = 'CodeBlock';