#![doc = include_str!("../README.md")]
#![allow(clippy::multiple_crate_versions)]
use std::borrow::Cow;
use dioxus::prelude::*;
use wasm_bindgen::prelude::*;
use crate::rust_component::RustComponent;
mod event;
pub use self::event::*;
mod rust_component;
#[non_exhaustive]
pub enum Message {
AttributeChanged {
name: String,
new_value: Option<String>,
},
}
#[derive(Clone)]
pub struct Context {
pub event_target: web_sys::HtmlElement,
pub rx: Receiver<Message>,
}
#[derive(Debug, Clone, Default)]
pub enum InjectedStyle {
#[default]
None,
Css(Cow<'static, str>),
Stylesheet(Cow<'static, str>),
}
pub trait DioxusWebComponent {
#[must_use]
fn attributes() -> &'static [&'static str] {
&[]
}
fn element() -> Element;
#[must_use]
fn style() -> InjectedStyle {
InjectedStyle::default()
}
}
pub fn register_dioxus_web_component<E>(custom_tag: &str)
where
E: DioxusWebComponent,
{
let attributes = E::attributes().iter().map(ToString::to_string).collect();
let dx_el_builder = E::element;
let style = E::style();
let rust_component = RustComponent {
attributes,
dx_el_builder,
style,
};
register_web_component(custom_tag, rust_component);
}
#[wasm_bindgen(module = "/src/lib.js")]
extern "C" {
#[allow(unsafe_code)]
fn register_web_component(custom_tag: &str, rust_component: RustComponent);
}
#[doc(hidden)]
pub type Sender<T> = async_channel::Sender<T>;
#[doc(hidden)]
pub type Receiver<T> = async_channel::Receiver<T>;
pub(crate) fn create_channel<T>() -> (Sender<T>, Receiver<T>) {
async_channel::unbounded()
}