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-blockuse 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),
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.
cargo install --git https://github.com/ismailqureshi72/Oxyn oxyn-cli
oxyn init
oxyn add code-blockuse 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
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.
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.
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).
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.
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
| Prop | Type | Default | Description |
|---|---|---|---|
coderequired | String | — | Source. |
filename | Option<String> | Default::default() | File name. |
language | String | "tsx" | Language id (`rust` · `ts` · `tsx` · `py` · `sh` · `json` · `diff`). |
max_lines | usize | Default::default() | Collapse beyond this many lines (0 = never). |
line_numbers | bool | Default::default() | Line numbers. |
highlight_lines | Vec<usize> | Default::default() | 1-based lines to highlight. |
wrap | bool | Default::default() | Wrap long lines. |
streaming | bool | Default::default() | Streaming caret after the last line. |
variant | String | "default" | `default` · `bare` (no header). |
Read from the Rust signature and doc comments.
Keyboard interactions
| Key | Action |
|---|---|
| Tab | Copy, the scrollable source, Show all |
| EnterorSpace | Copy 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
| Transition | Duration | Easing | Reduced motion |
|---|---|---|---|
| Line in | 240 ms | ease.out | instant |
| Toggle roll | 200 ms | ease.out | swap |
| Caret | 1 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
CodeBlockfromoxyn_dioxusand includePRIMITIVES_CSSplus the token stylesheet once. - A GPUI version exists:
CodeBlockElinoxyn_gpui. - Props to set first:
code. - Keep its states and timings as they ship; restyle it through the CSS variables rather than by hand.