dioxus-web-component-macro
Provide a proc macro to build Dioxus web component.
Example
The macro replaces the Dioxus #[component] macro.
use *;
use ;
use *;
<!-- include the script generated with wasm-pack -->
<!-- in the body -->
Usage
Tag
The custom element tag is built from the component name.
By default, the tag is the kebab-case version of the name.
For example, MyWebComponent becomes my-web-component.
You can change the default behavior with the tag attribute.
use *;
use ;
<!-- in the body -->
ℹ️ INFO: the custom element tag name have constraints. The macro checks the validity of the tag for you. See MDN - Valid custom element names
Style
You can provide the web component style with the style attribute.
use *;
use ;
The dioxus_web_component::InjectedStyle could be raw CSS included in
an HTML <style>...</style> element, or a link to an external stylesheet,
or a list of InjectedStyle styles.
⚠️ WARNING: the web component is wrapped into an HTML div with the dioxus CSS class.
Component parameters
Every parameter of your component should be either an attribute or an event. (properties are not yet supported)
The proc macro tries to detect the kind of parameter by looking at its type.
If the type starts by EventHandler it is expected to be an event.
But, this kind of detection is not reliable, so you might need to add an annotation
to correct this behavior.
Attributes
Attributes are like the href of an <a> HTML element.
You can enforce the parameter to be an attribute with the #[attribute] annotation.
When the attribute value changes the dioxus component will be rendered.
Attribute name
The attribute name is by default the kebab-case of the parameter name.
You can choose another name with #[attribute(name = "my-custom-name")].
Attribute option
The attribute could be optional or not.
The proc macro tries to detect it automatically with the type name.
However the detection is not reliable, so you can use the #[attribute(option = true)]
to fix the detection if necessary.
Attribute initial
Attributes require to have an initial value. This value is used when no HTML attribute is provided, or if the attribute is removed.
By default, we expect the attribute type to implement [std::default::Default].
If it's not the case, or if you want to use another value for your attribute you
can provide your default expression with #[attribute(initial = String::from("World"))].
Attribute parse
HTML attributes are strings and optional, so we need to convert the attribute value into the component parameter type.
The proc macro uses the std::str::parse method. That means the target type
needs to implement the std::str::FromStr trait.
In case of an error, the initial value (see below) is used.
If you want to change this behavior, you can provide your parsing expression.
If the parameter type is optional, the parse expression is used in this code:
let value = new_value.and_then(#parse);.
If the type is NOT optional, the code looks like let value = new_value.and_then(#parse).unwrap_or_else(|| #initial);.
The expected type for the parsing expression is FnOnce(String) -> Option<T>.
The default expression is |value| value.parse().ok().
For example, if you have a parameter required of type bool and you want the value to be true
if the attribute is present whatever the content of the attribute, you could use #[attribute(parse = |s| !s.is_empty() )].
Events
The web component could send custom events.
If the type of the component parameter is EventHandler, the parameter is detected as an event.
Because this detection is not reliable, you could enforce a parameter to be
an event with the #[event] annotation.
The custom event detail corresponds to the generic type of the Dioxus EventHandler.
⚠️ IMPORTANT: The event type needs to implement Into<JsValue> and be 'static (does not have any reference).
You may need to implement it manually.
You could use serde-wasm-bindgen, gloo_utils::format::JsValueSerdeExt, wasm_bindgen::UnwrapThrowExt
to implement the Into<JsValue> trait.
Event name
The HTML event name is detected from the parameter name by removing the on_ (or on) prefix
and converting the name to kebab-case.
You can choose your value with the name attribute like #[event(name = "build")]
to dispatch a build event.
Event no_bubble
By default, the event bubbles up through the DOM.
You can avoid the bubbling with #[event(no_bubble = true)].
Event no_cancel
By default, the event is cancelable.
You can avoid the bubbling with #[event(no_cancel = true)].
Properties
Not yet supported.