Dioxus Web Component
This crate provides a bridge to expose a Dioxus component as a web component.
This crate supports web component attributes and custom events. You can also add CSS style to your web component.
Take a look at the examples to see the usage in a full project: https://github.com/ilaborie/dioxus-web-component/tree/main/examples
If you are new to WebAssembly with Rust, take a look at the Rust WebAssembly book first.
Usage with macro
Ideally, you only need to replace the Dioxus #[component]
by #[web_component]
.
Then you should register the web component with wasm-bindgen.
To finish, you can create the npm package with wasm-pack.
use *;
use web_component;
// Function to call from the JS side
Then call the function from the JS side.
Customization of the web component
The #[web_component]
annotation can be configured with:
tag
to set the HTML custom element tag name. By default, it's the kebab case version of the function name.style
to provide the [InjectedStyle
] to your component.
Parameters of the component could be either an attribute or an event.
Attributes can be customized with the #[attribute]
annotation with:
name
to set the HTML attribute name. By default, it's the kebab-case of the parameter name.option
to mark the attribute optional.true
by default if the type isOption<...>
.initial
to set the default value when the HTML attribute is missing By default use thestd::defaultDefault
implementation of the type.parse
to provide the conversion between the HTML attribute value (a string) to the type value. By default use thestd::str::FromStr
implementation, and fall to the default value if it fails.
Events are parameters with the Dioxus EventHandler<...>
type.
You can customize the event with these attributes:
name
to set the HTML event name. By default use the parameter name without theon
prefix (if any)no_bubble
to forbid the custom event to bubbleno_cancel
to remove the ability to cancel the custom event
This example uses all annotations:
use *;
use web_component;
)]
attr1: String,
attr_option: ,
event: ,
)
See dioxus-web-component-macro documentation for more details.
Usage without macro
You can provide your manual implementation of [DioxusWebComponent
] and call
[register_dioxus_web_component
] to register your web component.
The key point is to use a Shared
element in the dioxus context.
For example, the greeting example could be written with
use *;
use ;
use *;
/// Install (register) the web component
///
/// # Errors
///
/// Registering the web-component may fail
// #[web_component(tag = "plop-greeting", style = InjectedStyle::css(include_str!("./style.css")) )]
The counter example looks like this:
use *;
use ;
use *;
/// Install (register) the web component
///
/// # Errors
///
/// Registering the web-component may fail
/// The Dioxus component
// #[web_component(tag = "plop-counter", style = InjectedStyle::stylesheet("./style.css"))]
Limitations
- web component properties not (yet) supported
- only extends
HTMLElement
- only work as a replacement of Dioxus
#[component]
annotation (does not work with handmadeProps
) - no validation of the custom element name
Contributions
Contributions are welcome ❤️.