custom_element/custom_element.rs
1use wasm_bindgen::JsValue;
2use web_sys::Event;
3
4/// This trait is the focal point of this crate, and implementing it
5/// allows your Rust struct to be used as a custom element.
6///
7/// It specifies all the methods can be called from the JavaScript wrapper class.
8///
9/// All of these functions have been provided default, no-op implementations.
10/// You may optionally provide implementations to hook into the custom element
11/// lifecycle.
12pub trait CustomElement {
13 /// Called each time the element is added to the document
14 ///
15 /// See [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements)
16 fn connected_callback(&mut self) {}
17
18 /// Called each time the element is removed from the document
19 ///
20 /// See [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements)
21 fn disconnected_callback(&mut self) {}
22
23 /// Called each time the element is moved to a new document
24 ///
25 /// See [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements)
26 fn adopted_callback(&mut self) {}
27
28 /// Enables using the custom element instance as an event handler catch-all
29 ///
30 /// see <https://gomakethings.com/the-handleevent-method-is-the-absolute-best-way-to-handle-events-in-web-components/>
31 fn handle_event(&mut self, _event: Event) {}
32
33 /// When defined, this is called instead of connectedCallback() and disconnectedCallback() each time the element is moved to a different place in the DOM via Element.moveBefore().
34 ///
35 /// See [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements)
36 fn connected_move_callback(&mut self) {}
37
38 /// Called when attributes are changed, added, removed, or replaced
39 ///
40 /// See [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements)
41 fn attribute_changed_callback(
42 &mut self,
43 _name: &str,
44 _old_value: JsValue,
45 _new_value: JsValue,
46 ) {
47 }
48}