#![doc = include_str!("../README.md")]
#![allow(clippy::multiple_crate_versions)]
use std::borrow::Cow;
use dioxus::prelude::*;
use wasm_bindgen::prelude::*;
use web_sys::{Document, ShadowRoot};
use crate::rust_component::RustComponent;
pub use dioxus_web_component_macro::web_component;
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>),
Multiple(Vec<InjectedStyle>),
}
impl InjectedStyle {
#[must_use]
pub fn css(css: &'static str) -> Self {
Self::Css(Cow::Borrowed(css))
}
#[must_use]
pub fn stylesheet(url: &'static str) -> Self {
Self::Stylesheet(Cow::Borrowed(url))
}
fn inject(&self, document: &Document, root: &ShadowRoot) {
match self {
Self::None => {}
Self::Css(css) => {
let style_el = document.create_element("style").unwrap_throw();
style_el.set_inner_html(css);
root.append_child(&style_el).unwrap_throw();
}
Self::Stylesheet(url) => {
let link_el = document.create_element("link").unwrap_throw();
link_el.set_attribute("rel", "stylesheet").unwrap_throw();
link_el.set_attribute("href", url).unwrap_throw();
root.append_child(&link_el).unwrap_throw();
}
Self::Multiple(styles) => {
for style in styles {
style.inject(document, root);
}
}
}
}
}
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/shim.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()
}