dioxus/examples/core_reference/inline_styles.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

2021-07-16 20:11:25 +00:00
//! Example: Inline Styles
//! ----------------------
//!
//! This example shows how to use inline styles in Dioxus components.
//!
2021-10-24 17:30:36 +00:00
//! Inline styles function very similarly to regular attributes, just grouped together in "style".
2021-07-16 20:11:25 +00:00
//!
//! Inline styles in Dioxus are more performant than React since we're able to cache attributes and compare by pointers.
//! However, it's still not as performant as cascaded styles. Use with care.
2021-07-02 05:30:52 +00:00
use dioxus::prelude::*;
pub fn Example(cx: Scope) -> Element {
2021-07-02 05:30:52 +00:00
cx.render(rsx! {
2021-07-16 20:11:25 +00:00
head {
style: { background_color: "powderblue" }
}
body {
h1 { style: { color: "blue" }
"This is a heading"
}
p { style: { color: "red" }
"This is a paragraph"
}
}
})
}
2021-07-02 05:30:52 +00:00
2021-10-24 17:30:36 +00:00
// .... technically the rsx! macro is slightly broken at the moment and allows styles not wrapped in style {}
2021-07-16 20:11:25 +00:00
// I haven't noticed any name collisions yet, and am tentatively leaving this behavior in..
// Don't rely on it.
pub fn Example2(cx: Scope) -> Element {
2021-07-16 20:11:25 +00:00
cx.render(rsx! {
div { color: "red"
"hello world!"
2021-07-02 05:30:52 +00:00
}
})
}