import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; import js from "react-syntax-highlighter/dist/esm/languages/hljs/javascript"; import ruby from "react-syntax-highlighter/dist/esm/languages/hljs/ruby"; import php from "react-syntax-highlighter/dist/esm/languages/hljs/php"; import python from "react-syntax-highlighter/dist/esm/languages/hljs/python"; // import { nightOwl } from "react-syntax-highlighter/dist/esm/styles/hljs"; import codeTheme from "../code-theme"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "./tabs"; import { Button } from "./button"; import { ClipboardCopy, Check } from "lucide-react"; import { useState } from "react"; import { cn } from "../lib/utils"; export type Language = "js" | "ruby" | "php" | "python" | "curl"; export type CodeBlock = { language: Language; title?: string; code: string; }; type CodeProps = { codeBlocks: CodeBlock[]; codeClassName?: string; }; SyntaxHighlighter.registerLanguage("js", js); SyntaxHighlighter.registerLanguage("ruby", ruby); SyntaxHighlighter.registerLanguage("php", php); SyntaxHighlighter.registerLanguage("python", python); export const Code: React.FC = ({ codeBlocks, codeClassName }) => { const [selectedTab, setSelectedTab] = useState( codeBlocks[0]?.language ?? "js" ); const [isCopied, setIsCopied] = useState(false); const copyToClipboard = async (code: string) => { try { await navigator.clipboard.writeText(code); setIsCopied(true); setTimeout(() => setIsCopied(false), 2000); // Reset the icon back to clipboard after 2 seconds } catch (err) { alert("Failed to copy code"); } }; return (
setSelectedTab(val as Language)} >
{codeBlocks.map((block) => ( {block.title || block.language} ))}
{codeBlocks.map((block) => (
{block.code}
))}
); };