mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-06 05:40:02 -06:00
Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com> Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
"use client";
|
|
|
|
import { DocumentDuplicateIcon } from "@heroicons/react/24/outline";
|
|
import Prism from "prismjs";
|
|
import "prismjs/themes/prism.css";
|
|
import React, { useEffect } from "react";
|
|
import toast from "react-hot-toast";
|
|
|
|
import { cn } from "@formbricks/lib/cn";
|
|
|
|
import "./style.css";
|
|
|
|
interface CodeBlockProps {
|
|
children: React.ReactNode;
|
|
language: string;
|
|
customCodeClass?: string;
|
|
customEditorClass?: string;
|
|
showCopyToClipboard?: boolean;
|
|
}
|
|
|
|
const CodeBlock: React.FC<CodeBlockProps> = ({
|
|
children,
|
|
language,
|
|
customEditorClass = "",
|
|
customCodeClass = "",
|
|
showCopyToClipboard = true,
|
|
}) => {
|
|
useEffect(() => {
|
|
Prism.highlightAll();
|
|
}, [children]);
|
|
|
|
return (
|
|
<div className="group relative mt-4 rounded-md text-sm text-slate-200">
|
|
{showCopyToClipboard && (
|
|
<div className="absolute right-2 top-2 z-20 h-8 w-8 cursor-pointer rounded-md bg-slate-100 p-1.5 text-slate-600 hover:bg-slate-200">
|
|
<DocumentDuplicateIcon
|
|
className=""
|
|
onClick={() => {
|
|
const childText = children?.toString() || "";
|
|
navigator.clipboard.writeText(childText);
|
|
toast.success("Copied to clipboard");
|
|
}}
|
|
/>
|
|
</div>
|
|
)}
|
|
<pre className={customEditorClass}>
|
|
<code className={cn(`language-${language} whitespace-pre-wrap`, customCodeClass)}>{children}</code>
|
|
</pre>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default CodeBlock;
|