Skip to content

Commit 583fdfa

Browse files
committed
docs: big updates to the reference
1 parent caf772c commit 583fdfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+724
-245
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dioxus-mobile = { path = "./packages/mobile", optional = true }
1919

2020
[features]
2121
# core
22-
default = ["core"]
22+
default = ["core", "ssr"]
2323
core = ["macro", "hooks", "html"]
2424
macro = ["dioxus-core-macro"]
2525
hooks = ["dioxus-hooks"]
@@ -48,6 +48,7 @@ async-std = { version = "1.9.0", features = ["attributes"] }
4848
im-rc = "15.0.0"
4949
rand = { version = "0.8.4", features = ["small_rng"] }
5050
fxhash = "0.2.1"
51+
gloo-timers = "0.2.1"
5152

5253
[workspace]
5354
members = [

docs/main-concepts/03-rsx.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Commas are entirely optional, but might be useful to delineate between elements
6060
The `render` function provides an **extremely efficient** allocator for VNodes and text, so try not to use the `format!` macro in your components. Rust's default `ToString` methods pass through the global allocator, but all text in components is allocated inside a manually-managed Bump arena. To push you in the right direction, all text-based attributes take `std::fmt::Arguments` directly, so you'll want to reach for `format_args!` when the built-in `f-string` interpolation just doesn't cut it.
6161

6262
```rust
63-
static Example: FC<()> = |cx| {
63+
pub static Example: FC<()> = |cx| {
6464

6565
let text = "example";
6666

docs/platforms/06-components.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn Example(cx: Context, name: String) -> VNode {
2525
// or
2626

2727
#[functional_component]
28-
static Example: FC = |cx, name: String| html! { <div> "Hello {name}!" </div> };
28+
pub static Example: FC = |cx, name: String| html! { <div> "Hello {name}!" </div> };
2929
```
3030

3131
The final output of components must be a tree of VNodes. We provide an html macro for using JSX-style syntax to write these, though, you could use any macro, DSL, templating engine, or the constructors directly.

examples/_examples/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn main() {
2525
#[derive(Debug)]
2626
struct CustomContext([&'static str; 3]);
2727

28-
static Example: FC<()> = |cx| {
28+
pub static Example: FC<()> = |cx| {
2929
cx.use_create_context(|| CustomContext(["Jack", "Jill", "Bob"]));
3030

3131
cx.render(rsx! {

examples/_examples/hello.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() {
1313
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(Example));
1414
}
1515

16-
static Example: FC<()> = |cx| {
16+
pub static Example: FC<()> = |cx| {
1717
let nodes = (0..500).map(|f| rsx! (li {"div"}));
1818
cx.render(rsx! {
1919
div {

examples/_examples/infer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
}
1515

1616
// this is a component
17-
static Example: FC<()> = |cx| {
17+
pub static Example: FC<()> = |cx| {
1818
let (event, set_event) = use_state_classic(cx, || None);
1919

2020
let handler = move |evt| {
@@ -50,7 +50,7 @@ struct ExampleProps {
5050
name: String,
5151
}
5252

53-
static Example2: FC<ExampleProps> = |cx| {
53+
pub static Example2: FC<ExampleProps> = |cx| {
5454
cx.render(rsx! {
5555
div {
5656
h1 {"hello {cx.name}"}

examples/_examples/input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static App: FC<()> = |cx| {
3737
})
3838
};
3939

40-
static Example: FC<()> = |cx| {
40+
pub static Example: FC<()> = |cx| {
4141
cx.render(rsx! {
4242
div { class: "max-w-lg max-w-xs bg-blue-800 shadow-2xl rounded-lg mx-auto text-center py-12 mt-4 rounded-xl"
4343
div { class: "container py-5 max-w-md mx-auto"

examples/_examples/jackjill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(Example))
1010
}
1111

12-
static Example: FC<()> = |cx| {
12+
pub static Example: FC<()> = |cx| {
1313
let (name, set_name) = use_state_classic(cx, || "...?");
1414

1515
log::debug!("Running component....");

examples/_examples/jackjillrsx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(Example))
1010
}
1111

12-
static Example: FC<()> = |cx| {
12+
pub static Example: FC<()> = |cx| {
1313
let (name, set_name) = use_state_classic(cx, || "...?");
1414
cx.render(rsx! {
1515
section { class: "py-12 px-4 text-center"

examples/_examples/many.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(Example));
1212
}
1313

14-
static Example: FC<()> = |cx| {
14+
pub static Example: FC<()> = |cx| {
1515
cx.render(rsx! {
1616
div {
1717
span {

examples/_examples/rsxt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct ExampleProps {
2727
initial_name: &'static str,
2828
}
2929

30-
static Example: FC<ExampleProps> = |cx| {
30+
pub static Example: FC<ExampleProps> = |cx| {
3131
let name = use_state(cx, move || cx.initial_name);
3232

3333
cx.render(rsx! {

examples/doggo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use dioxus::prelude::*;
22
fn main() {}
33

4-
static Example: FC<()> = |cx| {
4+
pub static Example: FC<()> = |cx| {
55
let (g, set_g) = use_state(cx, || 0).classic();
66
let v = (0..10).map(move |f| {
77
rsx!(li {

examples/examples/demo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
.expect("Webview finished");
1818
}
1919

20-
// static Example: FC<()> = |cx| {
20+
// pub static Example: FC<()> = |cx| {
2121
// cx.render(html! {
2222
// <div>
2323
// <svg class="octicon octicon-star v-align-text-bottom"
@@ -36,7 +36,7 @@ fn main() {
3636
// </div>
3737
// })
3838
// };
39-
static Example: FC<()> = |cx| {
39+
pub static Example: FC<()> = |cx| {
4040
cx.render(rsx! {
4141
div {
4242
class: "flex items-center justify-center flex-col"

examples/model.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ use dioxus::prelude::*;
2020

2121
const STYLE: &str = include_str!("./assets/calculator.css");
2222
fn main() {
23-
dioxus::desktop::launch(App, |cfg| {
24-
cfg.with_title("Calculator Demo")
25-
.with_resizable(true)
26-
.with_skip_taskbar(true)
27-
})
28-
.expect("failed to launch dioxus app");
23+
dioxus::desktop::launch(App, |cfg| cfg.with_title("Calculator Demo"))
24+
.expect("failed to launch dioxus app");
2925
}
3026

3127
static App: FC<()> = |cx| {

examples/reference/antipatterns.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
//! Feel free to file a PR or Issue if you run into another antipattern that you think users of Dioxus should know about.
1717
use dioxus::prelude::*;
1818

19-
fn main() {}
20-
2119
/// Antipattern: Iterators without keys
2220
/// -----------------------------------
2321
///
@@ -174,3 +172,10 @@ static _example: FC<()> = |cx| todo!();
174172
/// This will only include the `core` dioxus crate which is relatively slim and fast to compile and avoids target-specific
175173
/// libraries.
176174
static __example: FC<()> = |cx| todo!();
175+
176+
pub static Example: FC<()> = |cx| {
177+
cx.render(rsx! {
178+
AntipatternNoKeys { data: std::collections::HashMap::new() }
179+
AntipatternNestedFragments {}
180+
})
181+
};

examples/reference/async.rs

-10
This file was deleted.

examples/reference/basics.rs

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1+
//! Example: The basics of Dioxus
2+
//! ----------------------------
3+
//!
4+
//! This small example covers some of the basics of Dioxus including
5+
//! - Components
6+
//! - Props
7+
//! - Children
8+
//! - the rsx! macro
9+
110
use dioxus::prelude::*;
2-
fn main() {}
311

4-
static Example: FC<()> = |cx| {
12+
pub static Example: FC<()> = |cx| {
513
cx.render(rsx! {
614
div {
15+
Greeting {
16+
name: "Dioxus"
17+
div { "Dioxus is a fun, fast, and portable UI framework for Rust" }
18+
}
19+
}
20+
})
21+
};
722

23+
#[derive(PartialEq, Props)]
24+
struct GreetingProps {
25+
name: &'static str,
26+
}
27+
28+
static Greeting: FC<GreetingProps> = |cx| {
29+
cx.render(rsx! {
30+
div {
31+
h1 { "Hello, {cx.name}!" }
32+
p { "Welcome to the Diouxs framework" }
33+
br {}
34+
{cx.children()}
835
}
936
})
1037
};

examples/reference/children.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
//! In the future, this might become a runtime error, so consider it an error today.
1818
1919
use dioxus::prelude::*;
20-
fn main() {}
2120

22-
static App: FC<()> = |cx| {
21+
pub static Example: FC<()> = |cx| {
2322
cx.render(rsx! {
2423
div {
2524
Banner {
@@ -32,7 +31,7 @@ static App: FC<()> = |cx| {
3231
})
3332
};
3433

35-
static Banner: FC<()> = |cx| {
34+
pub static Banner: FC<()> = |cx| {
3635
cx.render(rsx! {
3736
div {
3837
h1 { "This is a great banner!" }

examples/reference/conditional_rendering.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
1212
use dioxus::prelude::*;
1313

14-
fn main() {}
15-
1614
// Convert a boolean conditional into a hide/show
1715
#[derive(PartialEq, Props)]
18-
struct MyProps {
16+
pub struct MyProps {
1917
should_show: bool,
2018
}
21-
static Example: FC<MyProps> = |cx| {
19+
pub static Example0: FC<MyProps> = |cx| {
2220
cx.render(rsx! {
2321
div {
2422
{cx.should_show.then(|| rsx!{
@@ -38,10 +36,10 @@ static Example: FC<MyProps> = |cx| {
3836
// In short:
3937
// `rsx!(in cx, ...)` is shorthand for `cx.render(rsx!(...))`
4038
#[derive(PartialEq, Props)]
41-
struct MyProps1 {
39+
pub struct MyProps1 {
4240
should_show: bool,
4341
}
44-
static Example1: FC<MyProps1> = |cx| {
42+
pub static Example1: FC<MyProps1> = |cx| {
4543
cx.render(rsx! {
4644
div {
4745
// With matching
@@ -70,16 +68,16 @@ static Example1: FC<MyProps1> = |cx| {
7068
/// Matching can be expanded
7169
7270
#[derive(PartialEq)]
73-
enum Color {
71+
pub enum Color {
7472
Green,
7573
Yellow,
7674
Red,
7775
}
7876
#[derive(PartialEq, Props)]
79-
struct MyProps2 {
77+
pub struct MyProps2 {
8078
color: Color,
8179
}
82-
static Example2: FC<MyProps2> = |cx| {
80+
pub static Example2: FC<MyProps2> = |cx| {
8381
cx.render(rsx! {
8482
div {
8583
{match cx.color {
@@ -90,3 +88,29 @@ static Example2: FC<MyProps2> = |cx| {
9088
}
9189
})
9290
};
91+
92+
pub static Example: FC<()> = |cx| {
93+
let should_show = use_state(cx, || false);
94+
let mut color_index = use_state(cx, || 0);
95+
let color = match *color_index % 2 {
96+
2 => Color::Green,
97+
1 => Color::Yellow,
98+
_ => Color::Red,
99+
};
100+
101+
cx.render(rsx! {
102+
div {
103+
button {
104+
onclick: move |_| should_show.set(!*should_show)
105+
"click to toggle showing the examples"
106+
}
107+
button {
108+
onclick: move |_| color_index += 1
109+
"click to for the enxt color"
110+
}
111+
Example0 { should_show: *should_show }
112+
Example1 { should_show: *should_show }
113+
Example2 { color: color }
114+
}
115+
})
116+
};
+28-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
use dioxus::prelude::*;
22
fn main() {}
33

4-
static Example: FC<()> = |cx| {
4+
pub static Example: FC<()> = |cx| {
55
cx.render(rsx! {
66
div {
77

88
}
99
})
1010
};
11+
12+
// A controlled component:
13+
static ControlledSelect: FC<()> = |cx| {
14+
let value = use_state(cx, || String::from("Grapefruit"));
15+
cx.render(rsx! {
16+
select { value: "{value}", onchange: move |evt| value.set(evt.value()),
17+
option { value: "Grapefruit", "Grapefruit"}
18+
option { value: "Lime", "Lime"}
19+
option { value: "Coconut", "Coconut"}
20+
option { value: "Mango", "Mango"}
21+
}
22+
})
23+
};
24+
25+
// TODO - how do uncontrolled things work?
26+
static UncontrolledSelect: FC<()> = |cx| {
27+
let value = use_state(cx, || String::new());
28+
29+
cx.render(rsx! {
30+
select {
31+
option { value: "Grapefruit", "Grapefruit"}
32+
option { value: "Lime", "Lime"}
33+
option { value: "Coconut", "Coconut"}
34+
option { value: "Mango", "Mango"}
35+
}
36+
})
37+
};

0 commit comments

Comments
 (0)