web_component.rs 940 B

1234567891011121314151617181920212223242526272829303132
  1. //! Dioxus allows webcomponents to be created with a simple syntax.
  2. //!
  3. //! Read more about webcomponents [here](https://developer.mozilla.org/en-US/docs/Web/Web_Components)
  4. //!
  5. //! We typically suggest wrapping webcomponents in a strongly typed interface using a component.
  6. use dioxus::prelude::*;
  7. fn main() {
  8. dioxus::launch(app);
  9. }
  10. fn app() -> Element {
  11. rsx! {
  12. div {
  13. h1 { "Web Components" }
  14. CoolWebComponent { my_prop: "Hello, world!".to_string() }
  15. }
  16. }
  17. }
  18. /// A web-component wrapped with a strongly typed interface using a component
  19. #[component]
  20. fn CoolWebComponent(my_prop: String) -> Element {
  21. rsx! {
  22. // rsx! takes a webcomponent as long as its tag name is separated with dashes
  23. web-component {
  24. // Since web-components don't have built-in attributes, the attribute names must be passed as a string
  25. "my-prop": my_prop,
  26. }
  27. }
  28. }