# CodeBlock
**Also known as:** CodeSnippet, Snippet
## Description
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.
## 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. |
| 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. |
## 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. |
## Enums
### 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. |
## Sub Classes
### 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. |
## CSS Variables
Public CSS custom properties read off the component's root. Set one on `:root` to restyle every instance, on any ancestor to restyle the ones inside it, or on the `Style` of one instance to restyle it and every instance nested inside it, which inherits the value too - a parameter changes one alone, as does the real CSS property on its `Style` / `Styles` when that property does not inherit (`opacity`); an inherited one (`color`, `font-family`) reaches the nested instances that do not set it themselves.
| 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. |
## Examples
\n**Basic**:
```razor
dotnet tool install --global Bit.Cli
```
```csharp
private readonly string basicCode = "dotnet new install Bit.Boilerplate";
```
\n**Header**:
```razor
```
```csharp
private readonly string headerCode = """
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddBitBlazorUIServices();
var app = builder.Build();
app.Run();
""";
private readonly string jsonCode = """{ "name": "bit BlazorUI", "free": true }""";
```
\n**Terminal**:
```razor
```
```csharp
private readonly string terminalCode = """
dotnet tool install --global Bit.Cli
bit new MyFirstProject
""";
private readonly string powershellCode = """
Set-Location .\MyFirstProject
dotnet run
""";
```
\n**MaxHeight**:
```razor
```
```csharp
private readonly string longCode = """
{
"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"
}
}
""";
```
\n**Copy**:
```razor
```
```csharp
private int copyCount;
private readonly string basicCode = "dotnet new install Bit.Boilerplate";
```
\n**Custom content**:
```razor
var greeting = "Hello, world!";
```
```csharp
private readonly string highlightedCode = "var greeting = \"Hello, world!\";";
```
\n**Style & Class**:
```razor
```
```csharp
private readonly string basicCode = "dotnet new install Bit.Boilerplate";
```
\n**RTL**:
```razor
```
```csharp
private readonly string basicCode = "dotnet new install Bit.Boilerplate";
```
Copied @copyCount time(s).