Code
Import
Section titled “Import”import { Code } from '@delightstack/components';Basic Usage
Section titled “Basic Usage”function greet(name: string) {
console.log(`Hello, ${name}!`);
}
greet('World');
View code
<script> import { Code } from '@delightstack/components';
const snippet = `function greet(name: string) { console.log(\`Hello, \${name}!\`);}
greet('World');`;</script>
<Code code={snippet} language="typescript" />Examples
Section titled “Examples”With Filename
Section titled “With Filename”A header bar appears when filename or show_copy is set.
greeting.ts
function greet(name: string) {
console.log(`Hello, ${name}!`);
}
greet('World');
View code
<Code code={snippet} language="typescript" filename="greeting.ts"/>Line Highlighting
Section titled “Line Highlighting”Draw attention to specific lines by providing their line numbers.
cart.js
function calculateTotal(items) {
let total = 0;
for (const item of items) {
total += item.price * item.quantity;
}
return total;
}
View code
<script> import { Code } from '@delightstack/components';
const snippet = `function calculateTotal(items) { let total = 0; for (const item of items) { total += item.price * item.quantity; } return total;}`;</script>
<Code code={snippet} language="javascript" highlight_lines={[3, 4]} filename="cart.js" />Without Line Numbers
Section titled “Without Line Numbers”npm install @delightstack/components
View code
<Code code="npm install @delightstack/components" language="bash" show_line_numbers={false}/>Diff Display
Section titled “Diff Display”Lines starting with + get a green background, lines starting with - get a red background, and @@ lines are styled as section headers.
greet.js
@@ -1,5 +1,6 @@
function greet(name) {
- console.log("Hello");
+ console.log("Hello, " + name);
+ return name;
}
-greet();
+greet("World");
View code
<script> import { Code } from '@delightstack/components';
const diffOutput = `@@ -1,5 +1,6 @@function greet(name) {- console.log("Hello");+ console.log("Hello, " + name);+ return name;}
-greet();+greet("World");`;</script>
<Code code={diffOutput} diff filename="greet.js" />Max Height with Scroll
Section titled “Max Height with Scroll”Long code blocks can be constrained with a maximum height. The content scrolls vertically while the header stays visible.
math_utils.py
def fibonacci(n):
"""Calculate the nth Fibonacci number."""
if n <= 0:
return 0
elif n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
def factorial(n):
"""Calculate n factorial."""
if n < 0:
raise ValueError("n must be non-negative")
result = 1
for i in range(2, n + 1):
result *= i
return result
def is_prime(n):
"""Check if a number is prime."""
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
if __name__ == "__main__":
print(fibonacci(10))
print(factorial(5))
print(is_prime(17))
View code
<Code code={longCodeBlock} language="python" max_height="200px" filename="math_utils.py"/>Line Wrapping
Section titled “Line Wrapping”utils.ts
// This file contains some intentionally long lines to demonstrate line wrapping behavior
const apiEndpoint = "https://api.example.com/v2/organizations/acme-corp/projects/my-project/environments/production/deployments/latest";
export function createQueryString(params: Record<string, string>): string {
return Object.entries(params).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&");
}
View code
<Code code={longLineCode} language="typescript" wrap filename="utils.ts"/>Skeleton Loading
Section titled “Skeleton Loading”View code
<Code skeleton={loading} code={loading ? '' : snippet} language="javascript" />| Prop | Type | Default | Description |
|---|---|---|---|
code | string | required | Code string to display |
language | string | 'plaintext' | Language for syntax highlighting |
filename | string | - | Filename displayed in the header |
show_line_numbers | boolean | true | Show line numbers |
show_copy | boolean | true | Show copy-to-clipboard button |
start_line | number | 1 | Starting line number |
highlight_lines | number[] | [] | Line numbers to highlight |
diff | boolean | false | Render as unified diff |
wrap | boolean | false | Wrap long lines instead of horizontal scroll |
max_height | string | - | Max height with vertical scroll (e.g., '400px') |
skeleton | boolean | false | Show loading skeleton |
id | string | auto | Element ID |
class | string | '' | Additional CSS classes |
Supported Languages
Section titled “Supported Languages”The built-in tokenizer supports these languages out of the box:
- JavaScript / TypeScript (js, jsx, ts, tsx)
- HTML / XML
- CSS / SCSS / Sass / Less
- JSON / JSONC
- Python
- Bash / Shell / Zsh
- SQL
- Svelte
- Markdown
- Plaintext (default)
Accessibility
Section titled “Accessibility”- Uses semantic
<pre><code>markup - Copy button is keyboard accessible with
aria-label(“Copy code” / “Copied”) - Line numbers are decorative (
aria-hidden="true") and excluded from text selection viauser-select: none - Focus ring on the copy button for keyboard navigation
- Respects
prefers-reduced-motionfor skeleton animations