refactor(component): migrate code block with copy feature to a more reusable pre component

This commit is contained in:
cfngc4594 2025-03-07 11:34:17 +08:00
parent 58a25b8a9c
commit c0a876322c

View File

@ -1,18 +1,20 @@
"use client"; "use client";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { useRef, useState } from "react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { CheckIcon, CopyIcon } from "lucide-react"; import { CheckIcon, CopyIcon } from "lucide-react";
import { ReactNode, useRef, useState } from "react";
interface CodeBlockWithCopyProps { interface PreProps {
children: React.ReactNode; children?: ReactNode,
className?: string;
} }
export function CodeBlockWithCopy({ export function Pre({
children, children,
className,
...props ...props
}: CodeBlockWithCopyProps) { }: PreProps) {
const preRef = useRef<HTMLPreElement>(null); const preRef = useRef<HTMLPreElement>(null);
const [copied, setCopied] = useState<boolean>(false); const [copied, setCopied] = useState<boolean>(false);
const [hovered, setHovered] = useState<boolean>(false); const [hovered, setHovered] = useState<boolean>(false);
@ -65,7 +67,7 @@ export function CodeBlockWithCopy({
<CopyIcon size={16} aria-hidden="true" /> <CopyIcon size={16} aria-hidden="true" />
</div> </div>
</Button> </Button>
<pre ref={preRef} {...props}> <pre ref={preRef} className={className} {...props}>
{children} {children}
</pre> </pre>
</div> </div>