Code
A static page with IzzyBlazor's script. No circuit.
Code: inline (a <code> in running text) or a Block (<pre><code>) that scrolls sideways. A block's copy button needs script, so it shows only where the browser runs script (from the first paint, so the block doesn't change when IzzyBlazor's script arrives; it copies once that has loaded); with JavaScript off the code is still there to select and copy by hand.
Usage
<Code>dotnet run</Code>
<Code Block Language="csharp" Title="Program.cs">builder.Services.AddIzzyBlazor();</Code>Inline and block
Use Code alone for names in running text, and Block for code on its own lines that scrolls sideways.
Preview
Install the package, then call AddIzzyBlazor() in Program.cs.
dotnet add package IzzyBlazor --prereleaseCode
<Stack Gap="4" Grow>
<TextBlock>Install the package, then call <Code>AddIzzyBlazor()</Code> in <Code>Program.cs</Code>.</TextBlock>
<Code Block Language="bash">dotnet add package IzzyBlazor --prerelease</Code>
</Stack>Syntax highlighting
Use Language to color the code on the server: C#, Razor, HTML, CSS, JavaScript, TypeScript, JSON and shell.
Preview
public sealed record Order(string Customer, decimal Total)
{
// Large orders need a second approval.
public bool IsLarge => Total > 500m;
}:root {
--ib-color-focus: #7c3aed;
--ib-radius-xl: 0.5rem;
}{ "Logging": { "LogLevel": { "Default": "Information" } }, "AllowedHosts": "*" }Code
<Stack Gap="4" Grow>
<Code Block Language="csharp">CSharp</Code>
<Code Block Language="css">Css</Code>
<Code Block Language="json">Json</Code>
</Stack>File name, line numbers and highlights
Use Title for a file name, LineNumbers to number the lines and HighlightLines to mark some, such as "2,9".
Preview
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddIzzyBlazor();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
var app = builder.Build();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.MapIzzyBlazor();
app.Run();Code
<Code Block Language="csharp" Title="Program.cs" LineNumbers HighlightLines="2,9" Grow>Startup</Code>Details
With a Language the highlighter knows (C#, Razor, HTML, CSS, JavaScript/TypeScript, JSON, shell), the code is colored on the server: spans with ib-tok-* classes, so it works with no script and never flashes plain first.
LineNumbers and HighlightLines put each line in its own span (a <mark> when highlighted), with its newline inside, so copying still gives the code as written; the numbers are CSS counters, never selected or copied. A Title turns the floating bar into a header row.
Mode support
| Feature | Off | Static | Live |
|---|---|---|---|
| Everything | Partly | Yes | Yes |
Off: JavaScript disabled. Static: a static SSR page with IzzyBlazor's script. Live: an interactive page (Server, WebAssembly or Auto). With JavaScript off, code blocks have no copy button.
Accessibility
- Inline code is a
code; a block is apreholding acode. - A block is a tab stop, so a long line or a block with
MaxHeightcan be scrolled from the keyboard. It's a group named by itsTitle, or "Code" without one. - The copy button is named "Copy code" (
CopyLabel); after copying, it shows a check for a moment and "Copied" (CopiedLabel) is announced politely. - With JavaScript off there's no copy button; the code is still there to select.
- Line numbers are CSS with empty alt text, so they're never copied and screen readers skip them where the browser supports it.
Parameters
| Prop | Type | Default |
|---|---|---|
ChildContentThe code. | RenderFragment? | |
BlockA block of code on its own lines (pre), scrolling sideways when long. | bool | false |
MaxHeightThe tallest a block gets before its code scrolls, such as Length.Rem(20) for a long file. | Length? | |
LanguageThe language, such as "razor", "csharp", "css" or "bash": shown above a block, and used to color the code when the highlighter knows it (see Languages). | string? | |
HighlightColors the code by Language. | bool | true |
TitleA file name or caption for a block, such as "Program.cs", shown in a header row above the code in place of Language (which still sets the colors). | string? | |
LineNumbersNumbers a block's lines. | bool | false |
HighlightLinesLines of a block to highlight, by number from 1: single lines and ranges, such as "3-5,9". | string? | |
CopyableAdds a copy button to a block (with JavaScript). | bool | true |
CopyLabelThe copy button's name. | string? | "Copy code" |
CopiedLabelAnnounced after copying. | string? | "Copied" |
Plus 16 item props and 4 common props.
Full APISee also
- CodeExample: a live example with its source under it.
- CopyButton: copies text, such as an install command.
- Kbd: a key or a key combination.
- Demos: Install guide.