2024-02-14 21:48:58 +00:00
|
|
|
//! This example demonstrates how to use the spread operator to pass attributes to child components.
|
|
|
|
//!
|
|
|
|
//! This lets components like the `Link` allow the user to extend the attributes of the underlying `a` tag.
|
|
|
|
//! These attributes are bundled into a `Vec<Attribute>` which can be spread into the child component using the `..` operator.
|
|
|
|
|
2024-01-16 05:12:44 +00:00
|
|
|
use dioxus::prelude::*;
|
2023-09-20 21:15:11 +00:00
|
|
|
|
|
|
|
fn main() {
|
2024-01-31 01:59:57 +00:00
|
|
|
let dom = VirtualDom::prebuilt(app);
|
2023-09-22 14:24:00 +00:00
|
|
|
let html = dioxus_ssr::render(&dom);
|
|
|
|
|
|
|
|
println!("{}", html);
|
2023-09-20 21:15:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-03-18 22:34:46 +00:00
|
|
|
SpreadableComponent {
|
2023-09-27 00:23:00 +00:00
|
|
|
width: "10px",
|
2023-09-27 15:05:02 +00:00
|
|
|
extra_data: "hello{1}",
|
|
|
|
extra_data2: "hello{2}",
|
2023-09-27 00:23:00 +00:00
|
|
|
height: "10px",
|
2024-01-06 14:58:49 +00:00
|
|
|
left: 1
|
2023-09-27 00:23:00 +00:00
|
|
|
}
|
2023-09-20 21:15:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:21:19 +00:00
|
|
|
#[derive(Props, PartialEq, Clone)]
|
|
|
|
struct Props {
|
2023-09-27 00:23:00 +00:00
|
|
|
#[props(extends = GlobalAttributes)]
|
2024-01-14 21:21:19 +00:00
|
|
|
attributes: Vec<Attribute>,
|
2024-01-16 01:04:39 +00:00
|
|
|
|
2024-01-14 21:21:19 +00:00
|
|
|
extra_data: String,
|
2024-01-20 08:11:55 +00:00
|
|
|
|
2024-01-14 21:21:19 +00:00
|
|
|
extra_data2: String,
|
2023-09-27 00:23:00 +00:00
|
|
|
}
|
2024-01-20 08:11:55 +00:00
|
|
|
|
2024-03-18 22:34:46 +00:00
|
|
|
#[component]
|
|
|
|
fn SpreadableComponent(props: Props) -> Element {
|
2024-01-20 08:11:55 +00:00
|
|
|
rsx! {
|
|
|
|
audio { ..props.attributes, "1: {props.extra_data}\n2: {props.extra_data2}" }
|
|
|
|
}
|
|
|
|
}
|