Code Block

The main structure of the Code Block component with header and content areas.

ReactMDXClientComponents

Preview

Preparing...

This is the main structure of the CodeBlock component, which includes a global wrapper, header and content area:

.tsx
<CodeBlock>
  <CodeBlockHeader>
    <CodeBlockGroup>
      <CodeBlockIcon language="bash" />
    </CodeBlockGroup>
  </CodeBlockHeader>
  <CodeBlockContent>
    {/* Your syntax highlighted code goes here. */}
  </CodeBlockContent>
</CodeBlock>

Installation

shadcn/ui

shadcn/ui Command
pnpm dlx shadcn@latest add https://code-blocks.pheralb.dev/r/code-block.json

Manual

Create the code-block.tsx component:

.tsx
import type { ComponentProps } from "react";

import { cn } from "@/utils/cn";
import { FileIcon } from "@react-symbols/icons/utils";

const CodeBlock = ({
  children,
  className,
  ...props
}: ComponentProps<"div">) => {
  return (
    <div
      className={cn(
        "not-prose",
        "flex w-full flex-col overflow-clip rounded-lg shadow-xs",
        "bg-neutral-200/40 dark:bg-neutral-800/70",
        "border border-neutral-200 dark:border-neutral-800",
        "text-neutral-950 dark:text-neutral-50",
        className,
      )}
      {...props}
    >
      {children}
    </div>
  );
};

type CodeBlockHeaderProps = ComponentProps<"div">;

const CodeBlockHeader = ({
  children,
  className,
  ...props
}: CodeBlockHeaderProps) => {
  return (
    <div
      className={cn(
        "not-prose", // Disable Markdown Styles
        "flex h-9 items-center justify-between px-2 py-1.5",
        "text-sm text-neutral-600 dark:text-neutral-400",
        className,
      )}
      {...props}
    >
      {children}
    </div>
  );
};

interface CodeBlockIconProps extends ComponentProps<"div"> {
  language?: string;
}

const CodeBlockIcon = ({ language, className }: CodeBlockIconProps) => {
  return (
    <FileIcon
      width={16}
      height={16}
      fileName={`.${language ?? ""}`}
      autoAssign={true}
      className={cn(className)}
    />
  );
};

type CodeBlockGroupProps = ComponentProps<"div">;

const CodeBlockGroup = ({
  children,
  className,
  ...props
}: CodeBlockGroupProps) => {
  return (
    <div
      className={cn(
        "flex items-center space-x-2",
        "text-sm text-neutral-600 dark:text-neutral-400",
        className,
      )}
      {...props}
    >
      {children}
    </div>
  );
};

const CodeBlockContent = ({
  className,
  children,
  ...props
}: ComponentProps<"div">) => {
  return (
    <div
      className={cn(
        "max-h-96 overflow-y-auto",
        "bg-neutral-50 dark:bg-neutral-900",
        "rounded-lg font-mono text-sm leading-5 whitespace-pre",
        className,
      )}
      {...props}
    >
      {children}
    </div>
  );
};

export {
  CodeBlock,
  CodeBlockHeader,
  CodeBlockIcon,
  CodeBlockGroup,
  CodeBlockContent,
};

Usage

.tsx
import {
  CodeBlock,
  CodeBlockHeader,
  CodeBlockIcon,
  CodeBlockGroup,
  CodeBlockContent,
} from "@/components/code-block/code-block";

<CodeBlock>
  <CodeBlockHeader>
    <CodeBlockGroup>
      <CodeBlockIcon language="bash" />
    </CodeBlockGroup>
  </CodeBlockHeader>
  <CodeBlockContent>
    {/* Your syntax highlighted code goes here. */}
  </CodeBlockContent>
</CodeBlock>;

Components

CodeBlock

The main wrapper for the code block.

.tsx
<CodeBlock>{/* ... */}</CodeBlock>

CodeBlockHeader

The header section of the code block, typically used for displaying the language icon, title, and action buttons.

.tsx
<CodeBlockHeader>{/* ... */}</CodeBlockHeader>

CodeBlockGroup

A container for grouping elements in the header, such as the language icon and title.

.tsx
<CodeBlockHeader>
  <CodeBlockGroup>{/* ... */}</CodeBlockGroup>
</CodeBlockHeader>

CodeBlockIcon

Displays the icon for the specified programming language. Internal icons are provided by @react-symbols/icons using <FileIcon /> component.

.tsx
<CodeBlockIcon language="js" />

CodeBlockContent

The content area of the code block where the syntax-highlighted code is displayed.

.tsx
<CodeBlockContent>{/* ... */}</CodeBlockContent>

Props

React Props

CodeBlock

PropTypeRequired
Included: DOMAttributes<HTMLDivElement>

CodeBlockHeader

PropTypeRequired
Included: DOMAttributes<HTMLDivElement>

CodeBlockIcon

PropTypeRequired
languagestringNo
Included: DOMAttributes<HTMLDivElement>

CodeBlockGroup

PropTypeRequired
Included: DOMAttributes<HTMLDivElement>

CodeBlockContent

PropTypeRequired
Included: DOMAttributes<HTMLDivElement>