Skip to main content

CustomElement

Trait CustomElement 

Source
pub trait CustomElement {
    // Provided methods
    fn connected_callback(&mut self) { ... }
    fn disconnected_callback(&mut self) { ... }
    fn adopted_callback(&mut self) { ... }
    fn handle_event(&mut self, _event: Event) { ... }
    fn connected_move_callback(&mut self) { ... }
    fn attribute_changed_callback(
        &mut self,
        _name: &str,
        _old_value: JsValue,
        _new_value: JsValue,
    ) { ... }
}
Expand description

This trait is the focal point of this crate, and implementing it allows your Rust struct to be used as a custom element.

It specifies all the methods can be called from the JavaScript wrapper class.

All of these functions have been provided default, no-op implementations. You may optionally provide implementations to hook into the custom element lifecycle.

Provided Methods§

Source

fn connected_callback(&mut self)

Called each time the element is added to the document

See MDN Documentation

Source

fn disconnected_callback(&mut self)

Called each time the element is removed from the document

See MDN Documentation

Source

fn adopted_callback(&mut self)

Called each time the element is moved to a new document

See MDN Documentation

Source

fn handle_event(&mut self, _event: Event)

Enables using the custom element instance as an event handler catch-all

see https://gomakethings.com/the-handleevent-method-is-the-absolute-best-way-to-handle-events-in-web-components/

Source

fn connected_move_callback(&mut self)

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().

See MDN Documentation

Source

fn attribute_changed_callback( &mut self, _name: &str, _old_value: JsValue, _new_value: JsValue, )

Called when attributes are changed, added, removed, or replaced

See MDN Documentation

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§