1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//! The Web Components standard creates a browser feature that allows you to create reusable components, called Custom Elements.
//!
//! While web_sys exposes the browser’s CustomElementRegistry interface, it can be hard to use. Creating a Custom Element requires calling customElements.define() and passing it an ES2015 class that extends HTMLElement, which is not currently possible to do directly from Rust.
//!
//! This crate provides a [CustomElement][CustomElement] trait that, when implemented, allows you to encapsulate any Rust structure as a reusable web component without writing any JavaScript. In theory it should be usable with any Rust front-end framework.
//! ```rust
//! impl CustomElement for MyWebComponent {
//! fn to_node(&mut self) -> Node {
//! self.view()
//! }
//!
//! fn observed_attributes() -> Vec<&'static str> {
//! vec!["name"]
//! }
//!
//! fn attribute_changed_callback(
//! &self,
//! this: &HtmlElement,
//! name: String,
//! old_value: Option<String>,
//! new_value: Option<String>,
//! ) {
//! if name == "name" {
//! // do something
//! }
//! }
//! }
//!
//! #[wasm_bindgen]
//! pub fn define_custom_elements() {
//! MyWebComponent::define("my-component");
//! }
//! ```
use std::sync::{Arc, Mutex};
use wasm_bindgen::{prelude::*, JsCast};
use web_sys::{HtmlElement, Node};
/// A custom DOM element that can be reused via the Web Components/Custom Elements standard.
///
/// Note that your component should implement [Default][std::default::Default], which allows the
/// browser to initialize a “default” blank component when a new custom element node is created.
pub trait CustomElement: Default + 'static {
/// Returns a root [Node][web_sys::Node] that will be appended to the custom element.
/// Depending on your component, this will probably do some kind of initialization or rendering.
fn to_node(&mut self) -> Node;
/// Whether a [Shadow root](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM)
/// should be attached to the element or not. Shadow DOM encapsulates styles, but makes some DOM manipulation more difficult.
///
/// Defaults to `true`.
fn shadow() -> bool {
true
}
/// The names of the attributes whose changes should be observed. If an attribute name is in this list,
/// [attribute_changed_callback](CustomElement::attribute_changed_callback) will be invoked when it changes.
/// If it is not, nothing will happen when the DOM attribute changes.
fn observed_attributes() -> Vec<&'static str> {
Vec::new()
}
/// Invoked each time the custom element is appended into a document-connected element.
/// This will happen each time the node is moved, and may happen before the element's contents have been fully parsed.
fn connected_callback(&mut self, _this: &HtmlElement) {}
/// Invoked each time the custom element is disconnected from the document's DOM.
fn disconnected_callback(&mut self, _this: &HtmlElement) {}
/// Invoked each time the custom element is moved to a new document.
fn adopted_callback(&mut self, _this: &HtmlElement) {}
/// Invoked each time one of the custom element's attributes is added, removed, or changed.
/// To observe an attribute, include it in [observed_attributes](CustomElement::observed_attributes).
fn attribute_changed_callback(
&mut self,
_this: &HtmlElement,
_name: String,
_old_value: Option<String>,
_new_value: Option<String>,
) {
}
/// CSS stylesheet to be attached to the element as a `<style>` tag.
fn style() -> Option<&'static str> {
None
}
/// URLs for CSS stylesheets to be attached to the element as `<link>` tags.
fn style_urls() -> Vec<&'static str> {
Vec::new()
}
/// Must be called somewhere to define the custom element and register it with the DOM Custom Elements Registry.
///
/// Note that custom element names must contain a hyphen.
///
/// ```rust
/// impl CustomElement for MyCustomElement { /* ... */ */}
/// #[wasm_bindgen]
/// pub fn define_elements() {
/// MyCustomElement::define("my-component");
/// }
/// ```
fn define(tag_name: &'static str) {
// constructor function will be called for each new instance of the component
let constructor = Closure::wrap(Box::new(move |this: HtmlElement| {
let component = Arc::new(Mutex::new(Self::default()));
// connectedCallback
let cmp = component.clone();
let connected = Closure::wrap(Box::new({
move |el| {
let mut lock = cmp.lock().unwrap();
lock.connected_callback(&el);
}
}) as Box<dyn FnMut(HtmlElement)>);
js_sys::Reflect::set(
&this,
&JsValue::from_str("_connectedCallback"),
&connected.as_ref().unchecked_ref(),
)
.unwrap();
connected.forget();
// disconnectedCallback
let cmp = component.clone();
let disconnected = Closure::wrap(Box::new(move |el| {
let mut lock = cmp.lock().unwrap();
lock.disconnected_callback(&el);
}) as Box<dyn FnMut(HtmlElement)>);
js_sys::Reflect::set(
&this,
&JsValue::from_str("_disconnectedCallback"),
&disconnected.as_ref().unchecked_ref(),
)
.unwrap();
disconnected.forget();
// adoptedCallback
let cmp = component.clone();
let adopted = Closure::wrap(Box::new(move |el| {
let mut lock = cmp.lock().unwrap();
lock.adopted_callback(&el);
}) as Box<dyn FnMut(HtmlElement)>);
js_sys::Reflect::set(
&this,
&JsValue::from_str("_adoptedCallback"),
&adopted.as_ref().unchecked_ref(),
)
.unwrap();
adopted.forget();
// attributeChangedCallback
let cmp = component.clone();
let attribute_changed = Closure::wrap(Box::new(move |el, name, old_value, new_value| {
let mut lock = cmp.lock().unwrap();
lock.attribute_changed_callback(&el, name, old_value, new_value);
})
as Box<dyn FnMut(HtmlElement, String, Option<String>, Option<String>)>);
js_sys::Reflect::set(
&this,
&JsValue::from_str("_attributeChangedCallback"),
&attribute_changed.as_ref().unchecked_ref(),
)
.unwrap();
attribute_changed.forget();
let mut lock = component.lock().unwrap();
lock.to_node()
}) as Box<dyn FnMut(HtmlElement) -> Node>);
// observedAttributes is static and needs to be known when the class is defined
let attributes = Self::observed_attributes();
let observed_attributes = JsValue::from(
attributes
.iter()
.map(|attr| JsValue::from_str(attr))
.collect::<js_sys::Array>(),
);
// styles
let stylesheets = JsValue::from(
Self::style_urls()
.iter()
.map(|attr| JsValue::from_str(attr))
.collect::<js_sys::Array>(),
);
// call out to JS to define the Custom Element
make_custom_element(
tag_name,
Self::shadow(),
&constructor,
observed_attributes,
Self::style(),
stylesheets,
);
constructor.forget();
}
}
// JavaScript shim
#[wasm_bindgen(module = "/src/make_custom_element.js")]
extern "C" {
fn make_custom_element(
tag_name: &str,
shadow: bool,
build_element: &Closure<dyn FnMut(HtmlElement) -> web_sys::Node>,
observed_attributes: JsValue,
style: Option<&str>,
stylesheets: JsValue,
);
}