Class CodeBlock
A reusable code-block VisualElement: a header row with an optional language label and
a copy-to-clipboard button (revealed on hover) above a monospaced body. When App UI's
TextMate integration is enabled (APPUI_ENABLE_SYNTAX_HIGHLIGHTING), the active
grammar and theme are resolved from two USS custom properties on this element:
--codeblock-grammar— GUID string of a TextMateGrammarAsset.--codeblock-theme— GUID string of a TextMateThemeAsset.
appui-code-block--lang-<value> class so per-language grammar mappings can be
expressed declaratively in USS (see PackageResources/Styles/Components/CodeBlock.uss).
Asset resolution from GUID requires the editor; in built players the body falls back to
plain monospaced text.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class CodeBlock : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder
Constructors
CodeBlock()
Default constructor.
Declaration
public CodeBlock()
Fields
bodyUssClassName
The USS class applied to the source-code body element.
Declaration
public const string bodyUssClassName = "appui-code-block__body"
Field Value
| Type | Description |
|---|---|
| string |
copyButtonUssClassName
The USS class applied to the copy / checkmark button.
Declaration
public const string copyButtonUssClassName = "appui-code-block__copy-btn"
Field Value
| Type | Description |
|---|---|
| string |
headerSpacerUssClassName
The USS class applied to the flexible spacer between the language label and the copy button.
Declaration
public const string headerSpacerUssClassName = "appui-code-block__header-spacer"
Field Value
| Type | Description |
|---|---|
| string |
headerUssClassName
The USS class applied to the header row.
Declaration
public const string headerUssClassName = "appui-code-block__header"
Field Value
| Type | Description |
|---|---|
| string |
hoveredUssClassName
The USS class toggled while the pointer is anywhere over this element. Used to reveal the copy button.
Declaration
public const string hoveredUssClassName = "appui-code-block--hovered"
Field Value
| Type | Description |
|---|---|
| string |
languageUssClassName
The USS class applied to the language label inside the header.
Declaration
public const string languageUssClassName = "appui-code-block__language"
Field Value
| Type | Description |
|---|---|
| string |
languageVariantUssClassNamePrefix
Prefix of the per-language modifier class added when language is set.
Declaration
public const string languageVariantUssClassNamePrefix = "appui-code-block--lang-"
Field Value
| Type | Description |
|---|---|
| string |
lineNumbersUssClassName
The USS class applied to the optional left-side line-numbers gutter.
Declaration
public const string lineNumbersUssClassName = "appui-code-block__line-numbers"
Field Value
| Type | Description |
|---|---|
| string |
scrollUssClassName
The USS class applied to the scrollable area that wraps the line-numbers gutter and the body.
Declaration
public const string scrollUssClassName = "appui-code-block__scroll"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The CodeBlock main USS class name.
Declaration
public const string ussClassName = "appui-code-block"
Field Value
| Type | Description |
|---|---|
| string |
viewportUssClassName
The USS class applied to the row inside the scroll content (line numbers + body).
Declaration
public const string viewportUssClassName = "appui-code-block__viewport"
Field Value
| Type | Description |
|---|---|
| string |
withLineNumbersUssClassName
The USS class toggled when showLineNumbers is enabled.
Declaration
public const string withLineNumbersUssClassName = "appui-code-block--with-line-numbers"
Field Value
| Type | Description |
|---|---|
| string |
Properties
code
The source code displayed in the body. Setting this triggers a re-render (with syntax highlighting if a grammar+theme pair is configured via USS).
Declaration
[CreateProperty]
[UxmlAttribute]
public string code { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
contentContainer
Children are managed internally — this element exposes no content container.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
language
The language identifier shown in the header (e.g. "csharp", "lua").
When null or empty the label is hidden but the copy button stays available.
Setting this also adds a appui-code-block--lang-<value> class so USS can
map the language to a grammar via --codeblock-grammar.
Declaration
[CreateProperty]
[UxmlAttribute]
public string language { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
showLineNumbers
When true, a left-side gutter shows 1-based line numbers next to the source.
The gutter and the body live inside the same ScrollView, so vertical
(and horizontal) scrolling keeps them aligned. Disabled by default.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool showLineNumbers { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |