feat: init hook for element constructor

This commit is contained in:
Jeremy Wall 2023-11-13 17:54:14 -05:00
parent c39293636d
commit 8956f485a8
2 changed files with 8 additions and 1 deletions

View File

@ -133,7 +133,9 @@ fn expand_wc_struct_trait_shim(
"class {name} extends HTMLElement {{
constructor() {{
super();
this._impl = impl();
var self = this;
self._impl = impl();
self._impl.init();
}}
connectedCallback() {{

View File

@ -142,6 +142,11 @@ pub trait WebComponentDef: IntoWasmAbi + Default {
/// Each method is optional. You only need to implement the ones
/// you want to specify behavior for.
pub trait WebComponentBinding: WebComponentDef {
/// Called during element construction.
fn init(&self, _element: &HtmlElement) {
// noop
}
/// Called when the web component is connected to the DOM.
/// This is when you should do any setup like attaching a ShadowDom
/// or appending elements.