Docs
Code Snippet
Code Snippet

A lightweight syntax-highlighted code display component with copy functionality and optional file titles.

19 views
0 downloads
Loading...

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"
2
3import { CodeSnippet } from "@/registry/media/code-snippet"
4
5export function Component() {
6 const code = `function hello() {
7 console.log("Hello, world!");
8}`
9
10 return (
11 <CodeSnippet
12 title="hello.js"
13 code={code}
14 language="javascript"
15 showLineNumbers={true}
16 />
17 )
18}

Custom Theme Example

1import { PrismTheme } from "prism-react-renderer"
2
3import { CodeSnippet } from "@/registry/media/code-snippet"
4
5const 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 styles
20 ],
21}
22
23export function CustomThemeExample() {
24 return (
25 <CodeSnippet
26 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"
2
3import { useState } from "react"
4
5import { CodeSnippet } from "@/registry/media/code-snippet"
6
7export function TabbedExample() {
8 const [activeTab, setActiveTab] = useState("npm")
9
10 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 }
28
29 return (
30 <CodeSnippet
31 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"
2
3import { CodeSnippet } from "@/registry/media/code-snippet"
4
5const 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 styles
11 ],
12}
13
14const 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 styles
20 ],
21}
22
23export function AdaptiveThemeExample() {
24 return (
25 <CodeSnippet
26 code="const greeting = 'Hello, world!';"
27 language="javascript"
28 adaptiveTheme={{ light: lightTheme, dark: darkTheme }}
29 showLineNumbers={true}
30 />
31 )
32}

API Reference

CodeSnippet

NameTypeDefaultDescription
titlestring-Optional title displayed in the header
codestring-The code content to display
languagestring"typescript"Programming language for syntax highlighting
classNamestring-Additional CSS classes
borderbooleantrueWhether to show the border around the component
themePrismTheme-Custom theme object for syntax highlighting
showLineNumbersbooleantrueWhether 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
activeTabstring-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 content
    • color: Default text color
    • backgroundColor: Background color for the code area and header
  • styles: Array of style rules for different token types
    • types: 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 comments
  • keyword - Language keywords (if, function, const, etc.)
  • string - String literals
  • number - Numeric values
  • function - Function names
  • property - Object properties
  • punctuation - Brackets, semicolons, etc.
  • tag - HTML/JSX tags
  • attr-name - HTML/JSX attribute names
  • attr-value - HTML/JSX attribute values

Examples

Basic Code Snippet

Simple code display with syntax highlighting and copy functionality.

Loading...

Theme Customization

Switch between dark and light themes for different visual preferences.

Loading...

Display Options

Toggle various display options including title, border, and line numbers.

Loading...

C++20 with Custom Theme

Modern C++20 features showcased with a custom purple-blue theme optimized for C++ syntax.

Loading...

Adaptive Dark/Light Mode

Automatically adapts to your system's dark/light mode preference using the adaptiveTheme prop.

Loading...

Tabbed Code Snippets

Display multiple related code examples with tab navigation, perfect for showing installation commands across different package managers.

Loading...

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