CodeBlock Component

Use <CodeBlock> to organize related code examples in one place while showing one panel at a time, like requests and responses, or implementations in different languages.

Syntax

<CodeBlock slots="heading, code" repeat="4" languages="cURL, JavaScript, JSON, XML" />

#### Request

```bash
# cURL request
```

#### Request

```javascript
// JavaScript request
```

#### Response

```json
{}
```

#### Response

```markup
<response />
```

Example

data-slots=heading, code
data-repeat=4
data-languages=cURL, JavaScript, JSON, XML

Request

curl -X POST 'https://api.example.com/items' \
  -H 'Content-Type: application/json' \
  -d '{"name":"example"}'

Request

const response = await fetch('https://api.example.com/items', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'example' }),
});
const result = await response.json();

Response

{
  "id": 42,
  "name": "example",
  "status": "created"
}

Response

<item>
  <id>42</id>
  <name>example</name>
  <status>created</status>
</item>

The Request tab groups the cURL and JavaScript panels; the Response tab groups the JSON and XML panels. Choose a tab first, then use the picker to switch between that tab's code choices.

Parameters

Without languages, the component still presents heading tabs, but hides the picker. Selecting a tab displays its panel; it does not show all panels simultaneously.

Supported Languages

The following language identifiers are supported:

bash, c, cpp, csharp, css, git, go, graphql, http, java, javadoc, javascript, jsdoc, json, json5, jsx, kotlin, less, markdown, markup, objectivec, php, python, regex, rest, sass, sql, swift, typescript, yaml

Common aliases are also accepted (for example, html for markup, js for javascript). See the Prism supported languages list for each language's aliases.

Use these with the code block syntax, not the languages param.

Variants

When to Use

Use the CodeBlock component when you need to:

For single code snippets, use regular markdown code blocks instead.