From 8956f485a80bc713f61210748025e0cb542b3fa3 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 13 Nov 2023 17:54:14 -0500 Subject: [PATCH] feat: init hook for element constructor --- macros/src/lib.rs | 4 +++- wasm-web-component/src/lib.rs | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2d00bf5..93a9bf9 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -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() {{ diff --git a/wasm-web-component/src/lib.rs b/wasm-web-component/src/lib.rs index 1671827..e92bd3e 100644 --- a/wasm-web-component/src/lib.rs +++ b/wasm-web-component/src/lib.rs @@ -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.