ComponentsCodeBlock

Code Block

A code block component for displaying code snippets


python

1def hello_world():
2    print("Hello, World!")


The CodeBlock component is a customizable code block that can be used to display code snippets. It supports syntax highlighting, line numbers, and copy-to-clipboard functionality.

Tip!

Use the copy, showLanguage, and showLineNumbers props to enable additional features for the code block.




Props

The CodeBlock component accepts the following props:

PropTypeDefaultDescription
childrenReactNode-The code to be displayed inside the code block
languagestring-The language of the code snippet for syntax highlighting
showLineNumbersbooleanfalseWhether to display line numbers for the code snippet
startingLineNumbernumber1The starting line number for the code snippet
copybooleanfalseWhether to display a copy-to-clipboard button
wrapLinesbooleanfalseWhether to wrap long lines of code
showLanguagebooleanfalseWhether to display the language name at the top of the code block

Info

The CodeBlock component uses the react-syntax-highlighter library to provide syntax highlighting for code snippets. You can customize the appearance and behavior of the code block by passing the appropriate props.


Usage Examples

Basic Code Block

print("Hello, World!")

Code Block with Line Numbers

5function add(a, b) {
6    return a + b;
7}

Code Block with Copy Button

interface User {
    name: string;
    age: number;
}

Code Block with Language Name

java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}