Skip to content

Utilities

CodeBlock

Bit.BlazorUICodeSnippetSnippet

BitCodeBlock shows a block of code - a command to run, a snippet to paste - in the theme's monospaced family, with a button that puts it on the clipboard, an optional header with a title and a language, a height past which it scrolls, and a terminal look for the commands of a shell.

Notes

The code is written as it is: there is no syntax highlighting, and the Language is a label only. A copy confirms itself - the glyph of the button turns into a check mark, its name says "Copied", and a polite live region announces it to screen readers. The words are the Copy and Copied of BitStrings unless CopyText and CopiedText say otherwise. For a fragment of code inside a sentence, use the Code parameter of BitText.

Usage

Every example is live. Open its code to see exactly what produced the component running underneath.

Basic

Code is the code to show, and exactly what the copy button puts on the clipboard. The code can also be written right inside the tag, as its child content.

dotnet new install Bit.Boilerplate

dotnet tool install --global Bit.Cli

Header

Title (a file name, most of the time) and Language are shown in a header above the code, and the copy button moves into it. The title also names the code to a screen reader.

Program.csC#
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddBitBlazorUIServices();

var app = builder.Build();
app.Run();

json
{ "name": "bit BlazorUI", "free": true }

Terminal

Terminal draws the block as a terminal window: dark in both color schemes, with the three dots of a title bar and a prompt before every command line. The prompt is drawn by the stylesheet, so it is neither selected nor copied with the commands; --bit-CodeBlock-prompt changes it.

dotnet tool install --global Bit.Cli
bit new MyFirstProject

PowerShell
Set-Location .\MyFirstProject
dotnet run

MaxHeight

MaxHeight (any CSS length) is the height past which the code scrolls. The scrolling box takes the focus, so a keyboard can scroll it too.

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqliteConnectionString": "Data Source=App_Data/AppDb.db;Mode=ReadWriteCreate;Cache=Shared"
  },
  "Identity": {
    "Issuer": "MyFirstProject",
    "Audience": "MyFirstProject",
    "BearerTokenExpiration": "0.00:05:00",
    "RefreshTokenExpiration": "14.00:00:00"
  }
}

Copy

OnCopy is raised once the code is on the clipboard. CopyOnClick copies the code when it is clicked as well - a click that ends a selection of a part of it copies nothing - and ShowCopyButton hides the button. CopyText and CopiedText are the name of the button before and after a copy.

dotnet new install Bit.Boilerplate
Copied 0 time(s).

dotnet new install Bit.Boilerplate

Custom content

The child content takes the place of the code that is shown, while Code stays what is copied: an app that highlights its code with a library of its own hands the highlighted markup in as the content and the plain source as the Code.

C#
var greeting = "Hello, world!";

Style & Class

Classes and Styles reach the root, the header and its parts, the copy button, the scrolling body, the pre and the code elements, and the --bit-CodeBlock-* variables restyle the defaults.

dotnet new install Bit.Boilerplate

Install
dotnet new install Bit.Boilerplate

dotnet new install Bit.Boilerplate

RTL

Use BitCodeBlock in right-to-left (RTL). The header follows the page, while the code itself is always laid out left to right.

نصب
dotnet new install Bit.Boilerplate

CSS variables

The public custom properties this component reads off its root, for what no parameter covers.

Every variable is read with a fallback and never declared by the component, so it inherits like any other custom property: set one on :root (or in a [bit-theme] block) to restyle every instance, on any ancestor to restyle the ones inside it, or on the Style of one instance to restyle it - and any instance nested inside it, which inherits the value too; a parameter changes one alone, as does the real CSS property on the instance's Style / Styles when that property does not inherit (opacity: 1 keeps one disabled instance undimmed without undimming a disabled one inside it), while an inherited one such as color or font-family also reaches the nested instances that do not set it themselves. Leave a variable unset and the component falls back to the theme token beside it.

BitCodeBlock CSS variables

Name Default value Description
--bit-CodeBlock-background 4% of --bit-clr-fg-pri Background of the block. The Terminal parameter wins over it.
--bit-CodeBlock-color inherit Text color of the code. The Terminal parameter wins over it.
--bit-CodeBlock-border-color --bit-clr-brd-sec Border of the block and under its header. The Terminal parameter wins over it.
--bit-CodeBlock-radius --bit-shp-radius-surface Corner radius of the block.
--bit-CodeBlock-padding spacing(1.5) Padding around the code.
--bit-CodeBlock-font-family --bit-tpg-font-family-mono Font of the code and of the language label.
--bit-CodeBlock-font-size --bit-tpg-fs-sm Text size of the code.
--bit-CodeBlock-max-height none Height past which the code scrolls. The MaxHeight parameter wins over it.
--bit-CodeBlock-header-background 4% of --bit-clr-fg-pri Background of the header bar.
--bit-CodeBlock-header-color --bit-clr-fg-sec Text color of the header bar.
--bit-CodeBlock-copy-button-color inherit Color of the glyph of the copy button.
--bit-CodeBlock-copy-button-hover-background 12% of the button's color Background of the copy button under the pointer (20% while it is pressed).
--bit-CodeBlock-terminal-background --bit-clr-ntr-gray190 Background of a Terminal block, dark in both color schemes.
--bit-CodeBlock-terminal-color --bit-clr-ntr-gray20 Text color of a Terminal block.
--bit-CodeBlock-terminal-border-color 16% of --bit-clr-ntr-gray20 Border of a Terminal block.
--bit-CodeBlock-prompt "$ " The prompt drawn before each line of a Terminal block, as a CSS string. It is neither selected nor copied with the commands.
--bit-CodeBlock-prompt-color --bit-clr-ntr-gray90 Color of the prompt of a Terminal block.

API

Every parameter, public member, sub-class and enum this component exposes.

BitCodeBlock parameters

Name Type Default value Description
ChildContent RenderFragment? null The code as markup, rendered in place of Code: code written right inside the tag, or code an app highlighted itself. The copy puts Code on the clipboard where it is set, and otherwise the text the browser renders of this content.
Classes BitCodeBlockClassStyles? null Custom CSS classes for different parts of the code block.
Code string? null The code to show, which is also exactly what the copy puts on the clipboard.
CopiedText string? null The name of the copy button while what it copied is on the clipboard, which is also what screen readers are told after a copy. Defaults to BitStrings.Copied ("Copied").
CopyOnClick bool false Copies the code when the code itself is clicked, as well as through the copy button. A click that ends a selection of a part of the code copies nothing, and neither does a double or a triple click selecting a word or a line. It is a shortcut for the pointer only, so the copy button is best kept shown beside it.
CopyText string? null The name of the copy button, which is what a screen reader reads out for it and what the pointer shows as its tooltip. Defaults to BitStrings.Copy ("Copy").
Language string? null The language of the code, shown as a label in the header. It is a label only: the code is not highlighted.
MaxHeight string? null The height (any CSS length) past which the code scrolls.
OnCopy EventCallback The callback raised after the code has been put on the clipboard.
OnCopyFailed EventCallback The callback raised when the browser refuses to put the code on the clipboard - a copy that comes too late after the press of the user, as one made after a round trip to the server can in Safari.
ShowCopyButton bool true Shows the button that copies the code.
Styles BitCodeBlockClassStyles? null Custom CSS styles for different parts of the code block.
Terminal bool false Draws the code block as a terminal window: a dark surface in both color schemes, the three dots of a title bar, and a prompt before every line of Code. Content handed in through ChildContent is drawn as it is written, with no prompt.
Title string? null The title shown in the header of the code block - a file name, most of the time - which also names the code to a screen reader.

BitComponentBase parameters

Name Type Default value Description
AriaLabel string? null Gets or sets the accessible label for the component, used by assistive technologies.
Class string? null Gets or sets the CSS class name(s) to apply to the rendered element.
Dir BitDir? null Gets or sets the text directionality for the component's content.
Disabled bool false Gets or sets a value indicating whether the component is disabled and cannot respond to user interaction.
ForceAnimation bool false Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested.
HtmlAttributes Dictionary<string, object> new Dictionary<string, object>() Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. An attribute written on the component wins over an entry of the same name.
Id string? null Gets or sets the unique identifier for the component's root element.
Style string? null Gets or sets the CSS style string to apply to the rendered element.
TabIndex string? null Gets or sets the tab order index for the component when navigating with the keyboard.
Visibility BitVisibility BitVisibility.Visible Gets or sets the visibility state (visible, hidden, or collapsed) of the component.

BitComponentBase public members

Name Type Default value Description
UniqueId Guid Guid.NewGuid() Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed.
RootElement ElementReference Gets the reference to the root HTML element associated with this component.

BitCodeBlockClassStyles properties

Name Type Default value Description
Root string? null Custom CSS classes/styles for the root element of the BitCodeBlock.
Header string? null Custom CSS classes/styles for the header bar of the BitCodeBlock, which holds the title, the language and the copy button (rendered when a Title or a Language is set, or the block is a Terminal).
Dots string? null Custom CSS classes/styles for the three window dots of the header of a Terminal BitCodeBlock.
Title string? null Custom CSS classes/styles for the title in the header of the BitCodeBlock.
Language string? null Custom CSS classes/styles for the language label in the header of the BitCodeBlock.
CopyButton string? null Custom CSS classes/styles for the copy button of the BitCodeBlock.
Body string? null Custom CSS classes/styles for the scroll container of the code of the BitCodeBlock.
Pre string? null Custom CSS classes/styles for the pre element of the BitCodeBlock.
Code string? null Custom CSS classes/styles for the code element of the BitCodeBlock.
LiveRegion string? null Custom CSS classes/styles for the visually hidden live region of the BitCodeBlock that announces a copy.

BitVisibility enum

Name Value Description
Visible 0 The content of the component is visible.
Hidden 1 The content of the component is hidden, but the space it takes on the page remains (visibility:hidden).
Collapsed 2 The component is hidden (display:none).

BitDir enum

Name Value Description
Ltr 0 Ltr (left to right) is to be used for languages that are written from the left to the right (like English).
Rtl 1 Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic).
Auto 2 Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.