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
See [web_component] macro documentation for more details.
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;
use *;
// 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:
tagto set the HTML custom element tag name. By default, it's the kebab case version of the function name.styleto provide the [InjectedStyle] to your component.
The parameters of the component could be:
- an attribute if you want to pass the parameter as an HTML attribute,
- a property if you only want to read/write the parameter as a property of the Javascript
HTMLElement, - or an event if the parameter is a Dioxus
EventHandler.
💡TIP: You can be an attribute AND a property if you use the two annotations.
Attributes
Attributes are pure HTML attributes, should be deserialize from string.
Attributes can be customized with the #[attribute] annotation with:
nameto set the HTML attribute name. By default, it's the kebab-case of the parameter name.optionto mark the attribute optional.trueby default if the type isOption<...>.initialto set the default value when the HTML attribute is missing By default use thestd::default::Defaultimplementation of the type.parseto provide the conversion between the HTML attribute value (a string) to the type value. By default use thestd::str::FromStrimplementation, and fall to the default value if it fails.
Property
Properties are custom properties accessible from Javascript.
To declare a property, you need to use the #[property] annotation.
We use wasm-bindgen to convert the Rust side value to a Javascript value.
You can customize the property with these attributes:
nameto set the Javascript name of the property. By default, it's the camelCase of the parameter name.readonlyto only generate the custom getterinitialto set the default value when the HTML attribute is missing By default use thestd::defaultDefaultimplementation of the type.try_from_jsto provide the conversion from aJsValueto the parameter type. By default use thestd::convert::TryIntoimplementation. The error case is ignored (does not set the value)try_into_jsto provide the conversion from the parameter type to aJsValue. By default use thestd::convert::TryIntoimplementation. Returnundefinedin case of error
⚠️ WARN: reading a property value return a JS Promise.
Events
Events are parameters with the Dioxus EventHandler<...> type.
You can customize the event with these attributes:
nameto set the HTML event name. By default use the parameter name without theonprefix (if any)no_bubbleto forbid the custom event from bubblingno_cancelto remove the ability to cancel the custom event
Usage without macro
Currently, the idea is to avoid breaking changes when you use the macros, but you should expect to have some in the API.
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
The counter example looks like this:
use *;
use ;
use ;
use *;
/// Install (register) the web component
///#[wasm_bindgen(start)]
/// The Dioxus component
Limitations
- only extends
HTMLElement - only work as a replacement of Dioxus
#[component]annotation (does not work with handmadeProps) - cannot add a method callable from Javascript in the web component.
- property getters return a JS promise
Contributions
Contributions are welcome ❤️.