Editors

Code

A framed block of code: the code highlighted by Highlight.js, with an optional header that holds a title (a file name, a language) and a copy button, and a long block cut to a few lines behind an "Expand" button. Without highlighting it is a plain <pre>, for text that is not code in a language: a tree, a command's output.

tk:highlightjs is the bare <pre><code>; tk:code is the box around it.

Blade

The code, colored as PHP, in a light gray box with rounded corners and a thin border, darker in dark mode. Lines longer than the box scroll sideways.

Title and copy button #

A header over the code shows the file name, in small monospace text, and a clipboard button at its end. Clicking it copies the code, swaps the icon for a check and shows "Copied". A string in copyable is the button's text.

app/Models/Order.php
Blade

Code in the slot #

The slot is the code, with its HTML entities decoded, as in tk:highlightjs: write &lt; and &gt; for the tags you want to show.

Blade

A long block #

A block longer than 12 lines shows its first 12 and fades out over an "Expand" button; clicking it shows the whole code, hides the button and moves the focus to the code box.

Blade

Without highlighting #

A plain <pre>: no library is loaded, and the text keeps its spacing.

tk:badge
└── tk:badge.close
Blade

Your own buttons in the header #

The actions slot goes at the end of the header, before the copy button.

Terminal

Runs: 0

Blade

Customization #

Classes go to the box; header:, title:, copy:, body: and highlight: reach the parts.

package.json script
Blade

Reference

tk:code
└── tk:highlightjs

tk:code

Prop Type Default Description
code string null The code. When not given, the slot is used.
language string auto The language for Highlight.js (php, js, html, bash...). Without it, the language is detected.
title string | slot null The text at the start of the header, translated: a file name, a language.
copyable bool | string false Adds a copy button to the header. A string is its text.
maxLines int null Cuts a longer block to this many lines, with an "Expand" button that shows the rest.
highlight bool true false renders a plain <pre>, without loading Highlight.js.

The header shows when there is a title, a copy button or actions.

Slots

Slot Description
default The code, with HTML entities decoded, when there is no code.
actions Buttons at the end of the header, before the copy button.

Events

Event Detail When
copied The code was written to the clipboard.
failed The browser refused to write to the clipboard.

Prefixes

Prefix Goes to
header: The header (<figcaption>).
title: The title.
copy: The copy button, a tk:copyable (copy:tooltip, copy:class...).
body: The box around the code.
highlight: The tk:highlightjs inside (its loadable: prefixes too), or the <code> of a plain block.
expand: The fade with the "Expand" button over a cut block.

Events

Events come from the copy button and bubble, so @copied on tk:code hears them.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes and attributes on the tag go to the <figure> around everything; its border, background, rounding and margin give way to classes passed on the tag. To reach an inner element, prefix the attribute (listed in the Reference above).

blade

Accessibility

The block is a <figure>, and the header, when there is one, its <figcaption>, so the title names the code. The copy button is a tk:copyable: named "Copy" when it has no text, announced as "Copied" after a click. The "Expand" button controls the code box (aria-controls) and reports aria-expanded. Once the code is shown the button is hidden, so the focus moves to the code box (tabindex="-1"): it stays inside the block instead of falling back to the page.

Key Action
Enter, Space On the "Expand" button, shows the whole code and moves the focus to the code box.

Behavior notes

  • The theme's background is left out, so the code sits on the box's own background, light or dark with the page.
  • The code is set without ligatures, so => and != read as they are typed.
  • The copy button copies the text as given, before highlighting, through tallkit.copy(); it works on plain HTTP too.
  • maxLines counts the lines of the code on the server: a block at or under the limit renders whole, with no button. Lines do not wrap (a long one scrolls sideways), so the cut falls after exactly that many lines.
  • Expanding is one way: there is no button to cut the block again.