Skip to content

Code block

Code with a filename header, light highlighting, copy, wrap and Show all, line numbers, highlighted lines, diffs and a streaming caret.

oxyn add code-block
MCP
src/main.rsRust
use oxyn_dioxus::Composer;

// The prompt box, wired to your agent.
#[component]
fn App() -> Element {
    let mut runs = use_signal(Vec::new);
    rsx! {
        Composer {
            placeholder: "Ask Atlas…",
            on_submit: move |s: Submission| runs.write().push(s.text),
Dioxus
rsx! {
    CodeBlock {
        code: source,
        filename: Some("src/main.rs".into()),
        language: "rust",
        max_lines: 10,
        line_numbers: true,
    }
}

Rendered from Rust at build time · goes live when visible

When to use

  • Code in answers and tool output: readable, copyable, collapsible, and still streaming.

When not to use

  • Markdown that renders correctly while it streams: use Streaming markdown instead.
  • Review an agent's change set: use Diff viewer instead.
  • Copy a value with immediate confirmation: use Copy button instead.

Installation

Code block is free and MIT. The oxyn CLI copies its code, and the components it needs, into src/oxyn/ of your Dioxus or GPUI project, so it is yours to change.

Terminal
cargo install --git https://github.com/ismailqureshi72/Oxyn oxyn-cli
oxyn init
oxyn add code-block
Rust
use crate::oxyn::{CodeBlock, PRIMITIVES_CSS};

Rather depend on the crate? cargo add oxyn-dioxus oxyn-core --git https://github.com/ismailqureshi72/Oxyn, then use oxyn_dioxus::{CodeBlock, PRIMITIVES_CSS};

Usage

Dioxus · rust
rsx! {
    CodeBlock {
        code: source,
        filename: Some("src/main.rs".into()),
        language: "rust",
        max_lines: 10,
        line_numbers: true,
    }
}

Examples

Source

Rust with line numbers, collapsed.

src/main.rsRust
use oxyn_dioxus::Composer;

// The prompt box, wired to your agent.
#[component]
fn App() -> Element {
    let mut runs = use_signal(Vec::new);
    rsx! {
        Composer {
            placeholder: "Ask Atlas…",
            on_submit: move |s: Submission| runs.write().push(s.text),
Code
rsx! {
    CodeBlock {
        code: source,
        filename: Some("src/main.rs".into()),
        language: "rust",
        max_lines: 10,
        line_numbers: true,
    }
}

Diff

A diff with added and removed lines.

auth/token.rsDiff
 fn verify(token: &str) -> Result<Claims> {
-    let exp = claims.exp;
-    if exp < now() { return Err(Expired) }
+    let exp = claims.exp + LEEWAY_SECS;
+    if exp < now() {
+        return Err(Expired);
+    }
     Ok(claims)
 }
Code
rsx! {
    CodeBlock {
        code: source,
        filename: Some("src/main.rs".into()),
        language: "diff",
        max_lines: 0,
        line_numbers: true,
    }
}

Streaming

A block still streaming (Oxyn plus).

agent.tsTypeScript
import { createAgent } from "@acme/agent";

const agent = createAgent({ model: "sonnet", tools: ["search", "shell"] });
// Stream tokens into the transcript
for await (const chunk of agent.run("Fix the flaky test")) {
  render(chunk);
}
Code
rsx! {
    CodeBlock {
        code: source,
        filename: Some("src/main.rs".into()),
        language: "ts",
        max_lines: 0,
        line_numbers: true,
    }
}

Highlighted

Highlighted lines.

src/main.rsRust
use oxyn_dioxus::Composer;

// The prompt box, wired to your agent.
#[component]
fn App() -> Element {
    let mut runs = use_signal(Vec::new);
    rsx! {
        Composer {
            placeholder: "Ask Atlas…",
            on_submit: move |s: Submission| runs.write().push(s.text),
        }
    }
}

fn budget(tokens: u32) -> f64 {
    tokens as f64 * 0.000_003
}
Code
rsx! {
    CodeBlock {
        code: source,
        filename: Some("src/main.rs".into()),
        language: "rust",
        max_lines: 0,
        line_numbers: true,
    }
}

API reference

CodeBlock

PropTypeDefaultDescription
coderequiredString—Source.
filenameOption<String>Default::default()File name.
languageString"tsx"Language id (`rust` · `ts` · `tsx` · `py` · `sh` · `json` · `diff`).
max_linesusizeDefault::default()Collapse beyond this many lines (0 = never).
line_numbersboolDefault::default()Line numbers.
highlight_linesVec<usize>Default::default()1-based lines to highlight.
wrapboolDefault::default()Wrap long lines.
streamingboolDefault::default()Streaming caret after the last line.
variantString"default"`default` · `bare` (no header).

Read from the Rust signature and doc comments.

Keyboard interactions

KeyAction
TabCopy, the scrollable source, Show all
EnterorSpaceCopy or toggle

Accessibility

  • A section labelled “file source code”; the pre is focusable.
  • Show all has aria-expanded and aria-controls.
From the spec

A section labelled “file source code”; the pre is focusable; Show all has aria-controls.

Motion & interaction

New lines rise in; the toggle label rolls and its chevron rotates; copy feedback comes from the copy button.

Timing
TransitionDurationEasingReduced motion
Line in240 msease.outinstant
Toggle roll200 msease.outswap
Caret1 s steps—static

Responsive behaviour

  • Scrolls horizontally, or wraps with the wrap toggle.
Edge cases (spec)

Long lines scroll horizontally unless wrapped. The highlighter is a light tokenizer (strings, numbers, comments, keywords, calls, types); unknown languages fall back to plain text.

Notes for AI

  • Import CodeBlock from oxyn_dioxus and include PRIMITIVES_CSS plus the token stylesheet once.
  • A GPUI version exists: CodeBlockEl in oxyn_gpui.
  • Props to set first: code.
  • Keep its states and timings as they ship; restyle it through the CSS variables rather than by hand.

Guides

Oxyn · 74 free components · 264 Pro components · 96 screens · 20 blocks · Dioxus and GPUI