inline_styles.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //! Example: Inline Styles
  2. //! ----------------------
  3. //!
  4. //! This example shows how to use inline styles in Dioxus components.
  5. //!
  6. //! Inline styles function very similarly to regular attributes, just grouped together in "style".
  7. //!
  8. //! Inline styles in Dioxus are more performant than React since we're able to cache attributes and compare by pointers.
  9. //! However, it's still not as performant as cascaded styles. Use with care.
  10. use dioxus::prelude::*;
  11. pub static Example: Component<()> = |cx| {
  12. cx.render(rsx! {
  13. head {
  14. style: { background_color: "powderblue" }
  15. }
  16. body {
  17. h1 { style: { color: "blue" }
  18. "This is a heading"
  19. }
  20. p { style: { color: "red" }
  21. "This is a paragraph"
  22. }
  23. }
  24. })
  25. };
  26. // .... technically the rsx! macro is slightly broken at the moment and allows styles not wrapped in style {}
  27. // I haven't noticed any name collisions yet, and am tentatively leaving this behavior in..
  28. // Don't rely on it.
  29. static Example2: Component<()> = |cx| {
  30. cx.render(rsx! {
  31. div { color: "red"
  32. "hello world!"
  33. }
  34. })
  35. };