Code Block

A component for displaying code snippets with syntax highlighting and customizable styling using Shiki.

💡 Tip: The first three examples use the light theme by default. Switch to light mode to view them properly. The "Different Themes" example adapts to your current color mode.

Examples

Basic Code Block

function greet(name: string) {
  return `Hello, ${name}!`;
}

// Call the function
const greeting = greet("World");
console.log(greeting);

Code Block with Header

You can use CodeBlockGroup to add a header with metadata and actions to your code blocks.

Svelte
counter.svelte
<script lang="ts">
  let count = $state(0);

  function increment() {
    count++;
  }
</script>

<div>
  <p>You clicked {count} times</p>
  <button onclick={increment}>
    Click me
  </button>
</div>

Different Languages

You can highlight code in various languages by changing the language prop.

Python Example

def fibonacci(n):
    """Generate first n Fibonacci numbers."""
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

# Generate the first 10 Fibonacci numbers
print("First 10 Fibonacci numbers:")
for number in fibonacci(10):
    print(number)

CSS Example

.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.button:hover {
  background-color: #45a049;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  transform: translateY(-2px);
}

.button:active {
  transform: translateY(0);
}

Different Themes

Shiki supports many popular themes. This example automatically switches between github-light and github-dark based on your current color mode. Try toggling your theme to see the change!

<script lang="ts">
  import { writable } from 'svelte/store';

  const todos = writable([
    { id: 1, text: 'Learn Svelte', done: false },
    { id: 2, text: 'Build an app', done: false }
  ]);

  function addTodo(text: string) {
    todos.update(items => [
      ...items,
      { id: Date.now(), text, done: false }
    ]);
  }

  function toggleTodo(id: number) {
    todos.update(items =>
      items.map(item =>
        item.id === id ? { ...item, done: !item.done } : item
      )
    );
  }
</script>

<div class="todo-list">
  {#each $todos as todo}
    <div class="todo-item">
      <input
        type="checkbox"
        checked={todo.done}
        onchange={() => toggleTodo(todo.id)}
      />
      <span class:done={todo.done}>{todo.text}</span>
    </div>
  {/each}
</div>

<style>
  .done {
    text-decoration: line-through;
    opacity: 0.6;
  }
</style>

Installation

Copy and paste the following code into your project.

pnpm dlx shadcn-svelte@latest add https://ai-elements.vercel.app/r/prompt-kit-code-block.json

Component API

CodeBlock

PropTypeDefaultDescription
childrenSnippet-Child components to render
classstring-Additional CSS classes
...propsHTMLAttributes<HTMLDivElement>-All other div props are supported

CodeBlockCode

PropTypeDefaultDescription
codestring-The code to display and highlight
languagestring"tsx"The language for syntax highlighting
themestring"github-light"The theme for syntax highlighting
classstring-Additional CSS classes
...propsHTMLAttributes<HTMLDivElement>-All other div props are supported

CodeBlockGroup

PropTypeDefaultDescription
childrenSnippet-Child components to render
classstring-Additional CSS classes
...propsHTMLAttributes<HTMLDivElement>-All other div props are supported

Usage with Markdown

The CodeBlock component is used internally by the Markdown component to render code blocks in markdown content. When used within the Markdown component, code blocks are automatically wrapped with the not-prose class to prevent conflicts with prose styling.

Tailwind Typography and not-prose

The CodeBlock component includes the not-prose class by default to prevent Tailwind Typography's prose styling from affecting code blocks. This is important when using the @tailwindcss/typography plugin, which provides beautiful typography defaults but can interfere with code block styling.

Since code blocks are styled with Shiki for syntax highlighting, they should not inherit Tailwind Typography styles. The not-prose class ensures that code blocks maintain their intended appearance regardless of the surrounding typography context.

Customizing Syntax Highlighting

The CodeBlockCode component uses Shiki for syntax highlighting. You can customize the theme by passing a different theme name to the theme prop.

Shiki supports many popular themes including:

  • github-light (default)
  • github-dark
  • dracula
  • nord
  • and more

For a complete list of supported themes, refer to the Shiki documentation.