Code Block
Code to read and copy: a header (title, actions), the code — highlighted, numbered, wrapped, its lines marked — and a footer. In the look of the app's style.
Usage
import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import { SiCheckIcon, SiCopyIcon, SiFileCodeIcon,} from '@semantic-icons/lucide-icons';
@Component({ selector: 'app-code-block-usage-demo', imports: [ ScCodeBlock, ScCodeBlockHeader, ScCodeBlockTitle, ScCodeBlockActions, ScCodeBlockCopy, ScCodeBlockContent, SiCheckIcon, SiCopyIcon, SiFileCodeIcon, ], template: ` <div scCodeBlock> <div scCodeBlockHeader> <div scCodeBlockTitle><svg siFileCodeIcon></svg>counter.ts</div> <div scCodeBlockActions> <button [scCodeBlockCopy]="source" #copy="scCodeBlockCopy"> @if (copy.copied()) { <svg siCheckIcon></svg> } @else { <svg siCopyIcon></svg> } <span class="sr-only">Copy code</span> </button> </div> </div> <pre scCodeBlockContent [code]="source" language="angular-ts"></pre> <span aria-live="polite" class="sr-only"> @if (copy.copied()) { Copied } </span> </div> `, host: { class: 'block w-full max-w-md' }, encapsulation: ViewEncapsulation.None,})export class CodeBlockUsageDemo { private readonly source = `import { Component, signal } from '@angular/core';
@Component({ selector: 'app-counter', template: \`<button (click)="count.set(count() + 1)">{{ count() }}</button>\`,})export class Counter { readonly count = signal(0);}`;}Installation
It has its own entry point, @semantic-components/ui/code-block, built on shiki — an optional peer of ui: install it where you use the component.
import { ScCodeBlock, ScCodeBlockContent } from '@semantic-components/ui/code-block';Then add the highlighter, a feature of the library, in your app's config — without it, code shows plain:
import { provideSemanticComponents } from '@semantic-components/ui';import { withCodeHighlighter } from '@semantic-components/ui/code-block';
export const appConfig: ApplicationConfig = { providers: [provideSemanticComponents({}, withCodeHighlighter())],};Anatomy
Every part is a directive on your element; the title, the buttons and their names are yours. The code is an input, highlighted in its language.
import { ScCodeBlock, ScCodeBlockHeader, ScCodeBlockTitle, ScCodeBlockActions, ScCodeBlockCopy, ScCodeBlockContent, ScCodeBlockFooter,} from '@semantic-components/ui/code-block';<div scCodeBlock> <div scCodeBlockHeader> <div scCodeBlockTitle>…</div> <div scCodeBlockActions> <button [scCodeBlockCopy]="code">…</button> </div> </div> <pre scCodeBlockContent [code]="code" language="…"></pre> <div scCodeBlockFooter>…</div></div>Highlighting
Shiki, loaded with the first block; each language the first time it is used: typescript, angular-ts, angular-html, javascript, html, css, scss, json, yaml, markdown, bash, python, sql, diff — and ansi, for a terminal's colours. Another language, or no highlighter, shows the code plain. The code is always left to right.
Examples
Basic
$ pnpm build> next build
✓ Compiled successfully in 4.2s✓ Linting and checking validity of types✓ Generating static pages (12/12)import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockContent,} from '@semantic-components/ui/code-block';
/** Only the code: plain, no language. */@Component({ selector: 'app-basic-code-block-demo', imports: [ScCodeBlock, ScCodeBlockContent],Header
$ pnpm build> next build
✓ Compiled successfully in 4.2s✓ Linting and checking validity of types✓ Generating static pages (12/12)import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import {Actions
$ pnpm build> next build
✓ Compiled successfully in 4.2s✓ Linting and checking validity of types✓ Generating static pages (12/12)import { DOCUMENT } from '@angular/common';import { Component, ViewEncapsulation, inject, signal } from '@angular/core';import { ScButton } from '@semantic-components/ui';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,Floating Actions
$ pnpm build> next build
✓ Compiled successfully in 4.2s✓ Linting and checking validity of types✓ Generating static pages (12/12)import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy,} from '@semantic-components/ui/code-block';import { SiCheckIcon, SiCopyIcon } from '@semantic-icons/lucide-icons';
/** Actions straight in the block, no header: over the code, shown on hover or focus (always on touch). */Line Numbers
import { Component, signal } from '@angular/core';
@Component({ selector: 'app-counter', template: `<button (click)="count.set(count() + 1)">{{ count() }}</button>`,})export class Counter { readonly count = signal(0);}import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockContent,} from '@semantic-components/ui/code-block';
/** lineNumbers: each line numbered; the numbers are not copied. */@Component({ selector: 'app-line-numbers-code-block-demo', imports: [ScCodeBlock, ScCodeBlockContent],Start Line
readonly total = computed(() => this.items().reduce((sum, item) => sum + item.price, 0), );import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockContent, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';
/** startLine: an excerpt numbered from where it starts in its file. */@Component({Wrap
npx create-semantic-app my-application --template dashboard --style nova --package-manager pnpm --no-git --skip-installimport { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockContent,} from '@semantic-components/ui/code-block';
/** wrap: long lines wrap instead of scrolling. */@Component({ selector: 'app-wrap-code-block-demo', imports: [ScCodeBlock, ScCodeBlockContent],Highlighted Lines
import { Component, signal } from '@angular/core';
@Component({ selector: 'app-counter', template: `<button (click)="count.set(count() + 1)">{{ count() }}</button>`,})export class Counter { readonly count = signal(0);}import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockContent,} from '@semantic-components/ui/code-block';
/** highlightedLines: lines (from 1) to draw the eye to. */@Component({ selector: 'app-highlighted-lines-code-block-demo', imports: [ScCodeBlock, ScCodeBlockContent],Diff
export function greet(name: string) { return 'Hello ' + name; return `Hello, ${name}!`;}import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import { SiCheckIcon, SiCopyIcon } from '@semantic-icons/lucide-icons';Height
[12:00:00] GET /api/items/1 200 in 12ms[12:00:01] GET /api/items/2 200 in 13ms[12:00:02] GET /api/items/3 200 in 14ms[12:00:03] GET /api/items/4 200 in 15ms[12:00:04] GET /api/items/5 200 in 16ms[12:00:05] GET /api/items/6 200 in 17ms[12:00:06] GET /api/items/7 200 in 18ms[12:00:07] GET /api/items/8 200 in 19ms[12:00:08] GET /api/items/9 200 in 20ms[12:00:09] GET /api/items/10 200 in 21ms[12:00:10] GET /api/items/11 200 in 22ms[12:00:11] GET /api/items/12 200 in 23ms[12:00:12] GET /api/items/13 200 in 24ms[12:00:13] GET /api/items/14 200 in 25ms[12:00:14] GET /api/items/15 200 in 26ms[12:00:15] GET /api/items/16 200 in 27ms[12:00:16] GET /api/items/17 200 in 28ms[12:00:17] GET /api/items/18 200 in 29ms[12:00:18] GET /api/items/19 200 in 30ms[12:00:19] GET /api/items/20 200 in 31ms[12:00:20] GET /api/items/21 200 in 32ms[12:00:21] GET /api/items/22 200 in 33ms[12:00:22] GET /api/items/23 200 in 34ms[12:00:23] GET /api/items/24 200 in 35msimport { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import { SiCheckIcon, SiCopyIcon } from '@semantic-icons/lucide-icons';Show More
import { Component, signal } from '@angular/core';
@Component({ selector: 'app-chat', template: ` @for (message of messages(); track message.id) { <p>{{ message.text }}</p> } <form (submit)="send($event)"> <input [value]="draft()" (input)="draft.set($any($event.target).value)" /> <button [disabled]="!draft()">Send</button> </form> `,})export class Chat { readonly messages = signal<{ id: number; text: string }[]>([]); readonly draft = signal('');
send(event: Event): void { event.preventDefault(); const text = this.draft().trim(); if (!text) return; this.messages.update((list) => [...list, { id: list.length, text }]); this.draft.set(''); }}import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScButton } from '@semantic-components/ui';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockFooter, ScCodeBlockHeader, ScCodeBlockTitle,Size
$ pnpm build> next build
✓ Compiled successfully in 4.2s✓ Linting and checking validity of types✓ Generating static pages (12/12)$ pnpm build> next build
✓ Compiled successfully in 4.2s✓ Linting and checking validity of types✓ Generating static pages (12/12)import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import {Destructive
Error: Cannot find module './config' at resolve (node:internal/modules/cjs/loader:1145:15) at load (src/app.ts:3:1)import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import {Tabs
import { Component, ViewEncapsulation } from '@angular/core';import { ScTabs, ScTabsList, ScTabsPanel, ScTabsPanelContent, ScTabsTab,} from '@semantic-components/ui';import { ScCodeBlock,Languages
.button { padding: 0.5rem 1rem; border-radius: var(--radius);}{ "name": "my-app", "private": true}def greet(name: str) -> str: return f"Hello, {name}!"import { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockContent, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';
/** language: each block highlighted in its own (loaded the first time it is used). */@Component({Terminal Output
✓ 12 tests passed! 1 test skipped✗ 2 tests failedDone in 1.8simport { Component, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';import {RTL
ثبّت المكتبة، ثم هيّئ مشروعك:
pnpm add @semantic-components/uipnpm dlx semantic-components initimport { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { ScCodeBlock, ScCodeBlockActions, ScCodeBlockContent, ScCodeBlockCopy, ScCodeBlockHeader, ScCodeBlockTitle,} from '@semantic-components/ui/code-block';API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScCodeBlock | div[scCodeBlock] | The block: variant ('default' or 'destructive'), size ('default' or 'sm'). |
ScCodeBlockHeader, ScCodeBlockTitle | div[scCodeBlockHeader], div[scCodeBlockTitle] | The header, its title (your icon and text) first. |
ScCodeBlockActions | div[scCodeBlockActions] | Your buttons: at the header’s end; straight in the block, over the code, shown on hover or focus. |
ScCodeBlockCopy | button[scCodeBlockCopy] | Copies its value, [scCodeBlockCopy]; copied() for two seconds (duration). variant and size: the button's, 'ghost' and 'icon-xs'. |
ScCodeBlockContent | pre[scCodeBlockContent] | The code: code, language, lineNumbers, startLine, wrap, highlightedLines, addedLines, removedLines. Focusable, to scroll it. |
ScCodeBlockFooter | div[scCodeBlockFooter] | A footer under the code. |