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.
<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.
Component API
CodeBlock
| Prop | Type | Default | Description |
|---|---|---|---|
children | Snippet | - | Child components to render |
class | string | - | Additional CSS classes |
...props | HTMLAttributes<HTMLDivElement> | - | All other div props are supported |
CodeBlockCode
| Prop | Type | Default | Description |
|---|---|---|---|
code | string | - | The code to display and highlight |
language | string | "tsx" | The language for syntax highlighting |
theme | string | "github-light" | The theme for syntax highlighting |
class | string | - | Additional CSS classes |
...props | HTMLAttributes<HTMLDivElement> | - | All other div props are supported |
CodeBlockGroup
| Prop | Type | Default | Description |
|---|---|---|---|
children | Snippet | - | Child components to render |
class | string | - | Additional CSS classes |
...props | HTMLAttributes<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-darkdraculanord- and more
For a complete list of supported themes, refer to the Shiki documentation.