- code! macroINCLUDED
- Pre-styled spansINCLUDED
- Themes (scoped CSS)× 30+
- SourceCode runtimeOPT-IN
- Runtime grammars+3.33 MiB
COMPILE-TIME MODESTATIC
A drop-in component with compile-time macros, runtime highlighting, and optional source-language detection.
$cargo add dioxus-code
Updating crates.io index
Adding dioxus-code v0.1 to dependencies
Done.
use dioxus::prelude::*;
#[component]
pub fn Counter() -> Element {
let mut count = use_signal(|| 0);
rsx! {
button {
onclick: move |_| count += 1,
"count: {count}"
}
}
}Enable the runtime feature when source comes from user input, generated files, or network responses.
dioxus-code = { version = "0.1", features = ["runtime"] }Pass any string through SourceCode. Provide a language hint when you know it, or enable detection for source-first workflows.
use dioxus::prelude::*;
use dioxus_code::{Code, CodeTheme, Language, SourceCode, Theme};
fn main() {
dioxus::launch(App);
}
#[component]
fn App() -> Element {
let source = use_signal(|| "fn main() {}".to_string());
rsx! {
Code {
src: SourceCode::new(Language::Rust, source()),
theme: CodeTheme::system(Theme::GITHUB_LIGHT, Theme::GITHUB_DARK),
}
}
}Use the macro for examples, docs, and any source checked in alongside your app. Highlight markup is generated at compile time.
use dioxus::prelude::*;
use dioxus_code::{Code, Theme, code};
fn main() {
dioxus::launch(App);
}
#[component]
fn App() -> Element {
rsx! {
Code {
src: code!("/snippets/example.rs"),
theme: Theme::GITHUB_DARK,
}
}
}