Skip to main content
Neural UIv2.0.0Documentation
View v1 GitHub

CodeBlock

Present copyable source code with a language badge, density and line wrapping.

Category
Layout and content
Import
@neural-ui/core/code-block
Selector
neu-code-block
import { NeuCodeBlockComponent } from '@neural-ui/core/code-block';

Usage

Pass plain source text to code. language supplies a recognizable badge; lang is an optional display-label override, not the document locale. wrap allows long lines to wrap; otherwise the code body scrolls horizontally. density changes spacing. Code is escaped text: this component does not execute it or apply syntax highlighting.

Examples

Try the interaction and open Code for the complete implementation.

Examples
TypeScript
const greeting = 'Hello, Neural UI';
console.log(greeting);

Try and configure

Change the options, try the result and see the template update. Code contains the full executable example.

Try and configure
TypeScript
const greeting = 'Hello, Neural UI';
console.log(greeting);

Template generated from your current options. Open Code for the complete example.

template.html
<neu-code-block
  [code]="code()"
  language="typescript"
  density="normal"
  [wrap]="true"
  [copiedAriaLabel]="'Example code copied'"
  [copiedLabel]="'Copied'"
  [copyAriaLabel]="'Copy example code'"
  [copyLabel]="'Copy code'"
/>

Data and content

The copy button writes the exact code string to the clipboard and confirms only a successful copy. Labels can be translated. CodeBlock has no Outputs or Templates; supply code and style it with its public tokens.

Accessibility and keyboard

Reach the copy button with Tab and activate it with Enter or Space. Keep copyAriaLabel and copiedAriaLabel meaningful, especially when using short visible labels. Wrapped code is useful on narrow screens; unwrapped source has local horizontal scrolling.

Key
Action
Tab Reach the enabled copy button.
Enter Copy the code from the focused copy button.
Space Copy the code from the focused copy button.

API

Inputs and Outputs match the installed package. Named types link to definitions. Every available Template has a Demo and Code example.

Inputs

Configure the component with [property]="value". Your application supplies these values; the component does not replace the state you pass in.

Name
Type
Default
Template binding
Description
codestring''[code]Exact source text displayed and copied.
copiedAriaLabelstring | undefinedundefined[copiedAriaLabel]Label for copy confirmation. Translate it for your application.
copiedLabelstring | undefinedundefined[copiedLabel]Label for successful copy. Translate it for your application.
copyAriaLabelstring | undefinedundefined[copyAriaLabel]Label for copy button. Translate it for your application.
copyLabelstring | undefinedundefined[copyLabel]Label for copy action. Translate it for your application.
densityNeuDensity'normal'[density]Spacing scale: compact, normal or relaxed.
langstring''[lang]Optional visible badge-label override.
languagestring''[language]Language identifier used to derive the badge.
wrapbooleanfalse[wrap]Wrap long source lines instead of local horizontal scrolling.

Templates

ng-content projects content into the component. TemplateRef inputs receive a template; ng-template directives identify templates with a typed context. A directive shared by an entrypoint is not necessarily a slot of this component.

This component does not expose content projection or public customization templates.

Public Types

Open a type to inspect its definition and interface fields.

Styles and tokens

Customize supported public tokens and check both themes. Example illustrations and application state are not Core inputs.

Specific tokens

Token
Purpose
State / variant
Default / source
Fallback
--neu-code-block-accent Inherited theme value used by this component Field surface #9ab8ffRoot declarationNo token alias
--neu-code-block-bar-bg Inherited theme value used by this component Field surface #16161eRoot declarationNo token alias
--neu-code-block-bg Inherited theme value used by this component Field surface #1a1b26Root declarationNo token alias
--neu-code-block-border Inherited theme value used by this component Field surface rgba(255, 255, 255, 0.14)Root declarationNo token alias
--neu-code-block-copy-bg Inherited theme value used by this component Field surface rgba(255, 255, 255, 0.12)Root declarationNo token alias
--neu-code-block-copy-border Inherited theme value used by this component Field surface rgba(255, 255, 255, 0.22)Root declarationNo token alias
--neu-code-block-copy-hover-bg Inherited theme value used by this component Field surface rgba(255, 255, 255, 0.18)Root declarationNo token alias
--neu-code-block-copy-hover-border Inherited theme value used by this component Field surface rgba(255, 255, 255, 0.34)Root declarationNo token alias
--neu-code-block-copy-hover-text Inherited theme value used by this component Field surface #ffffffRoot declarationNo token alias
--neu-code-block-copy-text Inherited theme value used by this component Field surface #d9e2ffRoot declarationNo token alias
--neu-code-block-muted Inherited theme value used by this component Field surface #565f89Root declarationNo token alias
--neu-code-block-text Inherited theme value used by this component Field surface #d9e2ffRoot declarationNo token alias

Shared tokens used

Override these on a local wrapper to affect this example. An override on :root affects other components that use the same token.

Token
Purpose here
Other impact
Default / fallback
--neu-focus-ring Normal keyboard-focus ring Shared by other Core consumers; scope the override. 0 0 0 var(--neu-focus-ring-width) rgba(0, 122, 255, 0.15)
--neu-font-mono Text typography Shared by other Core consumers; scope the override. 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace
--neu-font-sans Text typography Shared by other Core consumers; scope the override. 'Inter', system-ui, -apple-system, BlinkMacSystemFont, sans-serif
--neu-radius-lg Corner radius Shared by other Core consumers; scope the override. 12px
--neu-radius-sm Corner radius Shared by other Core consumers; scope the override. 6px
--neu-space-3 Spacing between content and controls Shared by other Core consumers; scope the override. 0.75rem
--neu-transition Timing of state changes Shared by other Core consumers; scope the override. 200ms ease