|
@@ -25,7 +25,7 @@ Produces:
|
|
|
|
|
|
## Composing Elements
|
|
## Composing Elements
|
|
|
|
|
|
-Every element has a set of properties that can be rendered in different ways. In particular, each Element may contain other Elements. To achieve this, we can simply declare new Elements within the parent:
|
|
|
|
|
|
+Every element has a set of properties that can be rendered in different ways. In particular, each Element may contain other Elements. To achieve this, we can simply declare new Elements contained within the parent's curly braces:
|
|
|
|
|
|
```rust
|
|
```rust
|
|
#use dioxus::prelude::*;
|
|
#use dioxus::prelude::*;
|
|
@@ -37,17 +37,27 @@ rsx!(
|
|
}
|
|
}
|
|
)
|
|
)
|
|
```
|
|
```
|
|
|
|
+As you might expect, the generated HTML for this structure would look like:
|
|
|
|
+```html
|
|
|
|
+<div>
|
|
|
|
+ <h1></h1>
|
|
|
|
+ <h2></h2>
|
|
|
|
+ <p></p>
|
|
|
|
+</div>
|
|
|
|
+```
|
|
|
|
+
|
|
With the default configuration, any Element defined within the `dioxus-html` crate can be declared in this way. To create your own new elements, see the `Custom Elements` Advanced Guide.
|
|
With the default configuration, any Element defined within the `dioxus-html` crate can be declared in this way. To create your own new elements, see the `Custom Elements` Advanced Guide.
|
|
|
|
|
|
## Text Elements
|
|
## Text Elements
|
|
|
|
|
|
-Dioxus also supports a special type of Element: Text. Text Elements do not accept children, but rather just text denoted with double quotes.
|
|
|
|
|
|
+Dioxus also supports a special type of Element: Text. Text Elements do not accept children, but rather just string literals denoted with double quotes.
|
|
|
|
|
|
```rust
|
|
```rust
|
|
rsx! (
|
|
rsx! (
|
|
"hello world"
|
|
"hello world"
|
|
)
|
|
)
|
|
```
|
|
```
|
|
|
|
+
|
|
Text Elements can be composed within other Elements:
|
|
Text Elements can be composed within other Elements:
|
|
```rust
|
|
```rust
|
|
rsx! (
|
|
rsx! (
|
|
@@ -57,7 +67,8 @@ rsx! (
|
|
}
|
|
}
|
|
)
|
|
)
|
|
```
|
|
```
|
|
-Text can also be formatted with any value that implements `Display`. We use f-string formatting - a "coming soon" feature for stable Rust that is familiar for Python and JavaScript users:
|
|
|
|
|
|
+
|
|
|
|
+Text can also be formatted with any value that implements `Display`. We use [f-string formatting](https://docs.rs/fstrings/0.2.3/fstrings/) - a "coming soon" feature for stable Rust that is familiar for Python and JavaScript users:
|
|
|
|
|
|
```rust
|
|
```rust
|
|
let name = "Bob";
|
|
let name = "Bob";
|
|
@@ -81,6 +92,7 @@ rsx!(
|
|
```
|
|
```
|
|
|
|
|
|
Each field is defined as a method on the element in the `dioxus-html` crate. This prevents you from misspelling a field name and lets us provide inline documentation. When you need to use a field not defined as a method, you have two options:
|
|
Each field is defined as a method on the element in the `dioxus-html` crate. This prevents you from misspelling a field name and lets us provide inline documentation. When you need to use a field not defined as a method, you have two options:
|
|
|
|
+
|
|
1) file an issue if the attribute _should_ be enabled
|
|
1) file an issue if the attribute _should_ be enabled
|
|
2) add a custom attribute on-the-fly
|
|
2) add a custom attribute on-the-fly
|
|
|
|
|
|
@@ -89,11 +101,36 @@ To use custom attributes, simply put the attribute name in quotes followed by a
|
|
```rust
|
|
```rust
|
|
rsx!(
|
|
rsx!(
|
|
div {
|
|
div {
|
|
- "custom_attr": "important data here"
|
|
|
|
|
|
+ "customAttr": "important data here"
|
|
}
|
|
}
|
|
)
|
|
)
|
|
```
|
|
```
|
|
|
|
|
|
|
|
+Note: the name of the custom attribute must match exactly what you want the renderer to output. All attributes defined as methods in `dioxus-html` follow the snake_case naming convention. However, they internally translate their snake_case convention to HTML's camelCase convention.
|
|
|
|
+
|
|
## Listeners
|
|
## Listeners
|
|
|
|
|
|
-## Arbitrary Tokens
|
|
|
|
|
|
+Listeners are a special type of Attribute that only accept functions. Listeners let us attach functionality to our Elements by running a provided closure whenever the specified Listener is triggered.
|
|
|
|
+
|
|
|
|
+We'll cover listeners in more depth in the Listeners chapter, but for now, just know that every listener must start with the `on` keyword and can accept either a closure or an expression wrapped in curly braces.
|
|
|
|
+
|
|
|
|
+```rust
|
|
|
|
+rsx!(
|
|
|
|
+ div {
|
|
|
|
+ onclick: move |_| {}
|
|
|
|
+ onmouseover: {handler},
|
|
|
|
+ }
|
|
|
|
+)
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+## Moving On
|
|
|
|
+
|
|
|
|
+This chapter just scratches the surface on how Elements can be defined.
|
|
|
|
+
|
|
|
|
+We learned:
|
|
|
|
+- Elements are the basic building blocks of User Interfaces
|
|
|
|
+- Elements can contain other elements
|
|
|
|
+- Elements can either be a named container or text
|
|
|
|
+- Some Elements have properties that the renderer can use to draw the UI to the screen
|
|
|
|
+
|
|
|
|
+Next, we'll compose Elements together to form components.
|