Skip to content

Commit a8de49e

Browse files
committed
add a readme
1 parent c695f60 commit a8de49e

4 files changed

Lines changed: 126 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ resolver = "3"
1414
[workspace.package]
1515
version = "0.1.0"
1616
edition = "2024"
17-
license = "MIT OR Apache-2.0"
17+
license = "MIT"
1818
repository = "https://github.com/ealmloff/dioxus-code"
1919
keywords = ["dioxus", "code", "highlighting", "editor"]
2020
categories = ["gui", "web-programming"]

README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<div align="center">
2+
<h1>dioxus-code</h1>
3+
<p><strong>Syntax-highlighted code blocks for Dioxus.</strong></p>
4+
</div>
5+
6+
<div align="center">
7+
<a href="https://crates.io/crates/dioxus-code">
8+
<img src="https://img.shields.io/crates/v/dioxus-code.svg?style=flat-square" alt="Crates.io version" />
9+
</a>
10+
<a href="https://crates.io/crates/dioxus-code">
11+
<img src="https://img.shields.io/crates/d/dioxus-code.svg?style=flat-square" alt="Downloads" />
12+
</a>
13+
<a href="https://docs.rs/dioxus-code">
14+
<img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs" />
15+
</a>
16+
</div>
17+
18+
---
19+
20+
A small Dioxus component for rendering source code with proper highlighting. Parsing is powered by [arborium](https://crates.io/crates/arborium); themes ship as scoped CSS so you can mix several on one page.
21+
22+
Two ways to highlight:
23+
24+
- **`code!()` macro** — parses at compile time. The runtime ships only the spans, no parser. Default.
25+
- **`SourceCode`** — parses at runtime. Opt in with the `runtime` feature when the source isn't known until the user types it.
26+
27+
## Quick start
28+
29+
```toml
30+
[dependencies]
31+
dioxus-code = "0.1"
32+
```
33+
34+
```rust
35+
use dioxus::prelude::*;
36+
use dioxus_code::{Code, Theme, code};
37+
38+
#[component]
39+
fn ReadMe() -> Element {
40+
rsx! {
41+
Code {
42+
src: code!("/snippets/demo.rs"),
43+
theme: Theme::TOKYO_NIGHT,
44+
}
45+
}
46+
}
47+
```
48+
49+
The path is resolved from the consumer's `CARGO_MANIFEST_DIR`. `concat!` and `env!` work too.
50+
51+
## Runtime highlighting
52+
53+
For editor-style use cases where the source isn't known at compile time:
54+
55+
```toml
56+
[dependencies]
57+
dioxus-code = { version = "0.1", features = ["runtime"] }
58+
```
59+
60+
```rust
61+
use dioxus_code::{Code, SourceCode, Theme};
62+
63+
rsx! {
64+
Code {
65+
src: SourceCode::new(user_input).with_language("rust"),
66+
theme: Theme::GITHUB_LIGHT,
67+
}
68+
}
69+
```
70+
71+
Language can be set explicitly, inferred from a filename via `with_name("main.rs")`, or auto-detected from the source. The default `runtime` feature includes Rust; pass `lang-python`, `lang-toml`, or `all-languages` for the rest.
72+
73+
## Editor
74+
75+
`dioxus-code-editor` is a sibling crate that pairs the highlighter with a `contenteditable` input layer:
76+
77+
```rust
78+
use dioxus_code_editor::CodeEditor;
79+
use dioxus_code::Theme;
80+
81+
let mut source = use_signal(|| String::new());
82+
83+
rsx! {
84+
CodeEditor {
85+
value: source(),
86+
language: "rust",
87+
theme: Theme::TOKYO_NIGHT,
88+
oninput: move |value| source.set(value),
89+
}
90+
}
91+
```
92+
93+
It is controlled — drive `value` from your own signal and update it inside `oninput`.
94+
95+
## Themes
96+
97+
Thirty-odd built-ins, including Tokyo Night, Catppuccin (all four), Dracula, GitHub Light/Dark, Gruvbox, Nord, One Dark, Rosé Pine, Solarized, the Rustdoc themes, and others. Each is exposed as a `Theme` constant and a CSS asset; pages with multiple themes render side-by-side without leaking styles.
98+
99+
```rust
100+
Code { src: code!("/example.rs"), theme: Theme::CATPPUCCIN_MOCHA }
101+
```
102+
103+
## Examples
104+
105+
```sh
106+
dx serve --example dioxus-code-basic # macro + runtime side by side
107+
dx serve --example dioxus-code-macro-only # compile-time only, no parser in the binary
108+
dx serve --example dioxus-code-live-input # textarea bound to runtime highlighter
109+
```
110+
111+
## Workspace layout
112+
113+
| crate | purpose |
114+
|---|---|
115+
| `dioxus-code` | The `Code` component, themes, and runtime/macro entry points. |
116+
| `dioxus-code-editor` | Editable code surface built on `Code`. |
117+
| `dioxus-code-macro` | Implementation of `code!()`. Re-exported by `dioxus-code` under the `macro` feature. |
118+
119+
## License
120+
121+
[MIT](LICENSE).

code-editor/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn CodeEditor(props: CodeEditorProps) -> Element {
6868
let lines = editor_lines(tree.source(), tree.spans());
6969
let line_count = lines.len();
7070
let theme_asset = props.theme.asset();
71+
let theme_key = props.theme.name();
7172
let class = editor_class(props.theme, props.line_numbers, &props.class);
7273
let (input_value, input_version) = synced_input_value(&input_sync, &props.value);
7374
let contenteditable = if props.read_only {
@@ -78,8 +79,8 @@ pub fn CodeEditor(props: CodeEditorProps) -> Element {
7879
let readonly = props.read_only.then_some("true");
7980

8081
rsx! {
82+
document::Stylesheet { key: "{theme_key}", href: theme_asset }
8183
document::Stylesheet { href: CODE_EDITOR_CSS }
82-
document::Stylesheet { href: theme_asset }
8384
div {
8485
class,
8586
if props.line_numbers {

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,13 @@ pub fn Code(props: CodeProps) -> Element {
407407
let segments = code_segments(&source, &spans);
408408
let class = format!("dxc {}", props.theme.class());
409409
let theme_asset = props.theme.asset();
410+
let theme_key = props.theme.name();
410411
let language = language.as_deref().unwrap_or("text");
411412
let error = error.as_deref();
412413

413414
rsx! {
415+
document::Stylesheet { key: "{theme_key}", href: theme_asset }
414416
document::Stylesheet { href: STYLE }
415-
document::Stylesheet { href: theme_asset }
416417
pre {
417418
class,
418419
"data-language": language,

0 commit comments

Comments
 (0)