feat: add copy button to code component

This commit is contained in:
2024-08-17 01:00:17 +07:00
parent fb02d273c2
commit 61a07068d5
2 changed files with 18 additions and 6 deletions
+17 -5
View File
@@ -1,17 +1,29 @@
import { cn } from "@/lib/utils";
import { cn, copyToClipboard } from "@/lib/utils";
import React from "react";
import Button from "./button";
import { Copy } from "lucide-react";
type Props = React.ComponentPropsWithoutRef<"code">;
type Props = Omit<React.ComponentPropsWithoutRef<"code">, "children"> & {
children?: string;
};
const Code = ({ className, ...props }: Props) => {
const Code = ({ className, children, ...props }: Props) => {
return (
<code
className={cn(
"border border-base-content/20 px-4 py-3 rounded-lg font-mono block",
"border border-base-content/20 px-4 py-3 rounded-lg font-mono block relative",
className
)}
{...props}
/>
>
{children}
<Button
icon={Copy}
className="absolute right-0 top-0"
color="ghost"
onClick={() => copyToClipboard(children || "")}
/>
</code>
);
};