code

A basic code component that can be used to display code with syntax highlighting.

1 const [value, setValue] = useState("");

Installation

npx shadcn add "https://registry.niels.foo/code.json"

With Markdown

You can use the Code component within the Markdown component to display syntax highlighted code properly within a Markdown document.

To do this edit the components/ui/markdown.tsx component and uncomment the code that is there.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 import ReactMarkdown, { Options } from "react-markdown"; import remarkGfm from "remark-gfm"; import { cn } from "@/lib/utils"; export type MarkdownProps = Options; export function Markdown({ children, className, ...props }: MarkdownProps) { return ( <ReactMarkdown className={cn( "prose prose-neutral break-words dark:prose-invert prose-p:leading-relaxed prose-pre:overflow-visible prose-pre:bg-inherit prose-pre:p-0 prose-p:m-0", className )} remarkPlugins={[remarkGfm]} components={{ p({ children }) { return <p className="mb-2 last:mb-0">{children}</p>; }, code({ children, className, ...rest }) { const match = /language-(\w+)/.exec(className || ""); if (!match) { return ( <code {...rest} className={cn("font-semibold text-primary", className)} > {children} </code> ); } const code = String(children).replace(/\n$/, ""); const language = match[1]; return ( <Code language={language}> <CodeHeader> <CodeLanguage /> <CodeCopyButton /> </CodeHeader> <CodeContent showLineNumbers>{code}</CodeContent> </Code> ); }, }} {...props} > {children} </ReactMarkdown> ); }

Source

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 "use client"; import { Clipboard, ClipboardCheck } from "lucide-react"; import { useTheme } from "next-themes"; import * as React from "react"; import { PrismAsyncLight as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark, prism } from "react-syntax-highlighter/dist/cjs/styles/prism"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; interface CodeContentContextState { language: string; content: string; setContent: React.Dispatch<React.SetStateAction<string>>; } const CodeContentContext = React.createContext<CodeContentContextState>({ language: "bash", content: "", setContent: () => {}, }); function useCodeContent() { return React.useContext(CodeContentContext); } const Code = React.forwardRef< HTMLDivElement, React.HTMLAttributes<HTMLDivElement> & { language?: string } >(({ className, language = "bash", children, ...props }, ref) => { const [content, setContent] = React.useState(""); return ( <CodeContentContext.Provider value={{ language, content, setContent, }} > <div ref={ref} className={cn( "rounded-lg border bg-neutral-50 dark:bg-neutral-900 shadow overflow-hidden", className )} {...props} > {children} </div> </CodeContentContext.Provider> ); }); Code.displayName = "Code"; const CodeHeader = React.forwardRef< HTMLDivElement, React.HTMLAttributes<HTMLDivElement> >(({ className, ...props }, ref) => ( <div ref={ref} className={cn( "flex items-center justify-between border-b bg-neutral-200 p-4 dark:bg-neutral-700 text-muted-foreground", className )} {...props} /> )); CodeHeader.displayName = "CodeHeader"; const CodeLanguage = React.forwardRef< HTMLLabelElement, React.HTMLAttributes<HTMLLabelElement> >(({ className, children, ...props }, ref) => { const { language } = useCodeContent(); return ( <label ref={ref} className={cn("text-xs lowercase !leading-none", className)} {...props} > {language || children} </label> ); }); CodeLanguage.displayName = "CodeLanguage"; type CodeCopyButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement>; const CodeCopyButton = React.forwardRef<HTMLButtonElement, CodeCopyButtonProps>( ({ className, ...props }, ref) => { const [isCopied, setIsCopied] = React.useState(false); const { content } = useCodeContent(); const copyToClipboard = React.useCallback(() => { if (typeof window === "undefined" || !navigator.clipboard?.writeText) { return; } navigator.clipboard.writeText(content).then(() => { setIsCopied(true); setTimeout(() => setIsCopied(false), 1000); }); }, [content]); return ( <Button ref={ref} variant="ghost" onClick={copyToClipboard} className={cn("h-4 w-4 cursor-pointer hover:bg-inherit", className)} size="icon" {...props} > {isCopied ? ( <ClipboardCheck className="h-4 w-4" /> ) : ( <Clipboard className="h-4 w-4" /> )} </Button> ); } ); CodeCopyButton.displayName = "CodeCopyButton"; interface CodeContentProps { showLineNumbers?: boolean; children: string; } const CodeContent = React.forwardRef<HTMLDivElement, CodeContentProps>( ({ showLineNumbers = true, children }, ref) => { const { resolvedTheme = "light" } = useTheme(); const { language, setContent } = useCodeContent(); React.useEffect(() => { setContent(children); }, [children, setContent]); return ( <div ref={ref}> <SyntaxHighlighter language={language} style={resolvedTheme === "dark" ? oneDark : prism} PreTag="div" showLineNumbers={showLineNumbers} customStyle={{ margin: 0, width: "100%", background: "transparent", padding: "1.5rem 1rem", }} codeTagProps={{ style: { fontSize: "0.9rem", }, }} > {children} </SyntaxHighlighter> </div> ); } ); CodeContent.displayName = "CodeContent"; export { Code, CodeContent, CodeCopyButton, CodeHeader, CodeLanguage };