Skip to content

Code

import { Code } from '@delightstack/components';
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" />

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"
/>

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" />
npm install @delightstack/components
View code
<Code
code="npm install @delightstack/components"
language="bash"
show_line_numbers={false}
/>

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" />

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"
/>
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"
/>
View code
<Code skeleton={loading} code={loading ? '' : snippet} language="javascript" />
PropTypeDefaultDescription
codestringrequiredCode string to display
languagestring'plaintext'Language for syntax highlighting
filenamestring-Filename displayed in the header
show_line_numbersbooleantrueShow line numbers
show_copybooleantrueShow copy-to-clipboard button
start_linenumber1Starting line number
highlight_linesnumber[][]Line numbers to highlight
diffbooleanfalseRender as unified diff
wrapbooleanfalseWrap long lines instead of horizontal scroll
max_heightstring-Max height with vertical scroll (e.g., '400px')
skeletonbooleanfalseShow loading skeleton
idstringautoElement ID
classstring''Additional CSS classes

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)
  • 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 via user-select: none
  • Focus ring on the copy button for keyboard navigation
  • Respects prefers-reduced-motion for skeleton animations