Skip to content
Logic2BUI

Search docs

Search components and documentation

New
Menu

Code Block

A styled code container with a one-click copy button, dependency-free.

bash
npx logic2b@next add code-block

Installation

npx logic2b@next add code-block

Usage

import { CodeBlock } from "@/components/ui/code-block"

<CodeBlock language="tsx" code={`const answer = 42`} />

The copy button writes code to the clipboard and shows a checkmark for two seconds. Omit language to render a bare block with a floating copy button in the corner.

Highlighting

CodeBlock is deliberately dependency-free and does not ship a syntax highlighter. To add highlighting, render your highlighter’s output inside the <pre> — e.g. pass Shiki/Prism-generated HTML — or wrap this component. Keeping the base free of a highlighter means it stays tiny and framework-agnostic.

Live playground

Edit the component props and inspect the exact JSX before copying it. The preview loads only when this section enters the viewport.

Generated JSX
<CodeBlock
  code="const ready = true\nconsole.log(ready)"
  language="ts"
 />

API reference

Generated from the public TypeScript exports in src/ui/code-block.tsx.

View JSON

CodeBlockProps

Type
interface CodeBlockProps extends Omit<React.ComponentProps<"div">, "children"> { /** The code to render and copy. */ code: string /** Optional language label shown in the header (display only — no highlighting). */ language?: string }

CodeBlock

Component

A styled code container with a one-click copy button. Dependency-free (no syntax highlighter): renders the code verbatim in a `<pre>` and copies it via the Clipboard API, with a copied-state affordance.

Props: CodeBlockProps
PropTypeRequiredDefaultDescription
codestringYes—The code to render and copy.
languagestringNo—Optional language label shown in the header (display only — no highlighting).

Accessibility contract

Machine-readable behavior and consumer responsibilities for code-block.

View JSON
Support
Native semantics
Pattern
code region with copy action
HTML pre/code/button
Keyboard interactions for code-block
KeysBehavior
Enter SpaceActivate the focused control.

Built-in semantics

  • Code uses pre/code semantics and the copy button updates its accessible label after success.

Consumer responsibilities

  • Provide a language label when it helps comprehension and surface copy failures in application context if critical.