Code Block
A multi-line code block with monospace rendering, optional filename header, copy-to-clipboard button, and language attribute for future syntax highlighting. For inline code inside running text, use <x-wirekit::.
Live Sandbox
This is a hydrated playground for the component. Toggle "Live preview" on the block below to swap the static HTML render for a real Livewire instance — every prop in the component's sandbox schema becomes an editable form field inside the iframe, so you can try different prop combinations live without writing any local code.
<x-wirekit::button>Save</x-wirekit::button>
Basic Usage
Wrap any code snippet in <x-wirekit::. The body is rendered verbatim inside a <pre><code> pair using the --font-wk-mono font token.
composer require pushery/wirekit
php artisan wirekit:install
The block takes its background from --color-wk-bg-muted and its text from --color-wk-text, and both follow the mode. The component colors no syntax; a highlighter of your own brings a palette for each mode. Side by side is the only way to read both at once, and the page's own mode does not decide what you see here.
With Filename
Pass filename="…" to render a subtle header bar above the code. Useful for showing file paths or command-line contexts.
Route::get('/', function () {
return view('welcome');
});
With Copy Button
Set :copy="true" to add a copy-to-clipboard button. With a filename the button stands in the header bar beside the name. The button uses navigator. and shows a checkmark for two seconds after a successful copy. The button is always accessible via keyboard and exposes aria-label="Copy to clipboard".
composer require pushery/wirekit
Without a filename the block draws no header bar for the button alone: the button stands in the top corner of the code, on the code's own background, so a long first line passes under the button rather than through it. A block that wraps its lines (wrap) keeps the button's width free at the end of every line, so its first line ends before the button.
php artisan vendor:publish --tag=wirekit-config --tag=wirekit-assets --force
Wrapping long lines
A long line scrolls inside the block by default, and the block takes keyboard focus so the hidden part can be reached. Set wrap to break the lines instead: nothing scrolls then, and the block is no tab stop of its own, which saves a keyboard reader one press per block on a page of examples.
While a line runs past the edge of the block, a shadow stands at that edge, and one at the start once the line has scrolled, as on a table that scrolls sideways. On a phone no scrollbar shows until the reader drags, and without the shadow a line cut off at the edge reads as the whole line. A block with wrap has nothing out of view and shows neither.
Where the browser knows text-indent: hanging each-line, the rest of a wrapped line stands two columns in, so it does not read as a line of its own: in a robots.txt, an .env file or a shell command the line is what counts. Safari and Firefox have it, Chrome from version 146; below that the rest of the line starts at the edge of the block. The indent counts from the edge of the block, not from the indentation of the line.
A wrapped line never ends between the hyphens of a token and the character after them, so an operator such as -> or an option such as --force stays whole: the line breaks at a space instead, or anywhere in a token longer than the line. What the copy button copies is the code as written.
On paper every block breaks its lines, wrap or not, and so does any other <pre> on the page: a sheet cannot scroll, and a line that ran past its edge would not print.
php artisan vendor:publish --provider="Pushery\WireKit\WireKitServiceProvider" --tag=wirekit-assets --force
php artisan vendor:publish --provider="Pushery\WireKit\WireKitServiceProvider" --tag=wirekit-assets --force
Wrapped code and a highlighter
The block holds those characters together with small elements around them. A highlighter that runs in the browser writes the block anew from its text, so the elements are gone, and highlight.js warns in the console about any block that holds elements before it highlights it. WireKit's plugin for highlight.js takes care of both: it takes the elements out before highlight.js reads the block and writes them into the colored one, also where - and > land in two different tokens. Register it, as step 4 of the example under Blade syntax highlighting shows. A page without a build step finds it as window. in any deferred script after @wirekitScripts, under 'bundle' => 'full', 'csp' and 'auto' alike.
With another highlighter, call wirekit on the <code> element before it runs and wirekit after it. apply() writes joins only into a block with wrap and into one that release() took them out of, so it is safe to call on every block the highlighter wrote.
Code placed into a rendered block
Code that you write into a rendered block yourself never passes through its slot, so the block cannot join it. A Markdown pipeline that renders the block once and places each example into it gives the example the same treatment with WireKit::. It takes markup, so escape the code first. Keep hyphens out of the placeholder, or the block joins it as code and your str_replace() no longer finds it:
use Illuminate\Support\Facades\Blade;
use Pushery\WireKit\WireKit;
// 1. Render the block once, with a placeholder as its slot.
$block = Blade::render('<x-wirekit::code-block wrap copy>__WIREKIT_CODE__</x-wirekit::code-block>');
// 2. Escape an example and join it as the block joins its own slot.
$example = 'php artisan vendor:publish --tag=wirekit-assets --force';
$html = str_replace('__WIREKIT_CODE__', WireKit::codeJoins(e($example)), $block);
With Language
Pass language="…" to emit a data-language="..." attribute on the <code> element. The component itself does not ship a syntax highlighter — the attribute is a hook for a highlighter of your own. Prism and highlight.js read the language from a language-… class, so copy the attribute into one before they run, as step 3 of the example below does.
use App\Models\User;
use Illuminate\Support\Facades\Gate;
public function boot(): void
{
Gate::define('manage-billing', fn (User $user): bool => $user->is_owner);
}
Blade syntax highlighting
highlight.js does not ship a built-in blade language module. Its built-in php-template language covers most Laravel-Blade idioms (@directive, {{ }}, {!! !!}, mixed HTML+PHP) — but it delegates HTML to hljs's xml mode, whose tag-name matcher is [A-Za-z0-9_.-]+ and explicitly does not accept :. Under a php-template alias, a <x-wirekit:: tag tokenizes as <x-wirekit: + bare : + orphan :button> — visibly broken on every WireKit component example.
The fix is a tiny custom grammar that extends hljs's XML-tag rule with :: support. Drop this in the same module where you call hljs.. WireKit ships no highlighter, so install the one you want first:
# 1. highlight.js is a developer-side choice — WireKit only emits the
# data-language="…" hook attribute the highlighter targets.
npm install highlight.js
// 1. Load highlight.js + the languages you use, and WireKit's plugin that keeps
// a block with `wrap` from breaking `->` or `--force` (the path is the one
// from resources/js/app.js).
import hljs from 'highlight.js/lib/core';
import xml from 'highlight.js/lib/languages/xml';
import php from 'highlight.js/lib/languages/php';
import { wirekitCodeJoins } from '../../vendor/pushery/wirekit/resources/js/utils/code-joins.js';
hljs.registerLanguage('xml', xml);
hljs.registerLanguage('php', php);
// 2. Register a custom `blade` grammar that handles `::` in tag names.
// Covers @directives, {{ }} echoes, {!! !!} raw, and HTML/component
// tags with the WireKit namespace separator.
hljs.registerLanguage('blade', function (hljs) {
const BLADE_COMMENT = { className: 'comment', begin: /\{\{--/, end: /--\}\}/ };
const BLADE_RAW = { className: 'template-variable', begin: /\{!!/, end: /!!\}/ };
const BLADE_ECHO = { className: 'template-variable', begin: /\{\{/, end: /\}\}/ };
const BLADE_DIR = { className: 'keyword', begin: /@[a-zA-Z]+/ };
const TAG = {
className: 'tag',
begin: /<\/?/, end: /\/?>/,
contains: [
// The `::` is the load-bearing change vs. stock xml/php-template —
// it lets `<x-wirekit::button>` tokenize as one tag name.
{ className: 'name', begin: /[\w.::-]+/, relevance: 0 },
{ className: 'attr', begin: /[\w-]+/ },
{ className: 'string', begin: /"/, end: /"/ },
{ className: 'string', begin: /'/, end: /'/ },
],
};
return {
name: 'Blade',
case_insensitive: true,
contains: [BLADE_COMMENT, BLADE_RAW, BLADE_ECHO, BLADE_DIR, TAG],
};
});
// 3. highlight.js reads the language from a `language-…` class; WireKit writes `data-language`.
document.querySelectorAll('code[data-language]').forEach((code) => {
code.classList.add(`language-${code.dataset.language}`);
});
// 4. Keep the joins of a block with `wrap`: out before highlight.js reads the
// block, back into the colored one after it.
hljs.addPlugin(wirekitCodeJoins);
// 5. Highlight all code blocks on the page.
hljs.highlightAll();
Without that grammar a block whose language is blade falls through to highlight.js's no-highlight fallback (you'll see a console warning Could not find the language 'blade') — the block still renders as plain monospace text, it just doesn't get colored tokens.
Do not alias blade to php-template:
// Don't do this — looks tempting, breaks WireKit components.
hljs.registerAliases('blade', { languageName: 'php-template' });
php-template covers Blade directives and interpolations but its underlying XML mode treats : as a tag-name boundary. Every x-wirekit::* example then renders with broken tokens — the component prefix splits off and the rest of the line reads as orphan text. The 30-line custom grammar above is the smallest fix that keeps WireKit's :: namespace separator intact.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
language |
string|null |
null |
Emitted as data-language on the <code> tag — hook for developer syntax highlighters. |
label |
string|null |
null |
Names the scrollable code region, and the switch that makes it a screen-reader landmark. Without it the block stays keyboard-reachable and simply is not a landmark — see Accessibility. |
filename |
string|null |
null |
Text shown in the header bar. When set, the header bar is rendered even without copy. |
copy |
bool |
false |
Show a copy-to-clipboard button in the header bar. |
wrap |
bool |
false |
Break long lines instead of scrolling them. The block is then no tab stop of its own — see Wrapping long lines. |
scope |
string|null |
null |
Named personalization scope for block-level class overrides. |
Events
A copy dispatches wirekit:copied on the block once the write has succeeded or failed. The event bubbles, and event. says which, so a page can count copies without reading anything inside the block. Every copy dispatches it, a second one with the same result as well.
document.addEventListener('wirekit:copied', (event) => {
// event.target is the block that was copied, or the clipboard button that copied.
countCopy(event.target, event.detail.succeeded);
});
Accessibility
- An
aria-labeloraria-labelledbyof yours names the code and makes it aregion, aslabeldoes - The copy button is a real
<button>element and reachable viaTab - The icon-only button exposes
aria-label="Copy to clipboard"and updates to"Copied to clipboard"after a successful copy - An
aria-live="polite"status region announces"Code copied to clipboard"to screen readers on success (WCAG 2.2 SC 4.1.3) - The success state swap is a visual-only change — the button is never removed from the tab order
- The underlying
<pre>block is horizontally scrollable on overflow, with a visible focus outline when reached by keyboard - The
<code>carriestabindex="0"unless the block haswrap, so a keyboard user can reach text that scrolled out of view (WCAG 2.1.1). A block that wraps has nothing out of view and is no tab stop
The landmark is opt-in — name the block to get one
A named region is a landmark, and landmarks have to be distinguishable. A name derived
from the language would read well on a page with one or two blocks and badly on a page with
twenty of the same language: the name meant to tell them apart would make them identical, and
an accessibility audit reports that as landmark-unique.
So an unnamed block is not a landmark at all. It keeps tabindex="0" and its focus ring, which
is what keyboard reachability actually requires; twenty landmarks with one name would not be a
way to find anything either.
Pass label when the block is worth navigating to, and name it after what it contains rather
than after what it is:
{{-- 1. "Change 4471" locates the block; "php code" only repeats what the page already is. --}}
{{-- 2. One name per instance — the rotor lists them all, in order. --}}
@foreach ($changes as $change)
<x-wirekit::code-block language="php" :label="__('Change :id', ['id' => $change->id])">
{{ $change->diff }}
</x-wirekit::code-block>
@endforeach
An empty or whitespace-only label counts as no name — a role="region" with an empty
accessible name is not exposed as a landmark anyway, so the block omits the role rather than
shipping one nobody can identify. An interpolated value over a record with no title lands here.
Keyboard Interaction
The copy button is a real control and behaves like one. The code itself is a tab stop unless the block has wrap, so a line that runs past the edge can be scrolled into view.
| Key | Action |
|---|---|
Tab |
Move focus to the copy button, when copy is set, then to the code |
Enter / Space |
Copy the code to the clipboard, on the copy button |
The code itself is a focusable scroll region, so once it has focus the browser's own arrow keys scroll a long line sideways; the component binds no key of its own for that.
Design Tokens
| Token | Used for |
|---|---|
--font-wk-mono |
Monospace font family |
--color-wk-bg-muted |
Code block background |
--color-wk-border |
Wrapper + header-bar border |
--color-wk-text |
Code color |
--color-wk-text-muted |
Filename and copy-button color |
--color-wk-success-text |
Copy-success checkmark color |
--radius-wk-md |
Wrapper border radius |
--text-wk-sm |
Code font size |
--text-wk-xs |
Filename font size |
--space-wk-md |
Code padding |
--space-wk-xs |
Header-bar vertical padding |
See Also
- Code — inline monospace for running text
- Customization —
WireKit::for block-level class overridesscope()