Skip to content

Instantly share code, notes, and snippets.

@miromannino
Created May 24, 2024 11:43
Show Gist options
  • Save miromannino/ce06dd85cecb6367449637f02281e844 to your computer and use it in GitHub Desktop.
Save miromannino/ce06dd85cecb6367449637f02281e844 to your computer and use it in GitHub Desktop.
Customizable Code Block Component with shadcn/ui
import React, { useRef } from "react";
import { cn } from "@/lib/utils";
import CodeEditor from "@uiw/react-textarea-code-editor";
export interface CodeBlockProps
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
language?: string;
darkMode?: boolean;
}
const CodeBlock = React.forwardRef<HTMLTextAreaElement, CodeBlockProps>(
({ className, ...props }, ref) => {
return (
<div
className={cn(
`relative flex min-h-[80px] w-full rounded border-input px-3 py-2
text-sm`,
className,
)}
style={{ backgroundColor: props.darkMode ? "#333333" : "#ffffff" }}
>
<CodeEditor
padding={0}
data-color-mode={props.darkMode ? "dark" : "light"}
style={{
backgroundColor: props.darkMode ? "#333333" : "#ffffff",
width: "100%",
fontFamily:
"ui-monospace,SFMono-Regular,SF Mono,Consolas,Liberation Mono,Menlo,monospace",
}}
ref={ref}
{...props}
/>
</div>
);
},
);
CodeBlock.displayName = "CodeBlock";
export { CodeBlock };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment