custom_elements.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //! Example: Web Components & Custom Elements
  2. //! -----------------------------------------
  3. //!
  4. //! Web components are a flavor of html elements that can be user-defined.
  5. //! See: https://www.webcomponents.org for more information.
  6. //!
  7. //! Users who use webcomponents typically don't use Dioxus. However, if you would like to use webcomponents in Dioxus,
  8. //! you can easily create a new custom element with compile-time correct wrappers around the webcomponent.
  9. //!
  10. //! We don't support building new webcomponents with Dioxus, however. :(
  11. use dioxus::prelude::*;
  12. pub static Example: FC<()> = |cx, props| {
  13. cx.render(rsx! {
  14. div {
  15. custom_element {
  16. custom_attr: "custom data on custom elements"
  17. }
  18. }
  19. })
  20. };
  21. mod dioxus_elements {
  22. use std::fmt::Arguments;
  23. use dioxus::prelude::DioxusElement;
  24. #[allow(non_camel_case_types)]
  25. pub struct custom_element;
  26. impl DioxusElement for custom_element {
  27. const TAG_NAME: &'static str = "custom_element";
  28. const NAME_SPACE: Option<&'static str> = None;
  29. }
  30. impl custom_element {
  31. pub fn custom_attr<'a>(&self, f: NodeFactory<'a>, val: Arguments) -> Attribute<'a> {
  32. f.attr("custom_asttr", val, None, false)
  33. }
  34. }
  35. // Re-export the normal html namespace
  36. pub use dioxus::prelude::dioxus_elements::*;
  37. use dioxus_core::{nodes::Attribute, NodeFactory};
  38. }