From 8ce79d34a57faa148dbc610292029077b24c9a0c Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 15 Nov 2023 20:53:54 -0500 Subject: [PATCH] Add the observed event handlers to the shadowRoot If it exists then add it there. Otherwise add it to the custom element itself. --- macros/src/lib.rs | 11 +++++++++-- wasm-web-component/Cargo.toml | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index d746946..2e96b78 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -144,8 +144,15 @@ fn expand_wc_struct_trait_shim( super(); this._impl = impl(); this._impl.init_impl(this); - for (const t of this.observedEvents()) {{ - this.addEventListener(t, function(evt) {{ this.handleComponentEvent(evt); }} ); + var self = this; + if (self.shadowRoot) {{ + for (const t of this.observedEvents()) {{ + self.shadowRoot.addEventListener(t, function(evt) {{ self.handleComponentEvent(evt); }} ); + }} + }} else {{ + for (const t of self.observedEvents()) {{ + self.addEventListener(t, function(evt) {{ self.handleComponentEvent(evt); }} ); + }} }} }} diff --git a/wasm-web-component/Cargo.toml b/wasm-web-component/Cargo.toml index 89c888d..84a8873 100644 --- a/wasm-web-component/Cargo.toml +++ b/wasm-web-component/Cargo.toml @@ -24,8 +24,8 @@ version = "0.3" version = "0.3" features = [ "CustomElementRegistry", + "CustomEvent", "Document", - "KeyboardEvent", "Event", "EventTarget", "Element",