shadimage
slidecn
uicode-blocksource: ok · hit

Code Block

A syntax-highlighted code block with filename, line numbers, and copy button.

npx shadcn add "https://slidecn.vercel.app/r/code-block.json"
free

Visual style

terminal-mono

Tags auto-generated by rules on 9/24/2026 — empty means no evidence, not lack of support.

Dependencies

Bundle weight · light
@revealjs/reactreveal.js

Files1 · source cached 10/8/2026, 9:03:52 PM

registry/slidecn/ui/code-block.tsx
"use client";

import { Code } from "@revealjs/react";
import type { CodeProps } from "@revealjs/react";
import { FileCode, Copy } from "lucide-react";

import { cn } from "@/lib/utils";

interface CodeBlockProps extends Omit<CodeProps, "children"> {
  children: string;
  language: string;
  lineNumbers?: boolean | string;
  filename?: string;
  showCopy?: boolean;
  autoAnimateId?: string;
  startFrom?: number;
  trim?: boolean;
}

const CodeBlock = ({
  children,
  language,
  lineNumbers,
  filename,
  showCopy = true,
  autoAnimateId,
  startFrom,
  trim = true,
  className,
  ...codeProps
}: CodeBlockProps) => {
  const handleCopy = async () => {
    try {
      await navigator.clipboard.writeText(children);
    } catch {
      //noop
    }
  };

  return (
    <div
      className={cn("overflow-hidden rounded-lg border bg-muted/50", className)}
    >
      {filename && (
        <div className="flex items-center gap-2 border-b bg-muted px-4 py-2 text-xs text-muted-foreground">
          <FileCode className="size-3.5" />
          <span>{filename}</span>
        </div>
      )}
      <div className="relative">
        <pre data-id={autoAnimateId}>
          <Code
            language={language}
            lineNumbers={lineNumbers}
            startFrom={startFrom}
            trim={trim}
            {...codeProps}
          >
            {children}
          </Code>
        </pre>
        {showCopy && (
          <button
            type="button"
            onClick={handleCopy}
            className="absolute right-2 top-2 flex size-7 items-center justify-center rounded-md text-muted-foreground opacity-0 transition-opacity hover:bg-muted hover:text-foreground group-hover:opacity-100"
            aria-label="Copy code"
          >
            <Copy className="size-3.5" />
          </button>
        )}
      </div>
    </div>
  );
};

export { CodeBlock };

More from slidecn

Browse all components in this library →