A lightweight syntax-highlighted code display component with copy functionality and optional file titles.
Overview
The Code Snippet component provides a clean, lightweight way to display syntax-highlighted code blocks with built-in copy functionality. It uses Prism for syntax highlighting and includes line numbers and hover effects for better code readability.
Installation
pnpm dlx shadcn@latest add https://deltacomponents.dev/r/code-snippet.json
Usage
1"use client"23import { CodeSnippet } from "@/registry/media/code-snippet"45export function Component() {6 const code = `function hello() {7 console.log("Hello, world!");8}`910 return (11 <CodeSnippet12 title="hello.js"13 code={code}14 language="javascript"15 showLineNumbers={true}16 />17 )18}
Custom Theme Example
1import { PrismTheme } from "prism-react-renderer"23import { CodeSnippet } from "@/registry/media/code-snippet"45const lightTheme: PrismTheme = {6 plain: {7 color: "#24292e",8 backgroundColor: "#FFFFFF",9 },10 styles: [11 {12 types: ["comment"],13 style: { color: "#6a737d", fontStyle: "italic" },14 },15 {16 types: ["keyword"],17 style: { color: "#d73a49" },18 },19 // ... more styles20 ],21}2223export function CustomThemeExample() {24 return (25 <CodeSnippet26 code="const greeting = 'Hello, world!';"27 language="javascript"28 theme={lightTheme}29 showLineNumbers={false}30 />31 )32}
Tabbed Code Snippets
The CodeSnippet component supports tabbed interfaces for displaying multiple related code examples or different approaches to the same task.
1"use client"23import { useState } from "react"45import { CodeSnippet } from "@/registry/media/code-snippet"67export function TabbedExample() {8 const [activeTab, setActiveTab] = useState("npm")910 const installationTabs = {11 npm: {12 code: "npm install @radix-ui/react-dialog lucide-react",13 language: "bash",14 },15 pnpm: {16 code: "pnpm add @radix-ui/react-dialog lucide-react",17 language: "bash",18 },19 yarn: {20 code: "yarn add @radix-ui/react-dialog lucide-react",21 language: "bash",22 },23 bun: {24 code: "bun add @radix-ui/react-dialog lucide-react",25 language: "bash",26 },27 }2829 return (30 <CodeSnippet31 code={installationTabs[activeTab].code}32 language="bash"33 showLineNumbers={false}34 tabs={installationTabs}35 activeTab={activeTab}36 onTabChange={setActiveTab}37 />38 )39}
Adaptive Theme Example
1import { PrismTheme } from "prism-react-renderer"23import { CodeSnippet } from "@/registry/media/code-snippet"45const lightTheme: PrismTheme = {6 plain: { color: "#24292f", backgroundColor: "#ffffff" },7 styles: [8 { types: ["keyword"], style: { color: "#cf222e" } },9 { types: ["string"], style: { color: "#0a3069" } },10 // ... more light theme styles11 ],12}1314const darkTheme: PrismTheme = {15 plain: { color: "#e6edf3", backgroundColor: "#0d1117" },16 styles: [17 { types: ["keyword"], style: { color: "#ff7b72" } },18 { types: ["string"], style: { color: "#a5d6ff" } },19 // ... more dark theme styles20 ],21}2223export function AdaptiveThemeExample() {24 return (25 <CodeSnippet26 code="const greeting = 'Hello, world!';"27 language="javascript"28 adaptiveTheme={{ light: lightTheme, dark: darkTheme }}29 showLineNumbers={true}30 />31 )32}
API Reference
CodeSnippet
Name | Type | Default | Description |
---|---|---|---|
title | string | - | Optional title displayed in the header |
code | string | - | The code content to display |
language | string | "typescript" | Programming language for syntax highlighting |
className | string | - | Additional CSS classes |
border | boolean | true | Whether to show the border around the component |
theme | PrismTheme | - | Custom theme object for syntax highlighting |
showLineNumbers | boolean | true | Whether to display line numbers |
adaptiveTheme | { light: PrismTheme, dark: PrismTheme } | - | Themes that automatically adapt to dark/light mode |
tabs | { [key: string]: { code: string; language?: string } } | - | Tab data for multi-code display |
activeTab | string | - | Currently active tab key |
onTabChange | (tab: string) => void | - | Callback fired when tab is changed |
Theme Schema
The theme
prop accepts a PrismTheme
object that defines colors and styles for syntax highlighting. The theme object follows this schema:
1{2 "plain": {3 "color": "#24292e",4 "backgroundColor": "#FFFFFF"5 },6 "styles": [7 {8 "types": ["comment"],9 "style": {10 "color": "#6a737d",11 "fontStyle": "italic"12 }13 },14 {15 "types": ["keyword", "property", "property-access", "attr-name"],16 "style": {17 "color": "#d73a49"18 }19 },20 {21 "types": ["tag"],22 "style": {23 "color": "#22863a"24 }25 },26 {27 "types": ["punctuation", "symbol", "dom"],28 "style": {29 "color": "#24292e"30 }31 },32 {33 "types": ["definition", "function"],34 "style": {35 "color": "#6f42c1"36 }37 },38 {39 "types": ["string", "char", "attr-value"],40 "style": {41 "color": "#032f62"42 }43 },44 {45 "types": ["static", "number"],46 "style": {47 "color": "#005cc5"48 }49 }50 ]51}
Theme Properties
plain
: Base styles applied to all code contentcolor
: Default text colorbackgroundColor
: Background color for the code area and header
styles
: Array of style rules for different token typestypes
: Array of token types to target (e.g., "comment", "keyword", "string")style
: CSS properties to apply (color, fontStyle, fontWeight, etc.)
Common Token Types
comment
- Code commentskeyword
- Language keywords (if, function, const, etc.)string
- String literalsnumber
- Numeric valuesfunction
- Function namesproperty
- Object propertiespunctuation
- Brackets, semicolons, etc.tag
- HTML/JSX tagsattr-name
- HTML/JSX attribute namesattr-value
- HTML/JSX attribute values
Examples
Basic Code Snippet
Simple code display with syntax highlighting and copy functionality.
Theme Customization
Switch between dark and light themes for different visual preferences.
Display Options
Toggle various display options including title, border, and line numbers.
C++20 with Custom Theme
Modern C++20 features showcased with a custom purple-blue theme optimized for C++ syntax.
Adaptive Dark/Light Mode
Automatically adapts to your system's dark/light mode preference using the adaptiveTheme
prop.
Tabbed Code Snippets
Display multiple related code examples with tab navigation, perfect for showing installation commands across different package managers.
Features
Syntax Highlighting
- Powered by Prism React Renderer
- Supports multiple programming languages
- Dark theme optimized for readability
Copy Functionality
- One-click copy to clipboard
- Visual feedback with check icon
- Positioned contextually based on content
Line Numbers
- Automatic line numbering
- Hover effects for better interaction
- Proper spacing and alignment
Tabbed Interface
- Support for multiple code examples in tabs
- Seamless tab switching with state management
- Compact tab design with underlined active states
- Perfect for package manager commands or code alternatives
Responsive Design
- Horizontal scrolling for long lines
- Vertical scrolling for tall code blocks
- Maximum height constraints for better layout