"use client" import { cn } from "@/lib/utils" import React, { useEffect, useState } from "react" import { codeToHtml } from "shiki" export type CodeBlockProps = { children?: React.ReactNode className?: string } & React.HTMLProps function CodeBlock({ children, className, ...props }: CodeBlockProps) { return (
{children}
) } export type CodeBlockCodeProps = { code: string language?: string theme?: string className?: string } & React.HTMLProps function CodeBlockCode({ code, language = "tsx", theme = "github-light", className, ...props }: CodeBlockCodeProps) { const [highlightedHtml, setHighlightedHtml] = useState(null) useEffect(() => { async function highlight() { if (!code) { setHighlightedHtml("
") return } const html = await codeToHtml(code, { lang: language, theme }) setHighlightedHtml(html) } highlight() }, [code, language, theme]) const classNames = cn( "w-full overflow-x-auto text-[13px] [&>pre]:px-4 [&>pre]:py-4", className ) // SSR fallback: render plain code if not hydrated yet return highlightedHtml ? (
) : (
        {code}
      
) } export type CodeBlockGroupProps = React.HTMLAttributes function CodeBlockGroup({ children, className, ...props }: CodeBlockGroupProps) { return (
{children}
) } export { CodeBlockGroup, CodeBlockCode, CodeBlock }