# Code Block

> A block of monospace text that keeps its whitespace, with a copy button; for code, commands, configuration and stack traces.

Source: https://shadleaf.dev/1.2.0/components/code-block/

Multiline text in a monospace font, with its whitespace and line breaks kept, and a button that copies it.

**Example: Code block.** Point at the block, or Tab to its copy button, to copy the text.

```html
<sl:code-block>java.lang.IllegalStateException: Order 1042 has no lines
    at com.example.orders.OrderService.submit(OrderService.java:42)
    at com.example.orders.OrderController.submit(OrderController.java:27)</sl:code-block>
```

## When to use

- A command or a configuration snippet for people to copy.
- A stack trace or a log on an error page or in a details view.
- A piece of code, an API key or a token to paste somewhere else.

Use something else for:

- A key or a shortcut: a [kbd](https://shadleaf.dev/1.2.0/components/kbd.md).
- A short value in a list of details, such as an id: an [item](https://shadleaf.dev/1.2.0/components/item.md).

The text goes between the tags, as written: start it right after the opening tag, or the block starts with an empty
line. Text from the model goes in with `[[${...}]]`, which escapes it, so markup in it shows as written.
`th:text` on the component would replace its copy button too.

**Example: Text from the model.** [[${...}]] escapes the text, so markup in it shows as written.

```html
<sl:code-block th:with="snippet='<sl:button>Save</sl:button>'">[[${snippet}]]</sl:code-block>
```

## The copy button

The copy button sits in the block's corner. With a mouse it shows while the pointer is over the block; with the
keyboard, when Tab reaches it; on a touch screen it always shows. A click copies the text and shows a check mark for
a moment, and screen readers hear "Copied".

The button needs Alpine and the browser's Clipboard API, which browsers only offer on HTTPS and on `localhost`.
Without either, the button is not shown, and people select the text themselves. Leave it out with `copy="false"`.

**Example: Without the copy button.**

```html
<sl:code-block copy="false">mvn spring-boot:run</sl:code-block>
```

The button's name and the "Copied" announcement are the messages `sl.code-block.copy` and `sl.code-block.copied`;
see [Translating](https://shadleaf.dev/1.2.0/guides/translating.md).

## Long lines

Long lines scroll horizontally, so the block never widens the page. The block takes the keyboard focus, so it can be
scrolled with the arrow keys too. Add `wrap` to break long lines instead.

**Example: Wrapping long lines.** wrap breaks long lines instead of scrolling them.

```html
<sl:code-block wrap>java.lang.IllegalArgumentException: The delivery date 2026-12-24 is after the last date the warehouse in Ghent accepts orders for this year
    at com.example.orders.delivery.DeliveryDateValidator.validate(DeliveryDateValidator.java:58)</sl:code-block>
```

To limit the height of a long block, give the text a `max-height` in your own CSS; it scrolls vertically past it:

```css
.code-block-content {
  max-height: 20rem;
}
```

## Left out

No syntax highlighting and no line numbers. For highlighted code, run a highlighter (such as highlight.js) over
`.code-block-content > code` in your own script.

## Attributes

Attributes of `<sl:code-block>`: Multiline text in a monospace font, such as a piece of code, a command or a stack trace, with its whitespace and line breaks kept. A copy button in its corner puts the text on the clipboard; it shows while the pointer is over the block or the keyboard focuses it, and always on a touch screen. The button needs Alpine and the Clipboard API, which browsers only offer on HTTPS and `localhost`; without them it is not shown. Write dynamic text as `[[${...}]]`, which escapes it. Every attribute that is not a prop passes through to the block.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `wrap` | *boolean* | — | Wrap long lines instead of scrolling them horizontally. |
| `copy` | *boolean* | `true` | Show the copy button. `false` leaves it out. |

Every prop can also be set from an expression: `th:wrap="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended.

## Styling hooks

The classes and attributes your own CSS can select to restyle the component, as explained in
[your own CSS rules](https://shadleaf.dev/1.2.0/guides/adjustability.md#5-your-own-css-rules).

Styling hooks of `<sl:code-block>`.

**Parts**

| Class | Element |
| --- | --- |
| `.code-block` | `<div>` |
| `.code-block-content` | `<pre>` |
| `.code-block-copied-icon` | `<svg>` |
| `.code-block-copy` | `<button>` |
| `.code-block-copy-icon` | `<svg>` |

**Props**

| Prop | Selector | Values |
| --- | --- | --- |
| `wrap` | `.code-block[data-wrap="…"]`, `.code-block:not([data-wrap])` | `true`; `false` (default) |

**States**

| Selector | Values |
| --- | --- |
| `.code-block-copy[data-copied]` | *present or absent* |
