Code Block

PreviousNext

A versatile code block component supporting package manager commands and syntax highlighting with themes.

# Hello World in Python
def main():
print("Hello, World!")
if __name__ == "__main__":
main()

Installation

pnpm dlx shadcn@latest add https://deltacomponents.dev/r/code-block.json

Usage

import { CodeBlock } from "@/components/ui/code-block"
 
export default function Example() {
  return (
    <CodeBlock
      code={`print("Hello, World!")`}
      language="python"
      filename="hello.py"
      showLineNumbers={true}
    />
  )
}

Features

  • Dual Mode: Package manager commands or syntax-highlighted code
  • Syntax Highlighting: Powered by Prism with custom themes
  • Package Manager Tabs: Support for npm, yarn, pnpm, and bun
  • Copy to Clipboard: One-click copy functionality with visual feedback
  • Line Numbers: Optional line numbering for code blocks
  • Custom Themes: Light/dark adaptive themes with hover effects
  • Terminal Styling: Command-line interface appearance
  • Responsive: Horizontal scrolling for long content
  • Flexible: Shows only provided package managers or code content

Examples

Package Manager Commands

npm install @radix-ui/react-dropdown-menu

Interactive Theme Demo

hello.go
1package main
2
3import "fmt"
4
5func main() {
6 fmt.Println("Hello, World!")
7}

API Reference

Props

PropTypeDefaultDescription
Package Manager Props
npmstring-npm command to display
yarnstring-yarn command to display
pnpmstring-pnpm command to display
bunstring-bun command to display
defaultPackageManagerPackageManager"npm"Default selected package manager
Code Highlighting Props
codestring-Code content to highlight
languagestring"typescript"Programming language for syntax highlighting
filenamestring-Filename to display in header
showLineNumbersbooleantrueWhether to show line numbers
themePrismTheme-Custom Prism theme object
adaptiveThemeobject-Object with light and dark theme variants
General Props
classNamestring-Additional CSS classes for styling

Types

type PackageManager = "npm" | "yarn" | "pnpm" | "bun"
 
interface AdaptiveTheme {
  light: PrismTheme
  dark: PrismTheme
}

Theme Structure

Custom themes follow the Prism theme structure:

const customTheme: PrismTheme = {
  plain: {
    color: "#e6e6fa",
    backgroundColor: "#1a1a2e",
  },
  styles: [
    {
      types: ["comment"],
      style: {
        color: "#6a7b9a",
        fontStyle: "italic",
      },
    },
    // ... more styles
  ],
}