docs: demonstrate extending both global and element attributes (#3555)
Co-authored-by: fabian <fabian@staff.identeco.de>
This commit is contained in:
1 parent
1419d9c71b
commit
a621cb8044
2 files changed
+67
-11
No files matched your search
@@ -301,23 +301,49 @@ The `extends` attribute lets you extend your props with all the attributes from
|
|||||||
```rust, no_run
|
```rust, no_run
|
||||||
# use dioxus::prelude::*;
|
# use dioxus::prelude::*;
|
||||||
#[component]
|
#[component]
|
||||||
fn Button(
|
fn Card(
|
||||||
// You can use the `extends` attribute on a field with the type `Vec<Attribute>` to extend the props with all the attributes from an element or the global element attributes.
|
// You can use the `extends` attribute on a field with the type `Vec<Attribute>` to extend the props with all the attributes from an element or the global element attributes.
|
||||||
#[props(extends = GlobalAttributes)]
|
#[props(extends = GlobalAttributes)]
|
||||||
attributes: Vec<Attribute>,
|
attributes: Vec<Attribute>,
|
||||||
) -> Element {
|
) -> Element {
|
||||||
rsx! {
|
rsx! {
|
||||||
// Instead of copying over every single attribute, we can just spread the attributes from the props into the button.
|
// Instead of copying over every single attribute, we can just spread the attributes from the props into the element.
|
||||||
button { ..attributes, "button" }
|
div { ..attributes, "card" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
// Since we extend global attributes, you can use any attribute that would normally appear on the button element.
|
// Since we extend global attributes, you can use any attribute that would normally appear on elements.
|
||||||
Button {
|
Card {
|
||||||
width: "10px",
|
width: "10px",
|
||||||
height: "10px",
|
height: "10px",
|
||||||
color: "red",
|
color: "red",
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To extend the props with both the global attributes and the attributes of a specific element, you can use the `extends` attribute multiple times:
|
||||||
|
|
||||||
|
```rust, no_run
|
||||||
|
# use dioxus::prelude::*;
|
||||||
|
#[component]
|
||||||
|
fn Button(
|
||||||
|
#[props(extends = GlobalAttributes, extends = button)]
|
||||||
|
attributes: Vec<Attribute>,
|
||||||
|
) -> Element {
|
||||||
|
rsx! {
|
||||||
|
button { ..attributes, "button" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
Button {
|
||||||
|
// A global attribute
|
||||||
|
width: "10px",
|
||||||
|
// A button specific attribute
|
||||||
|
disabled: true,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that extending from multiple elements will only work if the elements don't have conflicting attributes.
|
||||||
@@ -314,26 +314,56 @@ The `extends` attribute lets you extend your props with all the attributes from
|
|||||||
```rust, no_run
|
```rust, no_run
|
||||||
# use dioxus::prelude::*;
|
# use dioxus::prelude::*;
|
||||||
#[derive(Props, PartialEq, Clone)]
|
#[derive(Props, PartialEq, Clone)]
|
||||||
struct ButtonProps {
|
struct CardProps {
|
||||||
/// You can use the `extends` attribute on a field with the type `Vec<Attribute>` to extend the props with all the attributes from an element or the global element attributes.
|
/// You can use the `extends` attribute on a field with the type `Vec<Attribute>` to extend the props with all the attributes from an element or the global element attributes.
|
||||||
#[props(extends = GlobalAttributes)]
|
#[props(extends = GlobalAttributes)]
|
||||||
attributes: Vec<Attribute>,
|
attributes: Vec<Attribute>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn Button(props: ButtonProps) -> Element {
|
fn Card(props: CardProps) -> Element {
|
||||||
rsx! {
|
rsx! {
|
||||||
// Instead of copying over every single attribute, we can just spread the attributes from the props into the button.
|
// Instead of copying over every single attribute, we can just spread the attributes from the props into the element.
|
||||||
button { ..props.attributes, "button" }
|
div { ..props.attributes, "card" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
// Since we extend global attributes, you can use any attribute that would normally appear on the button element.
|
// Since we extend global attributes, you can use any attribute that would normally appear on elements.
|
||||||
Button {
|
Card {
|
||||||
width: "10px",
|
width: "10px",
|
||||||
height: "10px",
|
height: "10px",
|
||||||
color: "red",
|
color: "red",
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To extend the props with both the global attributes and the attributes of a specific element, you can use the `extends` attribute multiple times:
|
||||||
|
|
||||||
|
```rust, no_run
|
||||||
|
# use dioxus::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Props, PartialEq, Clone)]
|
||||||
|
struct ButtonProps {
|
||||||
|
#[props(extends = GlobalAttributes, extends = button)]
|
||||||
|
attributes: Vec<Attribute>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn Button(props: ButtonProps) -> Element {
|
||||||
|
rsx! {
|
||||||
|
button { ..props.attributes, "button" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
Button {
|
||||||
|
// A global attribute
|
||||||
|
width: "10px",
|
||||||
|
// A button specific attribute
|
||||||
|
disabled: true,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that extending from multiple elements will only work if the elements don't have conflicting attributes.
|
||||||
Reference in new issue
Block a user